Friday, May 24, 2013

Simple PHP Login Form

How to create a login page ? 

That's the question. How do we create a login page ?, If you are a pro maybe this post sounds easy for you, and if there's a mistake on this post, please let me know.  Login Page is a page that contains a script to login user from your website to use features of your website, for example facebook.com, twitter.com, those sites will not let us use it's features if we not login. In this post i would like to create a simple login page. Let's Begin :

What you need :
1. XAMPP (a free and open source cross-platform web server package).
2. Editor like notepad or notepad++

1. Open your Browser, and type http://127.0.0.1/phpmyadmin/ or http://localhost/phpmyadmin on the address bar.

2. Create a database and type the name, then click Create


3. Open your “tutorial” Database and on the create table type login on the name form and 3 for Number of Columns, then click Go.


4. On first column set Name : id | Type : int | Length : 3 | Index : Primary | A_I : Check
Second column Name : username | Type : varchar  | Length : 25
Third column Name : username | Type : varchar  | Length : 25
Then click save.


5.  Click insert on your login table, new window will show up.


6. Insert value for id, username, password, click Go.


7. Database Creation Completed.

II. Login Page

1. Open your editor, then type :
<html>
<head><title>Login</title></head> //title for this page
<body>
<h1><center>Welcome to Project</center></h1> //Heading
<center>
<form action="loginprocess.php" method="post"> //If this form submitted then it will go to loginprocess.php
<br />
<br />
<br />
<table>
<tr><td colspan="2" align="center"><h1>Login</h1></td></tr>
<tr><td> Username </td><td> : <input type="text" name="username" ></td></tr> //Username Form
<tr><td> Password </td><td> : <input type="text" name="password" </td></tr> //Password Form
<tr><td colspan="2" align="left"><input type="submit" value="Login"><input type="reset" value="Reset"></td></tr> //Submit and Reset Button
</table>
</form>
</center>
</body>
</html>
view raw index.php hosted with ❤ by GitHub

Save it on your htdocs as index.php.

2. Now let’s create loginprocess.php
<?php
include "config.php";
$username = $_POST['username']; //Take username on index.php
$password = $_POST['password']; //Take password on index.php
$user = mysql_query("SELECT * FROM login WHERE username='$username' AND password='$password'"); // Select login table username and password
$match = mysql_num_rows($user); //Check all table query
if($match==1){ //If user and password match, Login Success will appear.
echo “Login Success”;
}else if ($username == "" && $password == ""){ //If username and password form are blank.
echo "Please fill username and password";
}else{ //If wrong username or password
echo "Failed";
}
?>
.
Save it as loginprocess.php

3. Create config.php
<?php
mysql_connect("localhost", "root","") or die("Connection Failed");// Connect to localhost with username root and password.
mysql_select_db("tutorial") or die("DB Not Found"); // Select mysql Database tutorial
?>
view raw config.php hosted with ❤ by GitHub
4. Test your login page.

0 comments:

Post a Comment