PHP Session Variables Not Sticking
Here is the simplified version of my code
<?php session_start();
if($_GET['page'] == "login")
{ // process username/password
if(login is successful)
$_SESSION['access'] = "dev";
}
else if($_GET['page'] == "request")
{
//get some data from an xml file using simplexml
}
?>
<?php if(!isset($_SESSION['access']) { ?>
<!-- display login form. submits to this page + ?page=login -->
<?php } else { ?>
<!-- display edit form, nothing more than a textarea, a select and开发者_如何学Python a submit button. -->
<!-- The form points to this page + ?page=request -->
<?php } ?>
What happens is when I login $_SESSION['access'] is set correctly, but when I submit data again to the page to retrieve data the $_SESSION['access'] gets unset and the login form gets displayed again.
What am I doing wrong? I'm fairly new to PHP.
You need to explicitly store the data in the session. session_start opens the session for reading/writing, but what is written does not stick until you explicitly do it.
http://www.php.net/manual/en/function.session-write-close.php
Use that function.
精彩评论