I have a session:
session_start();
$_SESSION['auth'] = "true";
and the PHPSESSID cookie is set. However, when I refresh the page $_SESSION['auth'] returns NULL. Additionally, when I call session_destroy(); I receive the error Trying to destroy uninitialized session
How can I keep the session open?
Thanks!
Give this a try.. if this works I'd re examine your code + comment out session_destroy...
page1.php
<?php
session_start();
$_SESSION['auth'] = "true";
$_SESSION['superhero'] = "batman";
?>
Click here
page2.php
<?php
session_start(); // start the session before using it
echo $_SESSION['auth']; // will output 'true'
//print_r($_SESSION); // uncomment for testing
?>
Related
I cannot properly unset the session id of my page unless I close the browser and reopen it. I tried to set the $_SESSION = null; and to forcefully set the cookie to a negative value setcookie('cookiename', '', time()-3600); but no results yet.
<body>
<?php
session_start();
// Unset all of the session variables.
$_SESSION = null;
setcookie('cookiename', '', time()-3600);
session_destroy();
print "SESSION has been destroyed - all session data deleted";
?>
back to home page
</body>
replace lab5destroy.php with below code
<body>
<?php
ini_set('session.use_strict_mode', 1);
session_start();
// Unset all of the session variables.
session_regenerate_id();
session_destroy();
print "SESSION has been destroyed - all session data deleted";
?>
<hr>
back to home page
</hr>
</body>
session_start();
session_unset();
session_destroy();
The fix was session_name() instead of 'cookiename'
In PHP, when navigating from one page to another, the session is not logging.
suddenly the $_SESSION["username"]; that is sent from the login screen,
in :
session_start();
$_SESSION["username"]=$username;
echo $username;
echo $_SESSION["username"]; // THESE 2 ECHOes WORK
When in the index:
<?php
session_start();
echo $_SESSION["username"];
// THIS ECHO IS BLANK ALREADY TRIED IT WIWH OTHER ECHOES THE PAGE WORKS
?>
your calling session_start(); too many times.
Try a simple check if session is started
ex:
// start session if not session_id not present
if(session_id()=='') session_start();
You can also check if there is a session variable with:
if(isset($_SESSION['username']) && $_SESSION['username'] !== "") // this checks if its set and does not equal nothing
{
//do your code
}
else
{
//reset $_SESSION['username'];
}
Due to cross site session management, firstly I sent session id through url. Please note I had a $username variable created. First code is just a snap of a larger code.
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
$session_id = $username;
header("location: http://somedomain.com/receive.php?session_id=". $session_id );
Now I received it here and creating a new session I have forwarded the session variable in same site:
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_id($_GET['session_id']);
$some_var=session_id();
session_destroy();
session_start();
$_SESSION["var_name"] = $some_var;
//echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
header("location: anotherfile.php");
When I uncomment the echo line above and comment header line, then I can see the session array successfully. But when I pass it to anotherfile.php I loose the session.
session_start();
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
Any help why I am unable to fetch the session in last file.
change the second file
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_id($_GET['session_id']);
$some_var = session_id(); // remove session_destroy code because no session is set before.
session_start();
$_SESSION["var_name"] = $some_var;
header("location: anotherfile.php");
to this it will be fine
<?php
session_id($_GET['session_id']);
$some_var = session_id(); // remove session_destroy code because no session is set before.
session_start();
$_SESSION["var_name"] = $some_var;
header("location: anotherfile.php");
I have made a login and register system, which works flawlessly, and I am very proud of, but I cannot seem to get a logout function working.
My login system basically takes the database and scans it for rows that have both the username and password specified, and if it does, then it makes $_SESSION['loggedin']=1; and if it fails it makes it equal to 0.
Once the user is done, he/she clicks on a link that redirects to logout.php, and that is where the issues start. I have put session_start(); at the beginning of each page, but session_destroy, session_unset, and combinations of the two cannot seem to kill the session.
So I am wondering, is there a way that upon loading logout.php, it sets the $_SESSION['loggedin] to 0, and then redirects back to index.php(my homepage)? Which means it doesnt kill the session, but it would effectively log the user out. Any help is appreciated.
// Four steps to closing a session // (i.e. logging out)
// 1. Find the session
session_start();
// 2. Unset all the session variables
$_SESSION = array();
// 3. Destroy the session cookie
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// 4. Destroy the session
session_destroy();
if session_destroy doesn't work, use instead:
unset($_SESSION['put your session in here']);
// logout.php
session_start();
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == 1) {
$_SESSION['loggedin'] = 0;
header('Location: index.php');
}
It redirects the user to to index.php, if $_SESSION['loggedin'] equals to 1, and sets $_SESSION['loggedin'] to 0.
I suggest you to have 3 files
1) login.php
session_start();
/*if user $_POST username and password is correct then*/
$_SESSION['loggedin'] = 1;
?>
2)logout.php
<?php
session_start();
unset($_SESSION['loggedin']);
$_SESSION['loggedin'] = 0;
?>
3)checkLogin.php
<?php
session_start();
if ( isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == 0 )
{
echo "<script type='text/javascript'>alert('You need to login !')</script>";
echo '<meta http-equiv="Refresh" content="0;URL=index.php" />';
flush();
exit();
}
?>
with 3 files if you want to control some page that require login before access you just include(checkLogin.php);
e.g. index.php is not require login then not include(checkLogin.php);
but memberProfile.php is require login before then include(checkLogin.php);
I am trying to set a session variable but it's not working. Here is what I am doing in Code. Please suggest what's wrong:
Login-Validator.php
<?php
session_start();
$userName = "test";
$_SESSION['iUsername'] = $userName;
header("Location: http://www.XXXXXXXXXXXX.com/LoginSuccess.php");
?>
LoginSuccess.php
<?php
session_start();
$User = $_SESSION['iUsername'];
echo $User;
?>
Try this (put a 'exit' after the redirect)
session_start();
$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;
read more at # PHP: session isn't saving before header redirect
If this doesnt work..comment out the redirect and open each page in a different browser tab. Then open Login-Validator.php and then open LoginSuccess.php and check if the session was set. I think it cause by the cookie not setting before the redirect.
Also is Login-Validator.php and LoginSuccess.php on the same domain?
header("Location: /LoginSuccess.php");