PHP会话丢失页面之间的数据 [英] PHP session losing data between pages

查看:55
本文介绍了PHP会话丢失页面之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站设置一个登录系统,该系统要求有人登录才能发布.我已经用会话设置了它,它在我的本地主机上很好用,但是在服务器上却没有.我在某些页面上设置了print_r(session)来查看数据丢失的位置.在页面checklogin.php(有人登录时运行该页面)上,它可以正常工作.这里是一些代码:

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)

Array ( [login] => 1 [uname] => FrizbeeFanatic14 )

因此,它正在工作.但是,当我转到主页时,打印内容变为:

So at that point it's working. However, when I go to the main page, the print becomes:

数组()

因此它丢失了会话信息.这是主页上的代码:

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.

推荐答案

我发现了问题-PHP会话的保存目录是Apache根目录,而我正在运行nginx fastcgi.我只是更改了该文件夹的root权限,并且它可以工作.非常感谢您的所有帮助.

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.

这篇关于PHP会话丢失页面之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆