Tuesday, June 26, 2012

Simple PHP login from mysql database


Step-1: Copy this bellow code in to notepad and save it as "index.php".

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="login.php" method="post">
<input type="text" name="uname" /><br />
<input type="password" name="pass" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>

Step-2: Copy this bellow code and save it as "login.php".



<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$uname=$_POST["uname"];
$pass=$_POST["pass"];
//print "$name  $pass";
$con=mysql_connect("localhost","root","");
if(!$con){
die("Database could not connect: " .mysql_error());
}
mysql_select_db("userinfo",$con);
$sql= "SELECT * FROM `login` WHERE username='$uname' and password='$pass'";
$result=mysql_query($sql);
$n=mysql_num_rows($result);
if($n==0){
print "You did not registered yet!";
}else{
print "Login successful!";
}
?>
</body>
</html>

Step-3: If you use xampp then above two newly created php files save in a folder named "login"(if you want) and copy the folder and past it in your local host root folder name "htdocks". And if you use wampp then past it in to "www" folder. That's it.

Step-4: Create Database
Go to the localhost from browser and then go php myadmin. You will find this pictures bellow. Just follow and create your database and table according to my picture.

1. You are in localhost
2. Type "userinfo"
3. Click "Create"

how to create database

"userinfo" database created. Now:
1. Type "login"
2. type "2"
3. click "Go"

how to create database table

Fill up the form according to picture bellow and click "save"

how to create database

You will find a "login" table created under "userinfo" database in left side. Click on "login" from left side and you will see this picture bellow. Fill up it according to picture and click the second "Go" button.

database table input

After clicking "Go" button you will see the bellow picture.

database table inside

Click the "login" and you are done.

database table for login


Now you are ready to check your login form. Go to your browser and type "localhost/login" . You will find your login form. If your username and password are same as in database then your login is success otherwise cannot login.

No comments:

Post a Comment