Latest News: (loading..)

Archived

This topic is now archived and is closed to further replies.

stickypod

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

25 posts in this topic

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.

Landis and hiorti like this

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

@@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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@@lbucar

 

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

 

 

 

 

 

Chris

lbucar likes this

Share this post


Link to post
Share on other sites

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 :-( (www. gaz.hr) as you can see, everything is working except its basic function.

Share this post


Link to post
Share on other sites

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.

lbucar likes this

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites

@@lbucar

 

 

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

 

 

 

 

Chris

lbucar likes this

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

If you picked the low hanging fruit (ereg, etc.) but did nothing about the changes to session functions, or changed them incorrectly, that could be tripping you up. Did you follow the osC PHP 5.3 patch to also fix session handling?

Share this post


Link to post
Share on other sites

Post the code from the two lines that are giving the errors

 

/home/shoop/public_html/includes/functions/general.php on line 57

 

and

 

/home/shoop/public_html/includes/functions/general.php on line 1044

 

Someone will then help with the correct code to remove the errors.

 

If you have followed the 5.3 upgrade path you should have altered this file, and may not have done so correctly as there are many changes to be made to this file.

Share this post


Link to post
Share on other sites

I'm sorry I didn't make myself clear.

Four days ago, when I wrote my first post I haven't done all changes tutorial recommendes. Then I had an error which I describe at the bottom of post.

 

In meanwhile I completed tutorial to upgrade PHP including all necessary changes. And now I don't get any error message, but problem with login and buying are a still present. So the point is, I can't show you code lines that are giving the errors, because I dont get a notice.

 

I point out that in my /includes/application_top.php and admin/includes/application_top.php file in line 17 I use code

 

// set the level of error reporting
error_reporting(E_ALL & ~E_NOTICE);

 

So I let aplication to notify me for occured errors.

 

If you want you can try to test my web and registrate as a new user (www . gaz.hr), and you will get an email for successfully registration. And then when you try to login as the same user, it failes, and it redirectes you to home page. With no error message.

 

And when you try to fill your cart it gives you not logical notice that your cart is empty.

 

Yestarday I started with another upgrade of osC from 2.2 Milestone 2 to 2.2 RC2a . So far I completed first half of changes (1-40) but problem is still present.

Share this post


Link to post
Share on other sites

@@lbucar

 

There are errors with the sessions as they change on each request and not stay the same as it should on same user.

Just keep an eye on osCsid in your url.

lbucar likes this

Share this post


Link to post
Share on other sites