PHP会话在页面之间丢失数据(PHP session losing data between pages)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
PHP会话在页面之间丢失数据(PHP session losing data between pages)

我正在尝试为我的网站建立一个登录系统,要求有人登录才能发布。 我使用会话进行设置,它在我的本地主机上运行良好,但不在服务器上运行。 我在一些页面上设置了print_r(会话)来查看数据丢失的位置。 在页面checklogin.php上,这是当有人登录时运行的页面,它工作正常。 这里有一些代码:

<?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>"; } ?>

当我提交具有良好登录信息的表单时,弹出警报并且打印返回:

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

所以在那个时候它正在工作。 但是,当我进入主页面时,打印成为:

Array()

所以它失去了会话信息。 以下是主页面中的代码:

<?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());

它继续,但接下来的工作很好。

所以所有这些代码在我的本地主机上工作得很好,但在服务器上,我得到这个错误。

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.

最满意答案

唯一一次我遇到类似问题的时候,是我在意外更改域名的同时转到其他页面(从www.mydomain.com到mydomain.com以及其他方式)。

您可以使用session_set_cookie_params()将会话保持在不同的子域之间:

session_name('login_session'); session_set_cookie_params(0, '/', '.mydomain.com'); session_start();

编辑:也许这可以帮助你:一个非常有趣的文章: PHP会话调试

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.

更多推荐

本文发布于:2023-04-12 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0ed702610206d59ae74e306b39004454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   数据   PHP   session   pages

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!