Learning & Integrating web technology and code help directory

Simple Login Script in PHP and mysql

No comments
This is a simple login script using php and mysql. In this script, a form will be displayed with two fields, username and password. When user is submitting with valid user name and password, then he can access authenticated page.

Steps to create Simple Login Script in PHP and mysql

  1. First step we will connect to the database.
  2. Then we will select the database.
  3. We are checking, if the form is submitted or not. In this step we have two logic’s.
    • What if the form is submitted.
      1. If the form is submitted, we are assigning the posted values to variables and checking the values are existing in the database or not.
      2. If the values submitted in the form and the values in the database are equal, then we will create a session for the user.
      3. If the values are not equal then it will display an error message.
      4. And then we checks for the session, if the session exists we will great him with the username, otherwise the form will be displayed.
    • What if not the form is submitted.
      1. When the user comes first time, he will be displayed with a simple form.
      2. User Name, Password and a submit button.
1

Create a table

If you are already following from previous article, you should already have table created. If you don’t have create the table.
1CREATE TABLE `user` (
2  `id` int(11) NOT NULL AUTO_INCREMENT,
3  `username` varchar(255) NOT NULL,
4  `email` varchar(255) NOT NULL,
5  `passwordvarchar(255) NOT NULL,
6  `active` tinyint(1) NOT NULL,
7  PRIMARY KEY (`id`),
8  UNIQUE KEY `username` (`username`)
9)
2

Creating HTML Form

This is the form, only displayed if message variable in not set.
1<div class="register-form">
2<?php
3    if(isset($msg) & !empty($msg)){
4        echo $msg;
5    }
6 ?>
7<h1>Login</h1>
8<form action="" method="POST">
9    <p><label>User Name : </label>
10    <input id="username" type="text" name="username"placeholder="username" /></p>
11  
12     <p><label>Password&nbsp;&nbsp; : </label>
13     <input id="password" type="password" name="password"placeholder="password" /></p>
14  
15    <a class="btn" href="register.php">Signup</a>
16    <input class="btn register" type="submit" name="submit" value="Login"/>
17    </form>
18</div>
19<?php } ?>
3

Adding styles to form

And the styles for the form, if you have added styles in previous article. Skip this step.
1.register-form{
2    width500px;
3    margin0 auto;
4    text-aligncenter;
5    padding10px;
6    color#fff;
7    background : #c4c4c4;
8    border-radius: 10px;
9    -webkit-border-radius:10px;
10    -moz-border-radius:10px;
11}
12
13.register-form form input{padding5px;}
14.register-form .btn{background#726E6E;
15padding7px;
16border-radius: 5px;
17text-decorationnone;
18width50px;
19display: inline-block;
20color#FFF;}
21
22.register-form .register{
23border0;
24width60px;
25padding8px;
26}
4

Connect to Database

If you are following from previous user registration article, no need to create this file. Other wise create connect.php file.
1<?php
2$connection = mysql_connect('localhost''root''');
3if (!$connection){
4    die("Database Connection Failed" . mysql_error());
5}
6$select_db = mysql_select_db('test');
7if (!$select_db){
8    die("Database Selection Failed" . mysql_error());
9}
5

PHP Logic for User Login

And this is the PHP code for logging in user
1<?php  //Start the Session
2session_start();
3 require('connect.php');
4//3. If the form is submitted or not.
5//3.1 If the form is submitted
6if (isset($_POST['username']) and isset($_POST['password'])){
7//3.1.1 Assigning posted values to variables.
8$username $_POST['username'];
9$password $_POST['password'];
10//3.1.2 Checking the values are existing in the database or not
11$query "SELECT * FROM `user` WHERE username='$username' and password='$password'";
12  
13$result = mysql_query($queryor die(mysql_error());
14$count = mysql_num_rows($result);
15//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
16if ($count == 1){
17$_SESSION['username'] = $username;
18}else{
19//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
20echo "Invalid Login Credentials.";
21}
22}
23//3.1.4 if the user is logged in Greets the user with message
24if (isset($_SESSION['username'])){
25$username $_SESSION['username'];
26echo "Hai " $username . "
27";
28echo "This is the Members Area
29";
30echo "<a href='logout.php'>Logout</a>";
31  
32}else{
33//3.2 When the user visits the page first time, simple login form will be displayed.
34?>

No comments :

Post a Comment