Rumah > Artikel > pembangunan bahagian belakang > PHP会员登录代码,验证通过,跳转页面后没法获取Cookie的值
PHP会员登录代码,验证通过,跳转页面后无法获取Cookie的值。
跟踪代码,用户登录验证通过,然后跳转到首页,在首页又有段代码是验证用户是否已登录(Cookie中是否有该用户名),结果Cookie一直为空,在本地测试是正常的。服务器是IIS,本地测试的服务器是Apache.
相关代码如下:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?php session_start( ); require_once( "getRootDir.php" ); require_once( "../inc/config.php" ); $action = get_param( "action" ); $username = get_param( "username" ); $password = md5( get_param( "password" ) ); if ( $action == "login" ) { $IMGVER_RandomText = $_SESSION['IMGVER_RndText']; $otherpwd = get_param( "otherpwd" ); if ( $IMGVER_RandomText == $otherpwd ) { $query = "select * from house_admin "; $result = mysql_query( $query ); $row = mysql_fetch_array( $result ); if ( $row ) { setcookie( "house_havelogin", "true", time( ) + 36000 ); setcookie( "house_username", $row['house_username'], time( ) + 36000 ); header( "Location: index.php" ); echo "<script language='javascript'>alert('登陆成功!');window.location.href='index.php';"; } else { echo "<script language="javascript">alert('请正确输入用户名和密码!');window.location.href='login1.php';</script>"; exit(); } } else { echo "<script language="javascript">alert('请正确填写验证码!');window.location.href='login1.php';</script>"; exit(); } } ?>……略
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?php require_once( "checklogin.php" ); ?> ……略
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?php if ( $_COOKIE['house_username'] == "" || empty( $_COOKIE['house_username'] ) ) { /*这里一直会跑进来,也就是上面的cookie获取失败*/ header( "Location: login1.php" ); } ?>
setcookie( "house_havelogin", "true", time( ) + 36000 ); setcookie( "house_username", $row['house_username'], time( ) + 36000 ); print_r($_COOKIE); //header( "Location: index.php" ); echo "<script language="javascript">alert('登陆成功!');window.location.href='index.php';</script>"; <br><font color="#e78608">------解决方案--------------------</font><br>上面的if先不要判断 直接 print_f($_COOKIE); 看下有什么结果 或用firecookie插件看下cookie有没有值 <br><font color="#e78608">------解决方案--------------------</font><br> 1 注释掉 header( "Location: index.php" ); <br>2 在header( "Location: index.php" );下面添加 print_r($row);确认是否有用户信息存在<br>3 在index.php里面添加 echo $_COOKIE['house_username'] ;确认在COOKIE是否有数值<br><br>php.ini 内需要设置<br>output_buffering = on<br><br>程序自身还存在问题,如果登录正常了,那么任何人随便写用户名密码都能登录<br>建议在将查询的SQL加上用户名和密码2个字段进行查询 <div class="clear"> </div>