我是PHP的新手,我试图研究这个问题,但也许我没有问对...我可以从其他帖子/说明中告诉我打印后我不能用户头文件()或setcookie()出HTML ...
我注释掉了标题“redirects” - 我可以在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>
答案 0 :(得分:1)
确保<?php
位于文件的最开头,前面没有空行。在致电setcookie()
之前,您无法打印或回复任何内容。
答案 1 :(得分:1)
为什么不使用输出缓冲?
<?php
ob_start();
?>
<html>
<body>
...
</body>
</html>
<?php
ob_end_flush();
?>
这会将任何输出放入缓冲区,只要不刷新缓冲区,就可以修改标头信息。 如果在写入标题之前将PHP警告或错误消息发送到输出,这也可以避免出现问题。