我正在做一个登录页面。我想在成功登录后刷新页面,以便显示用户名和注销链接,否则显示登录和注册链接。正如我在堆栈溢出答案中看到的,我使用了标题('位置:'。$ _ SERVER [' HTTP_REFERER']);它正在工作。但现在它无法显示错误 警告:无法修改标头信息 - 已在C:\ Program中发送的标头(在C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ projects \ myphpproject \ login.php:191中开始输出)第228行的文件(x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ projects \ myphpproject \ login.php 正如我在特定答案中看到的那样,我将代码粘贴在最顶层。然后什么也没发生。只需刷新页面而无需登录。我想在登录后刷新页面,以便显示用户名和日志链接下面的代码
<div class="container">
<header>
<p class="left">
<?php
session_start();
if(!isset($_SESSION["user_id"]))
{
?>
<a href="login.php">Login</a>
<a href="register1.php">Register</a>
<?php
}
else
{ echo $_SESSION["name"];
?>
<a href="logout.php">Log out</a>
<?php
}
?>
</p>
<h1>title</h1>
<ul id="MenuBar1" class="MenuBarHorizontal">
<li><a href="index.php">Home</a>
</li>
<li><a href="Rooms.html">Rooms</a></li>
<li><a href="<?php if($_SESSION["username"]=="admin2006") echo "admin.php"; ?>">Admin</a></li>
<li><a href="#">About us</a></li>
</ul>
</header>
<div class="sidebar1"><!-- InstanceBeginEditable name="EditRegion3" -->
<aside> <img src="" width="180" height="330" alt="Insert Add" style="background-color:#EADCAE"> </aside>
<!-- InstanceEndEditable --><!-- end .sidebar1 --></div>
<!-- InstanceBeginEditable name="EditRegion4" -->
<article class="content">
<?php
include 'connection.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!preg_match('/^[A-Za-z][A-Za-z0-9]{5,9}$/', $_POST["username"])) {
echo "invalid username";
}
else if ( !preg_match('/^[A-Za-z][A-Za-z0-9]{5,9}$/', $_POST["password"]) ) {
echo "invalid password";
}
else{
$username=$_POST["username"];
$password=$_POST["password"];
$shapassword= sha1($password);
$sql="select user_id,fname,lname from users "
. "where username = '".$username. "'and password = '".$shapassword."'";
$result = db_query($sql);
$row= mysqli_fetch_array($result);
if(!$row)
{
$message= "login failed";
}
else{
$userid= $row["user_id"];
$_SESSION["user_id"]=$userid;
$_SESSION["username"]=$username;
$_SESSION["name"]=$row["fname"]." ".$row["lname"];
//header('Location: '.$_SERVER['HTTP_REFERER']);
}
}
}
if(!isset($_SESSION["user_id"]))
{
?>
<h1>Login</h1>
<section>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" name="login_form" class="form"><table width="500">
<tr>
<td>Username</td>
<td><input type="text" name="username" id="username" required></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password" required></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="login" id="login" value="Login"></td>
</tr>
</table>
</form>
</section>
<?php
}
else{
?>
<section class="form"><p>welcome <?php echo $_SESSION["name"];?></p></section>
<?php
}
?>