-1

我是 PHP 新手,我试图研究这个问题,但也许我问得不对……我可以从其他帖子/说明中看出,在打印出 HTML 后我不能使用 header() 或 setcookie()。 ..

我注释掉了标题“重定向” - 是否可以在 HTML 之后放置一个替代方法?目前失败的是 setcookies:

我没看到什么?我不断收到错误,但我的 HTML 在 PHP 之后:

if (!isset($_COOKIE["user"])) {

$sUserIdentity = $_POST["userIdentity"];
//username is not accepted as a get value
$sPassword = $_POST["password"];
$sEmail = $_POST["email"];


$cnn= odbc_connect("Driver={SQL Server};Server=$server;Database=$dbI", $user, $password);
//check to see if email account or username already used
$sql2 = "select id from i_user where email = '" . $sEmail  . "' or username ='" . $sUserIdentity . "'";
//echo $sql2 ."<br>";
$result = odbc_exec($cnn, $sql2);
$id = odbc_result($result,"id");


if ($id == ''){
    $cnnCreate = odbc_connect("Driver={SQL Server};Server=$server;Database=$dbI", $user, $password);
    $sqlCreate  = "insert into i_user (username,email,salt,active) values ";
    $sqlCreate .= "(";
    $sqlCreate .= "'" . $sUserIdentity  . "',";
    $sqlCreate .= "'" . $sEmail  . "',"; 
    $rsCreate = odbc_exec($cnnCreate, $sqlCreate);

    $sql2 = "select * from i_user where email = '" . $sEmail  . "' and username ='" . $sUserIdentity . "'";
    //echo $sql2 ."<br>";
    $result = odbc_exec($cnn, $sql2);

    $expire=time()+60*60*24*30;
    setcookie("uid", odbc_result($result,"id"), $expire);
    setcookie("user", odbc_result($result,"username"), $expire);

    if ($rsCreate){ 
        $sMsg = "congratulations " . $_COOKIE["user"] . " and welcome "; 
    } 
    else { 
        $sMsg = "There was an error with the query"; 
    }  
}else{
    $sMsg =  "User " . $id . " already in DB";
    //header('Location: ../fec/createuser.php?error=id');
}
}
//echo $sqlCreate;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Form</title>
<script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.10.2.custom.min.js"></script>
<link rel="stylesheet" href="../css/styles.css" type="text/css">
<style>
</style>
</head>

<body>


</body> 
</html>
4

2 回答 2

1

确保<?php位于文件的最开头,前面没有空行。在调用setcookie().

于 2013-05-30T20:34:21.160 回答
1

为什么不使用输出缓冲?

<?php

    ob_start();

?>
<html>
<body>
...
</body>
</html>
<?php

    ob_end_flush();

?>

这会将任何输出放入缓冲区,只要缓冲区未刷新,您就可以修改标头信息。这也将避免在您写入标头之前将 PHP 警告或错误消息发送到输出的问题。

于 2013-05-30T21:39:54.213 回答