Wednesday, 1 May 2013

Episode 21 : Login and LogOut process in PHP MySQL



When you logged in your account then the environment of your account is starting which is called session. When session is started then you can access your account. So, when we create an login process, must be create an session for accessing your account. Let’s do it.

At first create a database table for holding user information. The database structure is following :

-- Table structure for table `member`
--

CREATE TABLE IF NOT EXISTS `member` (
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `position` varchar(10) NOT NULL,
  `varify` varchar(100) DEFAULT NULL,
  `status` tinyint(2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--

Now creating a form for login and save it.

<?php
session_start();
session_destroy();
?> 
<html>
<form id="form1" name="login_form" method="POST" action="login.php">
                 
                  <div align="center">
                    <table width="433" height="169" border="0">
                      <tr>
                        <td height="36" colspan="2" valign="top"><h2 style="text-shadow: 5px 5px 5px red;"><strong>Login :</strong></h2></td>
        </tr>
                      <tr>
                        <td width="148">Username : </td>
        <td width="275" valign="top">
          <input type="text" name="username" value='' />    </td>
      </tr>
                      <tr>
                        <td>Password : </td>
        <td valign="top"><input type="password" name="password" value='' /></td>
      </tr>
                      <tr>
                        <td>&nbsp;</td>
        <td><input type="submit" value="Login" /></td>
      </tr>
                    </table>
          </div>
                </form>
    </div></td>
  </tr>
</table>
</html>


Now Create the processing code for login.

<?php
session_start();
session_destroy();

$user = "";
$pass = "";
$msg = "";

if($_SERVER['REQUEST_METHOD']=='POST')
{
include 'connection.php';

$user = $_POST['username'];
$pass = $_POST['password'];

$user = htmlspecialchars($user);
$pass = htmlspecialchars($pass);

$sql = "SELECT * FROM info";
$result = mysql_query($sql);
while($db_field = mysql_fetch_assoc($result))
{
          $a = $db_field['username'];
          $b = $db_field['password'];
          $pos = $db_field['position'];
          if(($user == $a) AND ($pass == $b)){
                  
          if($pos == "admin"){
          session_start();
          $_SESSION['username'] = $user;
          $_SESSION['admin'] = "log";
          mysql_close($connection);
          header("Location: admin.php");
          break;
          }
          else if($pos == "member"){
          session_start();
          $_SESSION['username'] = $user;
          $_SESSION['member'] = "log";
          mysql_close($connection);
          header("Location: member.php");
          break;
         
          }
          else if($pos == "Leader"){
          session_start();
          $_SESSION['username'] = $user;
          $_SESSION['Leader'] = "log";
          mysql_close($connection);
          header("Location: leader_home.php");
          break;
         
          }
}

}
$msg = "Check username and password !";
mysql_close($connection);
          }

?>


Now set following code in the first account for entering your account.

<?php
session_start();
$user = $_SESSION['username'];
$log = $_SESSION['admin'];
if($log != "log"){
header("Location: login.php");

}
?>


LogOut

You can logout your account simply destroy the session. Indicate the page first and link that page and set,

session_start();
session_destroy();





Written by ‘Shojib’.

No comments:

Post a Comment