Solved

Php deprecated function question

Posted on 2015-02-23
11
232 Views
Last Modified: 2015-02-28
Hi,
I'm trying to move a site from one server to another. The server I had the site on was running php 5.3.3. The new server has php 5.5.9

I'm going through the site and watching the error log.
I came across this error (and it also caused the site not to work on that page)
PHP Fatal error:  Call to undefined function session_is_registered()

I checked some forums and found it to be a removed function in 5.5.
The forum suggested I replace it with $_SESSION
I'm a programmer but not a PHP coder. I want to make sure I did the replace correctly.
Here is the original code:
function CCSetSession($param_name, $param_value)
{
    global $HTTP_SESSION_VARS;
    global ${$param_name};
    if(session_is_registered($param_name))
        session_unregister($param_name);
    ${$param_name} = $param_value;
    session_register($param_name);
    $HTTP_SESSION_VARS[$param_name] = $param_value;
}
//End CCSetSession




Here is the code I  modified:
function CCSetSession($param_name, $param_value)
     {
         global $HTTP_SESSION_VARS;
         global ${$param_name};
         if($_SESSION[$param_name] = $param_name)
             $_SESSION[$param_name] = $param_name;
         ${$param_name} = $param_value;
         $_SESSION[$param_name];
         $HTTP_SESSION_VARS[$param_name] = $param_value;
     }
     //End CCSetSession

The page that was having a problem seems to be working now, but I don't know if something I might have done incorrectly here will come back and get me later. Please let me know if my mods look correct and if not, how I can fix them.
Thanks!
Nacht
0
Comment
Question by:nachtmsk
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
If you need to check the variable, then you would check if it's set by using the isset() function. Also note that a single = is for assignment. For comparison you need the double ==

Part of your code doesn't really make sense. You seem to checking if a session variable is set and if it is, you unset, only to re-set it. You also seem to be setting it to the name, not the value.

$HTTP_SESSION_VARS is also deprecated.

$_SESSION is a superglobal, so there no need to GLOBAL it.

If you're simply looking to set a session variable, then all you need is:

function CCSetSession($param_name, $param_value)
{
    $_SESSION[$param_name] = $param_value;
}

Open in new window

0
 

Author Comment

by:nachtmsk
Comment Utility
Thanks,
It's not  my code. I'm taking over this site/code.
It was written a while ago -- probably about 10 years and I think some or most of it was generated by some framework.
So your saying the Original code (not my modified version) didn't make sense?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Sorry. Slightly mixed up my feedback. Technically, the original code made sense, in that it would work, but I'm not sure why you would check if a variable is set, then unset it, only to re-set it. Seems a little odd.

Your modified code doesn't really make sense. Here it is commented:

// access the global session variables - DEPRECATED
global $HTTP_SESSION_VARS;
// access a global variable - not really needed as $_SESSION is now global
global ${$param_name};
// this assigns the value of param_name to a session variable - will always return TRUE
if($_SESSION[$param_name] = $param_name)
// this always get called, because the IF statement above is TRUE.
// it does exactly the same as above - assigns the value of $param_name to a session variable of the same name
$_SESSION[$param_name] = $param_name;
// assign param_value to the GLOBAL variable $param_name - unnecessary (and potentially dangerous)
${$param_name} = $param_value;
// no idea what this does but it's wrong.
$_SESSION[$param_name];
// assign the $param_value to a session variable called $param_name - DEPRECATED
$HTTP_SESSION_VARS[$param_name] = $param_value;

Open in new window


If all you're doing is assigning a value to a Session variable, then all you need is the code I posted previously. You will need to make sure that session_start() is called before you can work with sessions.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Here's the doc page for $_SESSION and $HTTP_SESSION_VARS: http://php.net/manual/en/reserved.variables.session.php   That must be pretty old code since the change was made with PHP 4.1.
0
 

Author Comment

by:nachtmsk
Comment Utility
Thanks,
Yes, its really old code -- about 10 years ago.
I'm noticing other problems as well, in particular connecting to mysql.

I'm wondering if it's possible to install a slightly older version of php (5.3 as opposed to 5.5) on my ubuntu server.
This site only needs to be up for a short time and all the work that it looks like I will need to do to bring it up to php 5.5 is overwhelming.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
With each new release of PHP there are likely to be functions that become deprecated. In PHP 5.5, all of the mysql_* functions have become deprecated and you should be switching to PDO or mySQLi.

You could switch back to a previous version, but there will still be some deprecated (or even obsolete) functions as your code is really quite old.

While you may have the option of dropping back in version on your own system, if the site ever goes live on hosting that's outside of your control it could simply break overnight if the sysadmins upgrade. Always a better option to bring code up to date, rather than downgrade the system.
0
 

Author Comment

by:nachtmsk
Comment Utility
Thanks Chris,

Yeah, I am in control of the server completely. It's  Cloud server at rackspace.
I'm going to look into down grading. I don't have the time now to patch all that code. Not optimal, but only solution right now. The mysql code might not be the only thing that is broken. Each time I fix one thing, I will see something else that doesn't work.  
So the question is do I downgrade PHP or try to find and OS version that has the Php version I need.
I need to check with rackspace and see what versions of php exist with their OS choices.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Probably the best solution here is to install an additional version of PHP to your current OS. You can then set your site to use the older version so that it works, and as and when you have time you can patch it, and switch to the newer version easily. If it doesn't work, you just switch back.

You may find it's already setup to support multiple versions - many hosting accounts are - they'll keep the old version when they add a new one, just in case something breaks!

If your server won't support that, then an install of an older version of PHP is likely to be easier than a whole new OS.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
As to the MySQL problems, well, it's a similar situation faced by many of us.  MySQL is going away and PHP has given us years of fair notice.  This article explains the issues and shows what needs to be done to keep your scripts running safely.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:nachtmsk
Comment Utility
Just to update the thread -- I decided to start over from scratch with an earlier release of Ubuntu that  had an earlier release of Php and apache. Maybe not the best long term solution, but I had no time to fix all the problems the updated pHp version caused.
Thanks for all of your suggestions. I will come back to this question and your solutions when I have to upgrade php and have  no other choice.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now