在一些浏览器的cookie未定义指数(undefined index for cookie in s

2019-10-17 00:27发布

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

The login page processes like this:

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

        }

    });

}

And submits to login-user.php, here:

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

}
?>

I then print from that cookie onto the main screen

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

But I getting the error

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

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?

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

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.

Thanks for any help

Answer 1:

当您使用setcookie()函数来创建一个cookie,这将只提供另一页上。 因此,它可以与你的AJAX的问题。



文章来源: undefined index for cookie in some browsers