Fatkhan
On Sabtu, 21 November 2009
|
|
In this tutorial create 3 files
1. main_login.php
2. checklogin.php
3. login_success.php
Step
1. Create table "members" in database "test".
2. Create file main_login.php.
3. Create file checklogin.php.
4. Create file login_success.php.
5. Create file logout.php If you don't know how to create databse, click here | |
| Create table "members" | | CREATE TABLE `members` (
`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 `members` VALUES (1, 'john', '1234'); |
| | | |
| Create file main_login.php | | View In Browser
| ############### Code | | | | |
| Create file checklogin.php | | | ############### Code
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
| | | | |
| Create file login_success.php | | ############### Code
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
Login Successful
| | | | |
| Logout.php | | If you want to logout, create this file
session_start();
session_destroy();
?>
| | | | |
| For PHP5 User - checklogin.php | | ############### Code
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
} ob_end_flush();
?>
| | | | |
| Encrypting Password - Make your Login More Secure |
|
|