This tutorial, We Learn to create a simple login system with php + mysql, this tutorial is easy to follow and teach you step by step.
Overview
In this tutorial, we create 3 php files for testing our code.
1. index.php (Main Login)
2. db_connect.php (for db connection)
3. Success.php
Steps
1. Create table "user_info" in database "company_name".
2. Create file index.php (Main Login).
3. Create file db_connect.php.
4. Create file success.php.
5. Create file logout.php
Step:1 : Create table "user_info" in database "company_name".
For checking this code, we need to create database "company_name" and create table "user_info".
CREATE TABLE `user_info` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- Dumping data for table `members`
--
INSERT INTO `user_info` VALUES (1, 'hossaini', '12345');
Step:2. Create file index.php (Main Login).
// Top On the page
<?php
require_once('db_connect.php');
session_start();
ob_start();
error_reporting (0);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$username=$_POST['username'];
$password=md5($_POST['pass']);
$sql=("SELECT * FROM `user_info` WHERE `email` LIKE '$username' AND `password` LIKE '$password'");
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count==1){
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
session_register("username");
session_register("password");
echo "<script language='javascript'> location.href='success.php';</script>";
}
else {
$errormessage == "wrong User name or PassWord";
}
}
?>
// Where you need to show your user & password text box
<form name="login" method="post" action="">
Username:
<input style="color:#ccc;" type="text" name="username" value="User Name" onfocus="if (this.value == 'User Name'){this.value = '';}" onblur="if (this.value == ''){this.value = 'User Name';}" /><br />
Password:
<input style="color:#ccc;" type="password" name="pass" value="Pass Word" onfocus="if (this.value == 'Pass Word'){this.value = '';}" onblur="if (this.value == ''){this.value = 'Pass Word';}" /><br>
<input style="width:87px;" type="submit" name="login" value="Login" />
</form>
Step 3. Create file db_connect.php.
<?php
$connection = mysql_connect("localhost","root","");
if (!$connection)
{
die('could not connect:' . mysql_error());
}
$db_selected = mysql_select_db("company_name",$connection);
?>
Step 4. Create file success.php.
<h1> Successfully Login </h1>
Step 5. Create file logout.php
<?php
session_start();
session_destroy();
?>
Overview
In this tutorial, we create 3 php files for testing our code.
1. index.php (Main Login)
2. db_connect.php (for db connection)
3. Success.php
Steps
1. Create table "user_info" in database "company_name".
2. Create file index.php (Main Login).
3. Create file db_connect.php.
4. Create file success.php.
5. Create file logout.php
Step:1 : Create table "user_info" in database "company_name".
For checking this code, we need to create database "company_name" and create table "user_info".
CREATE TABLE `user_info` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- Dumping data for table `members`
--
INSERT INTO `user_info` VALUES (1, 'hossaini', '12345');
Step:2. Create file index.php (Main Login).
// Top On the page
<?php
require_once('db_connect.php');
session_start();
ob_start();
error_reporting (0);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$username=$_POST['username'];
$password=md5($_POST['pass']);
$sql=("SELECT * FROM `user_info` WHERE `email` LIKE '$username' AND `password` LIKE '$password'");
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count==1){
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
session_register("username");
session_register("password");
echo "<script language='javascript'> location.href='success.php';</script>";
}
else {
$errormessage == "wrong User name or PassWord";
}
}
?>
// Where you need to show your user & password text box
<form name="login" method="post" action="">
Username:
<input style="color:#ccc;" type="text" name="username" value="User Name" onfocus="if (this.value == 'User Name'){this.value = '';}" onblur="if (this.value == ''){this.value = 'User Name';}" /><br />
Password:
<input style="color:#ccc;" type="password" name="pass" value="Pass Word" onfocus="if (this.value == 'Pass Word'){this.value = '';}" onblur="if (this.value == ''){this.value = 'Pass Word';}" /><br>
<input style="width:87px;" type="submit" name="login" value="Login" />
</form>
Step 3. Create file db_connect.php.
<?php
$connection = mysql_connect("localhost","root","");
if (!$connection)
{
die('could not connect:' . mysql_error());
}
$db_selected = mysql_select_db("company_name",$connection);
?>
Step 4. Create file success.php.
<h1> Successfully Login </h1>
Step 5. Create file logout.php
<?php
session_start();
session_destroy();
?>