I am doing a login page. I want to refresh the page after successful login so that the user name and log out link to be displayed, otherwise login and register links are displayed. As I saw in stack overflow answers I used header('Location: '.$_SERVER['HTTP_REFERER']); it was working. But now it is not working showing an error Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\myphpproject\login.php:191) in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\myphpproject\login.php on line 228 As I saw in the particular answer I pasted the code on the very top. Then nothing is happening. just refreshing the page without logging in.I want to refresh the page after log in so that the user name and log link is displayed The code below
<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
}
?>