Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace session_register() for PHP 5.4+

Tags:

php

session

out of the blue I was getting the following error when logging in to one of my sites:

Call to undefined function session_register()

After some research I saw that session_register() is deprecated after PHP 5.4. I checked in my hosting control panel, and sure enough it says current version I'm running is PHP 5.4.19. I assume they just forced an upgrade which is why this problem seemed to occur out of the blue.

I looked through several Stack Overflow posts and reviewed links to PHP documentation. From what I gather, session_register() is deprecated and should be replaced with $_SESSION instead.

Problem is, both of those already appear in my code. Here is the code in my index.php file:

  53 <? if ($username) {
  54    session_register("ADMIN");
  55    $_SESSION['ADMIN'] = $username;
  56 ?>

So I hoped just by removing line 54 (the deprecated piece) then it should work, however that is not the case. It broke the code when I did that.

Based on other posts I read HERE and HERE they seem to be saying that the code I see in line 55 above should by itself do the trick. So I'm a bit confused why it didn't work. If anyone can tell me what code I should use I would sincerely appreciate it. The developer of the script is not really offering support.

NOTE: Also, yes there is session_start() called at the very top of the page.

like image 307
Michael Curving Avatar asked Sep 17 '13 20:09

Michael Curving


3 Answers

Because session_register('ADMIN') registers the global variable with that name in the current session, so maybe in your code you have $ADMIN variable, find them and replace them with:

$_SESSION['ADMIN']

I hope this will help you.

like image 28
01e Avatar answered Oct 10 '22 02:10

01e


Possible it is missing the call for session_start, session_register make this automatically, so what you have to do is just make sure that you have called session_start before use $_SESSION global.

You can check more here http://www.php.net/manual/en/function.session-register.php

If session_start() was not called before this function is called, an implicit call to session_start() with no parameters will be made. $_SESSION does not mimic this behavior and requires session_start() before use.

like image 52
krolow Avatar answered Oct 10 '22 03:10

krolow


Looks like I overlooked the obvious. I was on the right track by simply removing the old session_register code.

But notice the original code I posted starts

<?

instead of

<?php

doh

like image 28
Michael Curving Avatar answered Sep 25 '22 05:09

Michael Curving