Friday, March 28, 2014

PHP Login Example



Simple login example using PHP & Mysql.

Download Link



Step 1:
Create table using the following sql commands.

CREATE TABLE IF NOT EXISTS `admin` (
  `AdminId` int(11) NOT NULL AUTO_INCREMENT,
  `Username` varchar(225) NOT NULL,
  `Password` varchar(225) NOT NULL,
  PRIMARY KEY (`AdminId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Step 2:
HTML codes for Login form (Login.php)

<form action="#" method="POST">
    <fieldset>
    <p>
      <label for="email">E-mail address</label>
    </p>
    <p>
      <input type="text" id=" username " name="username" value="" required >
    </p>
    <p>
      <label for="password">Password</label>
    </p>
    <p>
      <input type="password" name="password" id="password" value="" required>
    </p>
    <p>
      <input type="submit" name="Login" value="Sign In">
    </p>
    </fieldset>
  </form>

Step 3:
Write database connection(config.php)

<?php
session_start();
$host = "localhost"; /*Your Hostname*/
$user = "root"; /*Your Databse Username*/
$pass = ""; /*Your Databse Password*/
$db_name = "login"; /*Your Databse Name*/
//database connection
$link = mysql_connect($host, $user, $pass);
$db=mysql_select_db($db_name);
?>

Step 4:
Check posted username and password are correct(include this code top of the login.php)
<?php
                //Database file
                require_once("config.php");
               
                if(isset($_POST['Login'])) {
                                //Get values from post
                                $username=mysql_real_escape_string($_POST['username']);
                                $password1=mysql_real_escape_string($_POST['password']);
                                $password=md5($password1); // Encrypted Password
                               
                                //Check method.Query is correct or not.
                                //echo "SELECT * FROM admin WHERE username='$username' AND Password='$password'";
                                //exit;
                               
                                //Query for login
                                                $res="SELECT * FROM admin WHERE username='$username' AND password='$password'";
                                                $result=mysql_query($res);
                                                $count=mysql_num_rows($result);
                                               
                                // If result matched $username and $password, table row must be 1 row
                                if($count==1)
                                                                                {   //Store values in SESSION
                                                                                                $_SESSION['username'] = $_POST['username'];
                                                                                                $username=$_SESSION['username'];
                                                                                                header("location:welcome.php");//redirect to welcome page
                                                                                } else {
                                                                                                ?>
                                                                                <script type="text/javascript">
                                                                                                 alert('OOPS!.Either the username or the password is wrong. Please try again');
                                                                                                 window.top.location="index.php";
                                                                                </script>
                                                                                <?php }
                                                               
                }
?>

Step 5:
If login successful redirect to welcome page.(welcome.php)
<?php
                //Database file
                require_once("config.php");
                $username=$_SESSION['username'];
?>
Wecome <?php echo $username; ?><a href="logout.php">Logout</a>

Step 6:
Logout.php

<?php

/**
 * The logout file
 * destroys the session
 * expires the cookie
 * redirects to login.php
 */
session_start();
setcookie('Email', '', time() - 1*24*60*60);
setcookie('Password', '', time() - 1*24*60*60);
//Session Destroy
if(session_destroy())
{
                header("location:index.php");
} ?>


1 comment:

  1. What if I already have login when I reach login.php? And what if I don't have login when I reach the welcome.php? You should deal with it ;)

    ReplyDelete