我有一个 AJAX 登录系统 - 所有表单数据都使用 PHP 进行验证,如果用户名和密码匹配,它会设置某些 cookie。
我知道需要在其他任何东西之前设置 cookie,但是我使用的模型怎么可能呢?我想保持整个登录不刷新。
我收到错误消息:“无法修改标头信息 - 标头已由.....发送”
有人有建议吗?
谢谢
编辑:使用 IF 语句验证表单,然后如果满足条件,则在其中设置 cookie
代码:全部
<?php
session_start();
include "connect.php";
if ($_POST) {
$loginuser = mysql_escape_string($_POST['loginuser']);
$loginpass = mysql_escape_string($_POST['loginpass']);
$rememberme = mysql_escape_string($_POST['rememberme']);
if ($rememberme = 1) {
$time = time()+60*60*24*365;
} else {
$time = time()+3600;
}
$salt = changedsalt";
$hashed_password = sha1($loginpass).sha1($salt);
$getUser_sql = "SELECT * FROM acts WHERE username = '$loginuser' AND hashed_password = '$hashed_password'";
$getUser = mysql_query($getUser_sql);
$getUser_RecordCount = mysql_num_rows($getUser);
if($getUser_RecordCount == 1) {
$rowLogin = mysql_fetch_array($getUser);
$user_id = $rowLogin['id'];
$emailad = $rowLogin['email'];
$activated = $rowLogin['activated'];
if ($activated == 1) {
$hash = "changedthistoo";
$randstring = rand(5, 10);
$code = sha1($hash . $randstring);
mysql_query("UPDATE `acts` SET `key` = '$code' WHERE `id` = '$user_id' AND `email` = '$emailad'");
setcookie('AHkey', $code, $time, '/');
setcookie('AHid', $user_id, $time, '/');
setcookie('AHem', $emailad, $time, '/');
setcookie('AHty', 'acts', $time, '/');
setcookie('AHtr', 1, time()+3600, '/');
$data['success'] = true;
$data['message'] = "Act Login - Activated";
$data['message'] = $code;
echo json_encode($data);
exit;
} else {
$data['success'] = false;
$data['message'] = "Act Login - Not Activated";
echo json_encode($data);
exit;
}
} elseif ($getUser_RecordCount == 0) {
$getUser_sqlp = "SELECT * FROM promoter WHERE username = '$loginuser' AND hashed_password = '$hashed_password'";
$getUserp = mysql_query($getUser_sqlp);
$getUser_RecordCountp = mysql_num_rows($getUserp);
$rowLogin = mysql_fetch_array($getUserp);
$user_id = $rowLogin['id'];
$emailad = $rowLogin['email'];
$activated = $rowLogin['activated'];
if($getUser_RecordCountp == 1) {
if ($activated == 1) {
//generate random string
$hash = "49ebva09afbh";
$randstring = rand(5, 10);
$code = sha1($hash . $randstring);
mysql_query("UPDATE `promter` SET `key` = '$code' WHERE `id` = '$user_id' AND `email` = '$emailad'");
setcookie('AHkey', $code, $time, '/');
setcookie('AHid', $user_id, $time, '/');
setcookie('AHem', $emailad, $time, '/');
setcookie('AHty', 'promoter', $time, '/');
setcookie('AHtr', 1, time()+3600, '/');
$data['success'] = true;
$data['message'] = "Act Login - Activated";
$data['message'] = $code;
echo json_encode($data);
exit;
} else {
$data['success'] = false;
$data['message'] = "Promoter Login - Not Activated";
echo json_encode($data);
exit;
}
} else {
$data['success'] = false;
$data['message'] = "Wrong Username or Password";
}
}
echo json_encode($data);
}
?>