PHP session losing data between pages
I am trying to set up a login system for my website that requires someone to be logged in in order to post. I have set it up with sessions and it works great on my localhost but not on the server. I set up print_r(session) on some of the pages to see where the data loss is. On the page checklogin.php, which is the page that gets run when someone logs in, it works fine. Here is some code from there:
<?php
session_name('login_session');
session_start();
mysql_connect("localhost"开发者_C百科,"root","root") or die(mysql_error());
mysql_select_db("date_ideas") or die(mysql_error());
$uname=$_POST['uname'];
$uname=mysql_real_escape_string($uname);
$pass=$_POST['pass'];
$pass=mysql_real_escape_string($pass);
$pass=md5($pass);
$query="SELECT * FROM users WHERE uname='$uname' AND pass='$pass'";
$result=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($result);
echo $numrows;
if ($numrows==1)
{
$_SESSION['login']="1";
$_SESSION['uname']=$uname;
echo "<script type='text/javascript'>alert('match');</script>";
print_r($_SESSION);
}
else
{
$_SESSION['login']="";
echo "<script type='text/javascript'>alert('Invalid Login');</script>";
echo "<script type='text/javascript'>window.location = 'login.php?uname=$uname'</script>";
}
?>
When I submit the form with good login information, the alert pops up and the print returns:
Array ( [login] => 1 [uname] => FrizbeeFanatic14 )
So at that point it's working. However, when I go to the main page, the print becomes:
Array ( )
So it has lost the session information. Here is the code from the main page:
<?php
session_name('login_session');
session_start();
?>
<html>
<head>
<title>Great Date Ideas</title>
<link rel="stylesheet" type="text/css" href="mainstyle.css" />
<script type="text/javascript" src="validate.js"></script>
<script type="text/javascript" src="cookiecheck.js"></script>
</head>
<body onload="checkCookie()">
<script type="text/javascript">
if (detect=false)
{
document.write("You must enable cookies to view this page.");
window.stop();
document.execCommand('Stop');
}
</script>
<div id="home"><?php require("header.php");?></div>
<?php
print_r($_SESSION);
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("date_ideas") or die(mysql_error());
$result=mysql_query("SELECT * FROM ideas ORDER BY post_date DESC") or die(mysql_error());
It goes on, but what follows works fine.
So all this code works just fine on my localhost, but on the server I get this error.
The only time I´ve had a similar problem, was when I was accidentally changing the domain while going to another page (from www.mydomain.com
tomydomain.com
and the other way around).
You can have your session persist between different sub-domains using session_set_cookie_params()
:
session_name('login_session');
session_set_cookie_params(0, '/', '.mydomain.com');
session_start();
Edit: Perhaps this can help you: A very interesting article: PHP Session Debugging
I found the problem - the save directory for the PHP sessions was a root apache directory, and I'm runnin nginx fastcgi. I just changed the root permissions for the folder and it works. Thanks a ton for all of your help.
精彩评论