Friday, 6 September 2013

Download Login Form in PHP and Mysql


In this tutorial I will explain that how to create a login form in PHP mysql. Creating login form in any language has same concept that We will retrieve the previously stored User credential like User name and password and while Login when user provide inputs like Username and password, we will compare this Input with the stored fields for that User. When both are same user will redirect to the next page otherwise some error will print with login fail message. Now let us understand the steps behind this

Steps for creating a Login Form are given below:
There are two PHP file
  1. dbConfig.php
  2. login.php
The dbConfig.php file looks like this:
1
2
3
4
5
6
7
8
9
<!--?php
define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", ""); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME",""); // set database name
 
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
?-->
Here just change the above value like Host name, database user name, database password and database name.
The login.php file looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!--php include "dbConfig.php";
$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST["name"];
    $password = md5($_POST["password"]);
     if ($name == '' || $password == '') {
        $msg = "You must enter all fields";
    } else {
        $sql = "SELECT * FROM members WHERE name = '$name' AND password = '$password'";
        $query = mysql_query($sql);
 
        if ($query === false) {
            echo "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }
 
        if (mysql_num_rows($query) --------> 0) {
          
            header('Location: YOUR_LOCATION');
            exit;
        }
 
        $msg = "Username and password do not match";
    }
}
?>
In this file just change YOUR_LOCATION variable into your exact success path.
That's all... Enjoy with login form
For any feedback and comment, Please write us below comment. Your comment will be highly appreciable.



:



No comments:

Post a Comment