How to fix the session_register() deprecated problem in PHP 5.3
7 Answers
Don't use it. The description says:
Register one or more global variables with the current session.
Two things that came to my mind:
- Using global variables is not good anyway, find a way to avoid them.
- You can still set variables with
$_SESSION['var'] = "value".
See also the warnings from the manual:
If you want your script to work regardless of
register_globals, you need to instead use the$_SESSIONarray as$_SESSIONentries are automatically registered. If your script usessession_register(), it will not work in environments where the PHP directiveregister_globalsis disabled.
This is pretty important, because the register_globals directive is set to False by default!
Further:
This registers a
globalvariable. If you want to register a session variable from within a function, you need to make sure to make it global using theglobalkeyword or the$GLOBALS[]array, or use the special session arrays as noted below.
and
If you are using
$_SESSION(or$HTTP_SESSION_VARS), do not usesession_register(),session_is_registered(), andsession_unregister().
- 795,719
- 175
- 1,089
- 1,143
Use $_SESSION directly to set variables. Like this:
$_SESSION['name'] = 'stack';
Instead of:
$name = 'stack';
session_register("name");
- 4,327
- 6
- 31
- 56
- 46,210
- 12
- 74
- 78
before PHP 5.3
session_register("name");
since PHP 5.3
$_SESSION['name'] = $name;
- 530
- 1
- 5
- 16
- 354
- 2
- 9
if you need a fallback function you could use this
function session_register($name){
global $$name;
$_SESSION[$name] = $$name;
$$name = &$_SESSION[$name];
}
- 14,365
- 19
- 99
- 170
To complement Felix Kling's answer, I was studying a codebase that used to have the following code:
if (is_array($start_vars)) {
foreach ($start_vars as $var) {
session_register($var);
}
} else if (!(empty($start_vars))) {
session_register($start_vars);
}
In order to not use session_register they made the following adjustments:
if (is_array($start_vars)) {
foreach ($start_vars as $var) {
$_SESSION[$var] = $GLOBALS[$var];
}
} else if (!(empty($start_vars))) {
$_SESSION[$start_vars] = $GLOBALS[$start_vars];
}
- 1
- 1
- 1,059
- 14
- 32
I wrote myself a little wrapper, so I don't have to rewrite all of my code from the past decades, which emulates register_globals and the missing session functions.
I've picked up some ideas from different sources and put some own stuff to get a replacement for missing register_globals and missing session functions, so I don't have to rewrite all of my code from the past decades. The code also works with multidimensional arrays and builds globals from a session.
To get the code to work use auto_prepend_file on php.ini to specify the file containing the code below. E.g.:
auto_prepend_file = /srv/www/php/.auto_prepend.php.inc
You should have runkit extension from PECL installed and the following entries on your php.ini:
extension_dir = <your extension dir>
extension = runkit.so
runkit.internal_override = On
.auto_prepend.php.inc:
<?php
//Fix for removed session functions
if (!function_exists('session_register'))
{
function session_register()
{
$register_vars = func_get_args();
foreach ($register_vars as $var_name)
{
$_SESSION[$var_name] = $GLOBALS[$var_name];
if (!ini_get('register_globals'))
{ $GLOBALS[$var_name] = &$_SESSION[$var_name]; }
}
}
function session_is_registered($var_name)
{ return isset($_SESSION[$var_name]); }
function session_unregister($var_name)
{ unset($_SESSION[$var_name]); }
}
//Fix for removed function register_globals
if (!isset($PXM_REG_GLOB))
{
$PXM_REG_GLOB=1;
if (!ini_get('register_globals'))
{
if (isset($_REQUEST)) { extract($_REQUEST); }
if (isset($_SERVER)) { extract($_SERVER); }
//$_SESSION globals must be registred with call of session_start()
// Best option - Catch session_start call - Runkit extension from PECL must be present
if (extension_loaded("runkit"))
{
if (!function_exists('session_start_default'))
{ runkit_function_rename("session_start", "session_start_default"); }
if (!function_exists('session_start'))
{
function session_start($options=null)
{
$return=session_start_default($options);
if (isset($_SESSION))
{
$var_names=array_keys($_SESSION);
foreach($var_names as $var_name)
{ $GLOBALS[$var_name]=&$_SESSION[$var_name]; }
}
return $return;
}
}
}
// Second best option - Will always extract $_SESSION if session cookie is present.
elseif ($_COOKIE["PHPSESSID"])
{
session_start();
if (isset($_SESSION))
{
$var_names=array_keys($_SESSION);
foreach($var_names as $var_name)
{ $GLOBALS[$var_name]=&$_SESSION[$var_name]; }
}
}
}
}
?>
- 1
- 2
We just have to use @ in front of the deprecated function. No need to change anything as mentioned in above posts. For example: if(!@session_is_registered("username")){ }. Just put @ and problem is solved.
- 8,408
- 6
- 48
- 68
- 17
- 1
-
3Just suppressing the errors is not a correct way to deal with deprecated functions. It's sometimes a quick hot-fix, but never a long-term solution (not even short-term!). Try to find out why it is marked as deprecated and what you can do against it! – Haudegen Aug 04 '15 at 14:32