Setting Cookies AJAX/PHP


Setting Cookies AJAX/PHP

我有一个AJAX登录系统-所有表单数据都使用PHP进行验证,如果用户名和密码匹配,它会设置某些cookie。

我知道cookie需要在其他任何东西之前设置,但是我使用的模型如何实现这一点。我想保持整个登录不刷新。

我得到错误:"无法修改报头信息-报头已由.....发送"

有人有什么建议吗?

感谢

编辑:使用IF语句验证表单如果满足条件cookie就会在里面设置

代码:all of it

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

请在使用header();重定向前使用ob_start();

在使用header()之前,不应该有任何输出到屏幕上。有关ob_start的更多使用,请访问http://php.net/manual/en/function.ob-start.php

您可以将session_start()保留在主索引页的顶部。然后使用Ajax将数据发送到该url或页面。