我正在使用我在专家交换中找到的登录脚本,以便在用户登录时制作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中的另一台计算机上正常运行。
感谢您的帮助
答案 0 :(得分:1)
使用setcookie()创建COOKIE时,只能在另一页上使用。因此,它可能是您的AJAX的问题。