title: refresh the page at a particular point header() not working
body: <p>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
  <strong>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</strong> The code below</p>
  
  <pre><code>    &lt;div class="container"&gt;
    &lt;header&gt;
    &lt;p class="left"&gt;
    &lt;?php
    session_start();
    if(!isset($_SESSION["user_id"]))
    {
    ?&gt;
      &lt;a href="login.php"&gt;Login&lt;/a&gt;
      &lt;a href="register1.php"&gt;Register&lt;/a&gt;
      &lt;?php
      }
      else
      { echo $_SESSION["name"];
      ?&gt;
      &lt;a href="logout.php"&gt;Log out&lt;/a&gt;
      &lt;?php
      }
      ?&gt;
      &lt;/p&gt;
      &lt;h1&gt;title&lt;/h1&gt;
      &lt;ul id="MenuBar1" class="MenuBarHorizontal"&gt;
        &lt;li&gt;&lt;a href="index.php"&gt;Home&lt;/a&gt;
  
        &lt;/li&gt;
        &lt;li&gt;&lt;a href="Rooms.html"&gt;Rooms&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="&lt;?php if($_SESSION["username"]=="admin2006") echo "admin.php";  ?&gt;"&gt;Admin&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;About us&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
  
  
  
    &lt;/header&gt;
    &lt;div class="sidebar1"&gt;&lt;!-- InstanceBeginEditable name="EditRegion3" --&gt;
      &lt;aside&gt; &lt;img src="" width="180" height="330" alt="Insert Add" style="background-color:#EADCAE"&gt; &lt;/aside&gt;
    &lt;!-- InstanceEndEditable --&gt;&lt;!-- end .sidebar1 --&gt;&lt;/div&gt;
    &lt;!-- InstanceBeginEditable name="EditRegion4" --&gt;
    &lt;article class="content"&gt;
      &lt;?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"]))
        {
        ?&gt;
      &lt;h1&gt;Login&lt;/h1&gt;
      &lt;section&gt;
       &lt;form action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;" method="post" name="login_form" class="form"&gt;&lt;table width="500"&gt;
    &lt;tr&gt;
      &lt;td&gt;Username&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="username" id="username" required&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Password&lt;/td&gt;
      &lt;td&gt;&lt;input type="password" name="password" id="password" required&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&lt;input type="submit" name="login" id="login" value="Login"&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
      &lt;/section&gt;
      &lt;?php
        }
        else{
        ?&gt;
      &lt;section class="form"&gt;&lt;p&gt;welcome &lt;?php echo $_SESSION["name"];?&gt;&lt;/p&gt;&lt;/section&gt;
      &lt;?php
        }
        ?&gt;
  </code></pre>