Solved

Php deprecated function question

Posted on 2015-02-23
11
243 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 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40626157
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
ID: 40626172
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 43

Expert Comment

by:Chris Stanyon
ID: 40626207
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40626249
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
ID: 40626260
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
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40626265
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
ID: 40626283
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 43

Expert Comment

by:Chris Stanyon
ID: 40626410
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 109

Expert Comment

by:Ray Paseur
ID: 40627029
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40627033
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
ID: 40637092
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 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