Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Php deprecated function question

Posted on 2015-02-23
11
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 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
 
LVL 1

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 44

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

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
 
LVL 1

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 44

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
 
LVL 1

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 44

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 111

Expert Comment

by:Ray Paseur
ID: 40627029
0
 
LVL 111

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
 
LVL 1

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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.
Suggested Courses

609 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