How to Fix the Session_Register() Deprecated Issue

How to fix the session_register() deprecated issue?

Don't use it. The description says:

Register one or more global variables with the current session.

Two things that came to my mind:

  1. Using global variables is not good anyway, find a way to avoid them.
  2. 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 $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

This is pretty important, because the register_globals directive is set to False by default!

Further:

This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword 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 use session_register(), session_is_registered(), and session_unregister().

Error: Deprecated: Function Session_register

The line it is complaining about will contain a line like this:

session_register("foo");

replace that with:

$_SESSION['foo'] = $foo;

replacing 'foo' and $foo with whatever name is being registered.

Replacing session_register() in PHP

Don't use deprecated session_is_registered() function, try below:-

<? session_start($PHPSESSID);
include_once "includes/config.php";

if (!isset($_SESSION["sess_lenguaje"])) {
$_SESSION["sess_lenguaje"] = "es";
}
include_once 'lenguaje/'.$_SESSION["sess_lenguaje"].'.php';

?>

porting deprecated session_register to php 7

The only thing what you need to do is issue an session_start() to start a session and you can use $_SESSION['anyindex'] to beable to use the session data.

session_register - Deprecated

Just remove the session_register lines. As long as you've called session_start() you won't have problems.

session_start();
...
...
$id = $row["id"];
$_SESSION['id'] = $id;

// Get member username into a session variable
$firstname = $row["firstname"];
$_SESSION['firstname'] = $firstname;

Deprecated: Function session_register() is deprecated and Cannot modify header information

Instead of doing:

session_register("myusername");
session_register("mypassword");

You can simply do:

session_start();
$_SESSION['username'] = 'something';
$_SESSION['password'] = 'something';

And to check whether the username is set you can do:

session_start();
if(!isset($_SESSION['username'])){
// not logged in
}

Note that I have the session_start() function right above my checks / initialization. In your code you may want to add it at the top of your script to prevent the "Headers already sent by PHP" message.

Also, please don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is a good PDO tutorial.

One last thing regarding your code. It looks like you do not properly hash the passwords, which is considered bad practice. If an attacker gets hold of your database you have some explaining to do to the people who are in the database (e.g. you have to tell them the attacker got all their passwords).

What's wrong with session_register()?

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

try like this:

session_start();
$_SESSION['username'] = $username;

then (in other page):

session_start();
echo $_SESSION['username'];


Related Topics



Leave a reply



Submit