我创建了一个包含用户名,密码,用户类型,电子邮件等的用户表...用户类型将用户区分为管理员或其他用户。现在,登录后,getuser.php 页面用于根据用户类型进行重定向。请帮助我。谢谢。
这是我的getuser.php页面:
<?php include 'connect.php'; ?>
<?
$user = $_GET['user'];
session_start();
$_SESSION['user'] = $username;
if(isset($_SESSION['user'])){
$sql = "SELECT usertype FROM users WHERE userName='".$username."'";
$result = $db->query($sql);
//the function num_rows() checks if there are more than zero rows returned
if ($result->num_rows > 0) {
//echo "<table><tr><th>SELECT</th><th>ADID</th><th>ADName</th><th>ADCATEGORY</th><th>CONTACTNUMBER</th><th>EXPIRATIONDATE</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$level= $row['usertype'];
}
}
if ($level=='1')
{
header("Location: adlist.php?user=$username");
}
else
{
header("Location: post_ad.php?user=$username");
}
}
else {
echo " Sorry, but you must login to view the members area"
}
?>
Scrowler的评论是完全正确的
当 PHP 执行文件时,?> <?php
之间的所有空格都被视为输出。
在您的问题示例中:
<?php include 'connect.php'; ?>
<!-- this gap produces output -->
<?php /* ... */ ?>
为什么这是一件大事?
要发出重定向标头,必须将标头发送到客户端,这些标头必须在 HTTP 响应正文之前发送。这意味着当您在程序中发出重定向后者时,已经晚了,因为正文已经开始写入 HTTP 响应。如果您正在使用输出缓冲,则客户端可能已经收到了一些响应。
响应标头如下所示:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
Date: Thu, 20 Nov 2014 00:55:09 GMT
Content-Length: 10614
响应看起来会更熟悉:
<!DOCTYPE html>
<html>
<head>
<title>Edit - Stack Overflow</title>
<!-- ... -->
</head>
<body>
<!-- ... -->
</body>
</html>
要解决此问题,请按如下方式更改文件的前几行
<?php
include 'connect.php';
$username = $_GET['user']; // not $user = $_GET['user'];
// ...