我使用的是登錄腳本,我在專家的幫助下發現在用戶登錄時創建cookie。在某些瀏覽器中未定義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中的另一臺電腦上運行。
感謝所有幫助
SQL注射掉右舷船長,船長! – Pointy 2012-07-16 14:53:11
這是我遇到的問題的原因,還是隻是,我不逃避東西呢? – 1252748 2012-07-16 14:55:55
不,這只是一個潛在的問題(這就是爲什麼它只是一個評論,而不是一個答案:-)我不知道爲什麼cookie不起作用。 – Pointy 2012-07-16 14:57:15