不设置cookie,而是运行其余的代码

时间:2012-07-01 14:03:33

标签: php mysql cookies

  

可能重复:
  Headers already sent by PHP
  Not setting cookies, but executing the rest of the code

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
include('global.php');
//if ($logged == true){
//  header('location: index.php');
//}
Connect();
function generateSalt($max = 15) {
        $characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?";
        $i = 0;
        $salt = "";
        while ($i < $max) {
            $salt .= $characterList{mt_rand(0, (strlen($characterList) - 1))};
            $i++;
        }
        return $salt;
}
if (isset($_POST['done']) && isset($_POST['username']) && isset($_POST['password'])){
    $password = mysql_real_escape_string($_POST['password']);
    $username = mysql_real_escape_string($_POST['username']);
    $passhash = hash("ripemd320", $password);
    $salt     = generateSalt(); 
    $hashUser = hash("ripemd320", $username);
    $getUsr   = mysql_fetch_array(mysql_query("SELECT * FROM `accounts` WHERE `Username`='$username'"));
    if ($getUsr != 0){
        if ($getUsr['Password'] == $passhash){
                $string = md5(uniqid(rand(), true));
                $salt = substr($string, 0, 3);
                $expire = 60 * 60 * 24 * 3650 + time(); //10 years
                setcookie("Salt", "x", $expire, "/", ".buildation.x10.bz") or die("Failed");
                setcookie("Setters", "f", $expire, "/", ".buildation.x10.bz");
                mysql_query("UPDATE accounts SET Salt='$salt' WHERE Username='$username'") or die(mysql_error());
        //header('location: index.php');
                exit();
        }
    }
}

?>
<form action="" method="post">
    Username: <input type="text" name="username" style="background-color: #00A8A8;" />
    <br/>
    Password: <input type="password" name="password" style="background-color: #00A8A8;"  />
    <br/>
    <input type="submit" name="done" value="Login" />
</form>

该代码应该设置包含值“x”和“f”的cookie“Salt”和“Setters” - 但是,它不执行此操作但它执行其余代码。

我得到的错误是:

警告:无法修改标题信息 - 第31行/home/buildati/public_html/Login.php中已经发送的标题(在/home/buildati/public_html/global.php:5中开始输出) 失败

第31行是:

setcookie(“Salt”,“x”,$ expire,“/”,“。buildild.x10.bz”)或死亡(“失败”);

我似乎无法理解这一点,可能是什么问题?

0 个答案:

没有答案