1

我正在使用我在专家交换中找到的登录脚本在用户登录时制作 cookie。

登录页面流程如下:

function process_login() {

    var username = $.trim($('#input_username').val());
    var password = $.trim($('#input_password').val());

    username = $.trim(username);
    password = $.trim(password);
    var remember = document.getElementById("remember_user_checkbox").checked;

    if (!username || !password) {

        return false;

    }

    remember == true ? remember = "true" : remember = "false";

    $.ajax({
        type: "POST",
        cache: false,
        url: "login_user.php",
        data: "username=" + username + "&password=" + password + "&remember=" + remember,
        dataType: "json",
        success: function (data) {

            if (data == "FALSE") {

                $('#input_password').val("");

                alert("The username or password you have entered is incorrect.");

                return false;
            }

            window.location = "orders-home.php?<?=time()?>";

        }

    });

}

并提交到login-user.php,这里:

<?php
include('login-config.php');
$username = pg_escape_string($_POST['username']);
$password = pg_escape_string($_POST['password']);

//no encryption for now


//php gets this as a string
$remember = $_POST['remember'];

if ( $remember == "true" )
{
    $remember = TRUE;   
}
else
{
    $remember = FALSE;  
}


$user_query = "SELECT * FROM users WHERE username = '$username' AND password = '$password' LIMIT 1";

$user_result = pg_query( $con , $user_query );

if ( !$user_result )
{
    echo json_encode("FALSE");  
}


$arr = array();

if (!$user_result)
{
die( pg_last_error($con) );
}
else
{ 

       while ( $row = pg_fetch_array($user_result) )
       {

          //put the customer id in a session so we can put it in a cookie later
          //then when the page is refreshed the stored customer id will be used
          //as their ksisoldby identifier
          if ( $row['cust_id'] )
          {
             $_SESSION['customer_id'] = $row['cust_id'];
             $_SESSION['customer_name'] = $row['first_name']." ".$row['last_name'];
             $_SESSION['uid'] = $row['id'];


             if ( $remember )
             {
                remember_user($row["id"]);
             }
          }

         $arr[] = array(

         "first_name"          =>$row['first_name'],
         "last_name"           =>$row['last_name'],
         "customer_id"         =>$row['cust_id'],
         "accepted_terms"      =>$row['accepted_terms'],
         );   
       }

}

if ( empty($arr) ){
echo json_encode('FALSE');
}
else
{

    $path = '/webtrack';
    $site = 'www.isco.net';


    if ($remember === TRUE)
    {
         $remember_time =  time()+60*60*24*30;

         setcookie('username', $username, $remember_time, $path, $site);
         setcookie('customer_id', $_SESSION['customer_id'], $remember_time, $path, $site);
         setcookie('customer_name', $_SESSION['customer_name'], $remember_time, $path, $site);
        // setcookie('uuk', $uuk, $remember_time, $path, $site);

    }
    else
    {
         setcookie('username', $username, false, $path, $site);
         setcookie('customer_id', $_SESSION['customer_id'], false, $path, $site);
         setcookie('customer_name', $_SESSION['customer_name'], false, $path, $site);

    }

echo json_encode($arr);

}
?>

然后我从那个 cookie 打印到主屏幕上

<div class="fl customer_id">
    <?= strtoupper($_COOKIE['customer_name']); ?>
</div>

但我得到了错误

Notice: Undefined index: customer_name in /home/iscotest/public_html/webtrack/orders-home.php

实际站点是 www.isco.net。但该网站托管在 iscotest.com。isco.net 只是指向 iscotest.com。这可能是我的 cookie 没有被设置的原因吗?

这是一个相当大的问题,因为这完全停止了页面的加载,因为该 cookie 信息用于检索显示的数据

另一个奇怪的事情是,这个错误并没有始终如一地出现。我在一台计算机上的 safari 和 chrome 上出现错误,但该站点在 safari 和 chrome 的另一台计算机上正常运行。

谢谢你的帮助

4

1 回答 1

1

当您使用 setcookie() 创建一个 COOKIE 时,它只会在另一个页面上可用。因此,这可能是您的 AJAX 的问题。

于 2012-07-16T15:02:45.737 回答