日期:2014-05-17  浏览次数:20543 次

PHP会员登录代码,验证通过,跳转页面后无法获取Cookie的值。
跟踪代码,用户登录验证通过,然后跳转到首页,在首页又有段代码是验证用户是否已登录(Cookie中是否有该用户名),结果Cookie一直为空,在本地测试是正常的。服务器是IIS,本地测试的服务器是Apache.
相关代码如下:

PHP code
<?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';</script>";
        }
        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();
    }
}
?><HTML>……略


index.php页代码:
PHP code
<?php
require_once( "checklogin.php" );
?>
<html>……略


这个是checklogin.php页的代码:
PHP code
<?php

if ( $_COOKIE['house_username'] == "" || empty( $_COOKIE['house_username'] ) )
{
    /*这里一直会跑进来,也就是上面的cookie获取失败*/
    header( "Location: login1.php" );
}
?>




网站没有跨域问题,都是在同一个网站下。


------解决方案--------------------
PHP code

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

------解决方案--------------------
上面的if先不要判断 直接 print_f($_COOKIE); 看下有什么结果 或用firecookie插件看下cookie有没有值
------解决方案--------------------
1 注释掉 header( "Location: index.php" ); 
2 在header( "Location: index.php" );下面添加 print_r($row);确认是否有用户信息存在
3 在index.php里面添加 echo $_COOKIE['house_username'] ;确认在COOKIE是否有数值

php.ini 内需要设置
output_buffering = on

程序自身还存在问题,如果登录正常了,那么任何人随便写用户名密码都能登录
建议在将查询的SQL加上用户名和密码2个字段进行查询