How to replace session_register () for PHP 5.4+

unexpectedly, I received the following error when logging into one of my sites:

Function call undefined session_register ()

After some research, I saw that session_register () is deprecated after PHP 5.4. I checked on the hosting control panel, and, of course, it says that the current version I'm running on is PHP 5.4.19. I suppose they just triggered an update, so this issue seemed to be due to blue.

I looked at a few stack overflow messages and looked at links to PHP documentation. From what I'm compiling, session_register () is deprecated and should be replaced instead of $ _SESSION.

The problem is that both of them are already displayed 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 was hoping just by deleting line 54 (the obsolete part), then it should work, however it is not. When I did this, he broke the code.

Based on other posts, I read HERE and HERE , they seem to say that the code I see on line 55 above does the trick by itself. So I'm a little confused why this didn't work. If someone tells me which code should I use, I would really appreciate it. The script developer does not actually offer support.

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

+6
source share
4 answers

Perhaps he lacks calling session_start , session_register does this automatically, so you just need to make sure you call session_start before using $ _ SESSION globally.

Here you can find out more http://www.php.net/manual/en/function.session-register.php

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

+6
source

Since session_register('ADMIN') registers a global variable with this name in the current session, so there may be a $ ADMIN variable in your code, find them and replace them with:

 $_SESSION['ADMIN'] 

Hope this helps you.

+2
source

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

But note that the source code I posted runs

 <? 

instead

 <?php 

Doh

+1
source

Add session_start() at the beginning of your code and $_SESSION to manipulate or create it.

0
source

Source: https://habr.com/ru/post/954021/


All Articles