在某些浏览器中未定义的cookie索引 [英] undefined index for cookie in some browsers

查看:94
本文介绍了在某些浏览器中未定义的cookie索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在专家交流中发现的登录脚本来在用户登录时制作cookie。

I am using a log in script that I found on experts exchange to make a cookie when a user logs on.

登录页面的过程如下:

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

        }

    });

}

并提交给登录用户.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打印到主屏幕上

I then print from that cookie onto the main screen

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

但是我遇到了错误

通知:未定义的索引:/home/iscotest/public_html/webtrack/orders-home.php中的customer_name

实际网站是www.isco.net。但是该网站托管在iscotest.com。 isco.net只是指向iscotest.com。这可能就是为什么未设置我的Cookie的原因吗?

The actual site is www.isco.net. But the website is hosted at iscotest.com. isco.net simply points to iscotest.com. Could this be why my cookie isn't being set?

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

It is quite a problem because this totally ceases the load of the page, as that cookie information is used to retrieve the data that is displayed

另一个奇怪的是,该错误没有始终如一地出现。我在一台计算机上出现了关于Safari和Chrome的错误,但是该站点在使用Safari和chrome的另一台计算机上正常运行。

The other odd thing is that this error isn't appearing consistently. I get the error on safari and chrome on one computer, but the site functions normally on another computer in safari and chrome.

感谢您的帮助

推荐答案

使用setcookie()时要创建COOKIE,此功能仅在另一页上可用。因此,您的AJAX可能会出现问题。

When you use setcookie () to create a COOKIE this will only be available on another page. Therefore, it can be a problem with your AJAX.

这篇关于在某些浏览器中未定义的cookie索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆