Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session losing data between pages

Tags:

php

session

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","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.

like image 484
FrizbeeFanatic14 Avatar asked Mar 25 '11 21:03

FrizbeeFanatic14


People also ask

Do I need session_start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

How are session variables destroyed in PHP?

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How does PHP keep track of sessions?

The session functions keep track of users by issuing them cookies with a randomly generated session IDs. If PHP detects that a user doesn't accept the session ID cookie, it automatically adds the session ID to URLs and forms.

What is PHP session_start () function?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.


2 Answers

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

like image 84
jeroen Avatar answered Sep 25 '22 02:09

jeroen


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.

like image 20
FrizbeeFanatic14 Avatar answered Sep 23 '22 02:09

FrizbeeFanatic14