title: create login and logout session
body: <p>I want to create login and logout session</p>
  
  <p>My table in MySQL database is looking like this</p>
  
  <pre><code>CREATE TABLE `login` (
    `id` int(10) NOT NULL auto_increment,
    `username` varchar(20) NOT NULL,
    `password` varchar(20) NOT NULL,
    PRIMARY KEY  (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  </code></pre>
  
  <p>and MySQL connection named as <code>config.inc</code></p>
  
  <pre><code>&lt;?php
  
   $hostname = 'localhost';        // Your MySQL hostname. Usualy named as 'localhost',     so you're NOT necessary to change this even this script has already     online on the internet.
  $dbname   = ''; // Your database name.
  $username = '';             // Your database username.
  $password = '';                 // Your database password. If your database has no     password, leave it empty.
  
  // Let's connect to host
  mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed,     perhaps the service is down!');
  // Select the database
  mysql_select_db($dbname) or DIE('Database name is not available!');
  
  ?&gt;
  </code></pre>
  
  <p>My <code>index.php</code> page looking like this</p>
  
  <pre><code>&lt;?php
  
  // Inialize session
  session_start();
  
  // Check, if user is already login, then jump to secured page
  if (isset($_SESSION['username'])) {
  header('Location: securedpage.php');
  }
  
  ?&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;title&gt;PHPMySimpleLogin 0.3&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
   &lt;h3&gt;User Login&lt;/h3&gt;
  
  &lt;table border="0"&gt;
  &lt;form method="POST" action="loginproc.php"&gt;
  &lt;tr&gt;&lt;td&gt;Username&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="username" size="20"&gt;        &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Password&lt;/td&gt;&lt;td&gt;:&lt;/td&gt;&lt;td&gt;&lt;input type="password" name="password"     size="20"&gt;    &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="submit" value="Login"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/form&gt;
  &lt;/table&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Checking username and password file named as <code>loginproc.php</code></p>
  
  <pre><code>&lt;?php
  
  // Inialize session
  session_start();
  
  // Include database connection settings
  include('config.inc');
  
  // Retrieve username and password from database according to user's input
  $login = mysql_query("SELECT * FROM user WHERE (username = '" .     mysql_real_escape_string($_POST['username']) . "') and (password = '" .     mysql_real_escape_string(md5($_POST['password'])) . "')");
  
  // Check username and password match
   if (mysql_num_rows($login) == 1) {
  // Set username session variable
  $_SESSION['username'] = $_POST['username'];
  // Jump to secured page
   header('Location: securedpage.php');
  }
  else {
  // Jump to login page
  header('Location: index.php');
  }
  
  ?&gt;
  </code></pre>
  
  <p>My secure page code named as <code>securedpage.php</code></p>
  
  <pre><code>&lt;?php
  
  // Inialize session
  session_start();
  
  // Check, if username session is NOT set then this page will jump to login page
  if (!isset($_SESSION['username'])) {
  header('Location: index.php');
  }
  
  ?&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;title&gt;Secured Page&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;p&gt;This is secured page with session: &lt;b&gt;&lt;?php echo $_SESSION['username']; ?&gt;&lt;/b&gt;
  &lt;br&gt;You can put your restricted information here.&lt;/p&gt;
  &lt;p&gt;&lt;a href="logout.php"&gt;Logout&lt;/a&gt;&lt;/p&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>And finally logout page named as <code>logout.php</code> </p>
  
  <pre><code>&lt;?php
  
  // Inialize session
  session_start();
  
  // Delete certain session
  unset($_SESSION['username']);
  // Delete all session variables
  // session_destroy();
  
  // Jump to login page
  header('Location: index.php');
  
  ?&gt;
  </code></pre>
  
  <p>Now my problem is when I am entering username and password it will stay only at <code>index.php</code> , it is not going to another page. Please see this code and tell me when I am doing wrong.</p>
  
  <p>Thanks.</p>