Php deprecated function question

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
LVL 1
nachtmskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nachtmskAuthor Commented:
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
Chris StanyonWebDevCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Dave BaldwinFixer of ProblemsCommented:
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
nachtmskAuthor Commented:
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
Chris StanyonWebDevCommented:
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
nachtmskAuthor Commented:
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
Chris StanyonWebDevCommented:
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
Ray PaseurCommented:
0
Ray PaseurCommented:
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
nachtmskAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.