Jump to content



Photo
- - - - -

Since we're updating ... deprecated session and ereg solutions

session_is_registered deprecated php 5.3 ereg functions

This topic has been archived. This means that you cannot reply to this topic.
24 replies to this topic

#1   stickypod

stickypod
  • Members
  • 85 posts

Posted 11 November 2011 - 20:38

I recently upgraded to PHP 5.3.8 and as all of you are finding out, this does create some problems with the deprecated code you currently have in your store. Here's a summary of how I solved this problem:

First, do not turn off the error codes. That's the only way you're going to find the problems.
Next, you must change your sessions.php on both the catalog and admin side: /catalog/includes/functions/sessions.php and /catalog/admin/includes/functions/sessions.php

REPLACE THIS: (around line 70)

  function tep_session_register($variable) {
	return session_register($variable);
  }
 
  function tep_session_is_registered($variable) {
	return session_is_registered($variable);
  }
 
  function tep_session_unregister($variable) {
	return session_unregister($variable);
  }

WITH THIS:

// check PHP version and use appropriate session variables - authored by ecartz (Matt)
  function tep_session_register($variable) {
		global $session_started;
 
		if ($session_started == true) {
		  if (PHP_VERSION < 4.3) {
				return session_register($variable);
		  } else {
				if (isset($GLOBALS[$variable])) {
				  $_SESSION[$variable] =& $GLOBALS[$variable];
				} else {
				  $_SESSION[$variable] = null;
				}
		  }
		}
 
		return false;
  }
 
  function tep_session_is_registered($variable) {
		if (PHP_VERSION < 4.3) {
		  return session_is_registered($variable);
		} else {
		  return isset($_SESSION) && array_key_exists($variable, $_SESSION);
		}
  }
 
  function tep_session_unregister($variable) {
		if (PHP_VERSION < 4.3) {
		  return session_unregister($variable);
		} else {
		  unset($_SESSION[$variable]);
		}
  }

Once you have this complete, you need to change all your ereg functions to the new preg functions for 5.3. I used the following tutorial authored by Mark Evans:

https://github.com/o...def4f2c9f94b0b7

Once this was complete, you must update your usps.php files for shipping. catalog/includes/modules/shipping/usps.php

In this file, you will find many ereg functions that need to be changed to preg_match functions. They look like this:

OLD if (ereg('<Error>', $response[0])) {
NEW if (preg_match('/<Error>/', $response[0])) {

OLD $number = ereg('<Number>(.*)</Number>', $response[0], $regs);
NEW $number = preg_match('|<Number>(.*)</Number>|', $response[0], $regs);

Note the | instead of the / in some of the lines. In the 2nd example, you'll see this and that's because the forward slash interferes with the original code. So you must use a pipe ( | ) character for these lines.

Finally, once you have this complete, you'll get your USPS shipping module back and you should be done (FedEx was unaffected and I don't use UPS). If you did not turn off error messages, you will not see any errors.

I run Dynamo Effects One Page Checkout and everything works great after all the above is complete.

Now I'm updated to PHP 5.3.8 and I can start upgrading to osC 3.0 and still run my old store.

I hope this helps others and I take no credit as I did not write any of the code.

Edited by stickypod, 11 November 2011 - 20:42.

Anyone can buy a camera... it's what you do with it that counts!
Sticky Pod
www.stickypod.com

#2 ONLINE   MrPhil

MrPhil
  • Members
  • 5,380 posts

Posted 11 November 2011 - 22:37

OLD if (eregi('<Error>', $response[0])) {
NEW if (preg_match('/<Error>/i', $response[0])) {

OLD $number = eregi('<Number>(.*)</Number>', $response[0], $regs);
NEW $number = preg_match('|<Number>(.*)</Number>|i', $response[0], $regs);

In general, if / is in the pattern, you either choose a different delimiter | or #, or escape any / in the pattern with \ : <\/Number>. If the pattern is a variable, enclose it in quotation marks: "/$pattern/".

OLD $array = split('pattern', $string)
NEW $array = preg_split('/pattern/', $string)

etc.
Also see ereg_replace() and probably a few others.

#3   Landis

Landis
  • Members
  • 2 posts

Posted 05 December 2011 - 14:07

I recently upgraded to PHP 5.3.8 and as all of you are finding out, this does create some problems with the deprecated code you currently have in your store. Here's a summary of how I solved this problem:

First, do not turn off the error codes. That's the only way you're going to find the problems.
Next, you must change your sessions.php on both the catalog and admin side: /catalog/includes/functions/sessions.php and /catalog/admin/includes/functions/sessions.php.......

Now I'm updated to PHP 5.3.8 and I can start upgrading to osC 3.0 and still run my old store.

I hope this helps others and I take no credit as I did not write any of the code.



THANK YOU!
Спасибо,
Landis.

p.s., I also needed this for the language.php
http://forums.oscomm...20#entry1613041

Edited by Landis, 05 December 2011 - 14:10.


#4   hiorti

hiorti
  • Members
  • 4 posts

Posted 06 May 2012 - 00:33

Thank You! This was very helpful. After all changes however, I was having a problem with 'selected_box' variable in admin area not being recorded into the session. I removed the "if ($session_started == true) " condition from inside function tep_session_register and it resolved the issue. Hope this will not create other unseen issues. Thanks again!

Edited by hiorti, 06 May 2012 - 00:33.


#5   jo_cs

jo_cs
  • Members
  • 2 posts

Posted 25 September 2012 - 19:01

Hi - thanks for the info on how to update the functions for osCommerce. I've updated my code but I'm having an issue when it comes to logging in as an admin to the admin area.

When I attempt to login, it is bouncing me back to the login page, as though it's not saving the login sessions correctly. I know the database query is working as I can put in an incorrect login and it will tell me it's incorrect. The thing I find strange is that I can go to the customer login page (login.php not via admin obviously) and login to the site fine, it's just logging in via the admin page (/admin/login.php) that I can't do.

Can you help?

#6   mthiner

mthiner
  • Members
  • 1 posts

Posted 02 October 2012 - 15:40

I have the same issues as Jo, does anyone have a solution?

#7   DunWeb

DunWeb

    The Censored One

  • Members
  • 13,084 posts

Posted 02 October 2012 - 15:57

@mthiner

Truncate the action_recorder, administrators and sessions tables. Then, clear your browser cookies and cache. Then, log back into the admin area. You will see there are NO administrators and you will be prompted to create a new administrator. Do that and then log into your admin area.



Chris
:|: Was this post helpful ? Click the LIKE THIS button :|:

See my Profile to learn more about add ons, templates, support plans and custom coding (click here)

#8   lbucar

lbucar
  • Members
  • 7 posts

Posted 21 December 2012 - 17:29

I have a problem with my shop. Costumers can't buy in market and they can't login with their account. Oscommerce is RC1 and problem has shown up after upgrading PHP 5.3.

I used the following tutorial authored by Mark Evans but I don't know how to change /catalog/includes/functions/sessions.php and /catalog/admin/includes/functions/sessions.php.
My sessions.php is

function tep_session_start() {
// >>> BEGIN REGISTER_GLOBALS
    $success = session_start();
    // Work-around to allow disabling of register_globals - map all defined
    // session variables
    if ($success && count($_SESSION))
    {
	  $session_keys = array_keys($_SESSION);
	  foreach($session_keys as $variable)
	  {
	    link_session_variable($variable, true);
	  }
    }
    return $success;
// <<< END REGISTER_GLOBALS
  }
  function tep_session_register($variable) {
// >>> BEGIN REGISTER_GLOBALS
// -skip-   return session_register($variable);
    // Work-around to allow disabling of register_globals - map session variable
    link_session_variable($variable, true);
    return true;
// <<< END SESSION_REGISTER
  }
  function tep_session_is_registered($variable) {
// >>> BEGIN REGISTER_GLOBALS
//    return session_is_registered($variable);
    return isset($_SESSION[$variable]);
// <<< END REGISTER_GLOBALS
  }
  function tep_session_unregister($variable) {
// >>> BEGIN REGISTER_GLOBALS
    // Work-around to allow disabling of register_globals - unmap session variable
    link_session_variable($variable, false);
    unset($_SESSION[$variable]);
//  return session_unregister($variable);
    return true;
// <<< END REGISTER_GLOBALS
  }
 
// >>> BEGIN REGISTER_GLOBALS
  // Work-around function to allow disabling of register_globals in php.ini
  // This is pretty crude but it works. What it does is map session variables to
  // a corresponding global variable.
  // In this way, the main application code can continue to use the existing
  // global varaible names but they are actually redirected to the real session
  // variables
  //
  // If the global variable is already set with a value at the time of the mapping
  // then it is copied over to the real session variable before being mapped back
  // again
  //
  // Parameters:
  // var_name - Name of session variable
  // map - true = map variable, false = unmap varaible
  //
  // Returns:
  // None
  function link_session_variable($var_name, $map)
  {
    if ($map)
    {
	  // Map global to session variable. If the global variable is already set to some value
	  // then its value overwrites the session variable. I **THINK** this is correct behaviour
	  if (isset($GLOBALS[$var_name]))
	  {
	    $_SESSION[$var_name] = $GLOBALS[$var_name];
	  }
	  $GLOBALS[$var_name] =& $_SESSION[$var_name];
    }
    else
    {
	  // Unmap global from session variable. Note that the global variable keeps the value of
	  // the session variable. This should be unnecessary but it reflects the same behaviour
	  // as having register_globals enabled, so in case the OSC code assumes this behaviour,
	  // it is reproduced here
	  $nothing;
	  $GLOBALS[$var_name] =& $nothing;
	  unset($GLOBALS[$var_name]);
	  $GLOBALS[$var_name] = $_SESSION[$var_name];
    }
  }
// <<< END REGISTER_GLOBALS
  function tep_session_id($sessid = '') {
    if ($sessid != '') {
	  return session_id($sessid);
    } else {
	  return session_id();
    }
  }
  function tep_session_name($name = '') {
    if ($name != '') {
	  return session_name($name);
    } else {
	  return session_name();
    }
  }
  function tep_session_close() {
// >>> BEGIN REGISTER_GLOBALS
    // Work-around to allow disabling of register_gloabls - unmap all defined
    // session variables
    if (count($_SESSION))
    {
	  $session_keys = array_keys($_SESSION);
	  foreach($session_keys as $variable)
	  {
	    link_session_variable($variable, false);
	  }
    }
// <<< END REGISTER_GLOBALS
    if (function_exists('session_close')) {
	  session_close();
    }
  }
  function tep_session_destroy() {
// >>> BEGIN REGISTER_GLOBALS
    // Work-around to allow disabling of register_gloabls - unmap all defined
    // session variables
    if (count($_SESSION))
    {
	  $session_keys = array_keys($_SESSION);
	  foreach($session_keys as $variable)
	  {
	    link_session_variable($variable, false);
	    unset($_SESSION[$variable]);
	  }
    }
// <<< END REGISTER_GLOBALS
    return session_destroy();
  }
  function tep_session_save_path($path = '') {
    if ($path != '') {
	  return session_save_path($path);
    } else {
	  return session_save_path();
    }
  }

This is an error when costumers tries to login

Deprecated: Function ereg_replace() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 57
Deprecated: Function ereg_replace() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 57
Warning: Cannot modify header information - headers already sent by (output started at /home/shoop/public_html/includes/functions/general.php:57) in /home/shoop/public_html/includes/functions/general.php on line 29

and this is an error when customer tries to buy

Deprecated: Function ereg() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 1044
Deprecated: Function ereg() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 1044
Deprecated: Function ereg() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 1044
Deprecated: Function ereg() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 1044
Deprecated: Function ereg() is deprecated in /home/shoop/public_html/includes/functions/general.php on line 1044
Warning: Cannot modify header information - headers already sent by (output started at /home/shoop/public_html/includes/functions/general.php:1044) in /home/shoop/public_html/includes/functions/general.php on line 29

Thank you in advance, best regarads.

#9   14steve14

14steve14
  • Members
  • 3,577 posts

Posted 22 December 2012 - 08:10

There is an updated addon about replacing code for 5.3 deprecated errors here http://addons.oscommerce.com/info/7394 There has also been several posts about updating code to 5.3 and a search using google will find many.

REMEMBER BACKUP, BACKUP AND BACKUP

Don't take life too seriously. no one gets out alive anyway.


#10   lbucar

lbucar
  • Members
  • 7 posts

Posted 22 December 2012 - 18:31

The same thing is suggested by @stickypod in first post, here. It didn't helped me resolving my problem.
I already searched throught google and this forum but I havent found solution.

But, thanks anyway.

#11   DunWeb

DunWeb

    The Censored One

  • Members
  • 13,084 posts

Posted 22 December 2012 - 19:38

@lbucar

I suggest updating to the latest version of osCommerce to correct the issue.





Chris
:|: Was this post helpful ? Click the LIKE THIS button :|:

See my Profile to learn more about add ons, templates, support plans and custom coding (click here)

#12   lbucar

lbucar
  • Members
  • 7 posts

Posted 23 December 2012 - 12:05

I know updating osCommerce would probably correct the issue, but I don't have knowledge and experience for that update. My shop has over one thousand registered users and I don't want to risk existing database.

I already solved some other problems without upgrading (language error, system's timezone), so I was hoping to find alternative for registration too. No luck so far.

My "old" shop /whistling.gif' class='bbc_emoticon' alt=':-' />( (www. gaz.hr) as you can see, everything is working except its basic function.

#13 ONLINE   Jack_mcs

Jack_mcs
  • Members
  • 26,685 posts

Posted 23 December 2012 - 14:17

You need to convert the two ereg functions mentioned in the errors with their equivalent preg functions. Examples are spread throughout the forums and on the web. Or someone would probably provide them here if you posted the code.

#14   lbucar

lbucar
  • Members
  • 7 posts

Posted 23 December 2012 - 15:40

I tried with preg function earlier, then I get no error message but problem is still the same.

In application_top.php I don use
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

#15 ONLINE   Jack_mcs

Jack_mcs
  • Members
  • 26,685 posts

Posted 23 December 2012 - 16:49

Stating it is still borken won't help. You need to post the code, as mentioned, or search the web.

#16 ONLINE   MrPhil

MrPhil
  • Members
  • 5,380 posts

Posted 23 December 2012 - 16:51

Show us the line of code you changed, before and after your change. You probably made a mistake there.

#17   lbucar

lbucar
  • Members
  • 7 posts

Posted 23 December 2012 - 21:13

Ok, I'll try to be more precise. Web shop worked fine on PHP 5.2. Until my hoster upgraded it on PHP 5.3.19.

I have done all changes as descibed in first post (stickypod) here including changes from Mark Evans tutorial.

The changes I made are in following files:

catalog/includes/functions/sessions.php
catalog/admin/includes/functions/sessions.php
catalog/admin/backup.php
catalog/admin/cache.php
catalog/admin/configuration.php
catalog/admin/includes/classes/language.php
catalog/admin/includes/classes/phplot.php
catalog/admin/includes/classes/sessions.php
catalog/admin/includes/functions/compatibility.php
catalog/admin/includes/functions/general.php
catalog/admin/includes/functions/html_graphs.php
catalog/admin/includes/functions/validations.php
catalog/admin/modules.php
catalog/admin/server_info.php
catalog/admin/whos_online.php
catalog/advanced_search_result.php
catalog/includes/application_top.php
catalog/includes/classes/cc_validation.php
catalog/includes/classes/http_client.php
catalog/includes/classes/language.php
catalog/includes/classes/sessions.php
catalog/includes/functions/compatibility.php
catalog/includes/functions/general.php
catalog/includes/functions/validations.php
catalog/index.php
catalog/includes/modules/shipping/usps.php


So there is too many lines of code to post. All lines of code I changed are the similar as in link here.
Lines coloured in red are before and green ones are after change.

Funny fact is that I can registrate as a new costumer but logging in with same account failes.(redirects to home page). When I try to fill my card, it pops the message "Your card is empty".

#18   DunWeb

DunWeb

    The Censored One

  • Members
  • 13,084 posts

Posted 23 December 2012 - 21:56

@lbucar


Sounds like a sessions problem. Did you make the necessary database changes ?




Chris
:|: Was this post helpful ? Click the LIKE THIS button :|:

See my Profile to learn more about add ons, templates, support plans and custom coding (click here)

#19   lbucar

lbucar
  • Members
  • 7 posts

Posted 23 December 2012 - 22:20

No, I haven't done any changes in database. I do not know much about this segment.
Can you specify what changes does that include.

#20 ONLINE   Jack_mcs

Jack_mcs
  • Members
  • 26,685 posts

Posted 24 December 2012 - 01:10

Ok, I'll try to be more precise. Web shop worked fine on PHP 5.2. Until my hoster upgraded it on PHP 5.3.19.

I have done all changes as descibed in first post (stickypod) here including changes from Mark Evans tutorial.

So there is too many lines of code to post. All lines of code I changed are the similar as in link here.
Lines coloured in red are before and green ones are after change.

Funny fact is that I can registrate as a new costumer but logging in with same account failes.(redirects to home page). When I try to fill my card, it pops the message "Your card is empty".

You're missing the point. When you see a failure, like the one you previously posted (general.php on line 57), it shows the file and the line number in that file where the error occurred. If you would post the code at that location, the problem will probably be fixed shortly after.