title: Check the login status and save the login sessions(PHP)
body: <p>I am working on a log-in system, Whenever the user tries to access the non-authorized page 
  then he should return on the login page to login, how can I perform it</p>
  
  <p>Below is my log-in script</p>
  
  <pre><code>&lt;?php
  
  session_start();
  $host="localhost"; // Host name 
  $db_username="root"; // Mysql username 
  $db_password=""; // Mysql password 
  $db_name="designshop"; // Database name 
  $tbl_name="member"; // Table name 
  
  // Connect to server and select databse.
  mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // username and password sent from form 
  $member_username=$_POST['member_username']; 
  $password=$_POST['password']; 
  
  // To protect MySQL injection (more detail about MySQL injection)
  $member_username = stripslashes($member_username);
  $password = stripslashes($password);
  $member_username = mysql_real_escape_string($member_username);
  $password = mysql_real_escape_string($password);
  $sql="SELECT * FROM $tbl_name WHERE member_username='$member_username' and password='$password'";
  $result=mysql_query($sql);
  
  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){
  
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  $_SESSION['member_username']=$_POST['member_username'];
  $_SESSION['password']=$_POST['password'];
  
  header("location:login_success.php");
  }
  else {
  header("location:try_again.html");
  }
  ?&gt;
  </code></pre>