页面加载之间失去了与本地主机上WAMPserver 2.0 PHP会话数据 [英] PHP SESSION data lost between page loads with WAMPserver 2.0 on localhost
问题描述
我有一个PHP的认证系统上使用$ _SESSION变量我的网站。
一个表单提交的用户名和密码的文件的login.php。它是这样处理的:
<?PHP的包括../includes/sessionstart.inc.php'; ?>
<?PHP的ob_start(); ?>如果($ _ POST){
$ Q =的mysql_query(SELECT ID,公司从用户其中username ='.mysql_real_escape_string($ _ POST ['用户名'])。'和密码='.md5($ _ POST ['密码'])。' );
如果(mysql_num_rows($ Q)> = 1){
$ F = mysql_fetch_Array($ Q);
$ _SESSION ['公司'] = $ F ['公司'];
$ _SESSION ['身份证'] = $ F ['身份证'];
$ _SESSION ['logedin'] =真;
session_write_close(); ob_clean();
标题(地点:的index.php);}
之后,index.php文件被加载,并检查logedin是否是真实的。
<?PHP的包括../includes/sessionstart.inc.php'; ?>
?<如果PHP(!使用isset($ _ SESSION ['logedin']))标题(位置:login.php中'); ?>
在我的生产服务器,它继续进行,但在我的Wampserver,它将恢复为login.php中。我注意到,Wampserver在页面加载速度很慢,这可能需要用它做什么。这就是为什么我包括session_write_close,使切换页面之前,请确保会话数据被保存,但它并不能帮助。
session_start.inc.php的内容是简单的:
< PHP
在session_start();
?>
我以前有更多的code在那里,但目前它只是这个。之前,我开始使用一个包含文件的问题也存在。
没有任何人有一个想法,我做错了什么?为什么不Wampserver我的会话数据传输到下一个PHP文件?
在我终于修正了这个错误很长的时间。
在我的本地WAMP,会话数据不保存页面加载之间,因为会话数据存储在cookie中,有对本地主机设置没有cookie域。
解决方案:
session.cookie_domain'应该被设置为空字符串所有本地域名,不仅关于'本地主机'(但不应该是空的本地IP地址):
块引用>< PHP
的ini_set('。'!'session.cookie_domain',(strpos($ _ SERVER ['HTTP_HOST'])==假)$ _ SERVER ['HTTP_HOST']:'');
?>由于马尔钦Wiazowski 谁张贴在这里。
I have a PHP authentication system on my website using the $_SESSION variable.
A form submits a username and password to the file "login.php". It is handled like this:
<?php include '../includes/sessionstart.inc.php'; ?> <?php ob_start(); ?> if($_POST){ $q = mysql_query("SELECT id, company FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."' AND password = '".md5($_POST['password'])."'"); if(mysql_num_rows($q) >= 1){ $f = mysql_fetch_Array($q); $_SESSION['company'] = $f['company']; $_SESSION['id'] = $f['id']; $_SESSION['logedin'] = true; session_write_close(); ob_clean(); header("Location: index.php"); }
Afterwards, index.php is loaded and checks whether 'logedin' is true.
<?php include '../includes/sessionstart.inc.php'; ?> <?php if(!isset($_SESSION['logedin'])) header('Location: login.php'); ?>
On my production server, it continues, but on my Wampserver, it reverts back to login.php. I notice that Wampserver is very slow in page loading, this might have to do something with it. That's why I included the session_write_close, to make sure session data is saved before the pages are switched, but it doesn't help.
The contents of session_start.inc.php are simply:
<?php session_start(); ?>
I used to have more code in there, but at the moment it's just this. The problem also existed before I started using an include file.
Does anybody have an idea what I'm doing wrong? Why doesn't Wampserver transmit my SESSION data to the next PHP file?
解决方案After a long time I have fixed this bug finally.
On my localhost WAMP, the session data is not saved between page loads, because the session data is stored in a cookie, and there is no cookie domain to be set for localhost.
The solution:
'session.cookie_domain' should be set to empty string for all local domain names, not only for 'localhost' (but should not be empty for local IP addresses):
<?php ini_set('session.cookie_domain', (strpos($_SERVER['HTTP_HOST'],'.') !== false) ? $_SERVER['HTTP_HOST'] : ''); ?>
Thanks to Marcin Wiazowski who posted it here.
这篇关于页面加载之间失去了与本地主机上WAMPserver 2.0 PHP会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!