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:
- 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$_SESSION
array as$_SESSION
entries are automatically registered. If your script usessession_register()
, it will not work in environments where the PHP directiveregister_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 theglobal
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 usesession_register()
,session_is_registered()
, andsession_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
How to Fix Error: Laravel.Log Could Not Be Opened
Is There Way to Use Two PHP Versions in Xampp
Performance of For VS Foreach in PHP
How to Remove "Public/Index.PHP" in the Url Generated Laravel
Json_Encode PHP Array as Json Array Not Json Object
PHP Datetime::Modify Adding and Subtracting Months
Html Element Array, Name="Something[]" or Name="Something"
How to Delete a Line from the File With PHP
PHP Constants Containing Arrays
What Is the Best Method to Merge Two PHP Objects
Post an Array from an HTML Form Without JavaScript
Loading .SQL Files from Within PHP
How to Gracefully Handle Files That Exceed PHP'S 'Post_Max_Size'