Who's Online
We have 7 guests onlineBreadcrumbs
| Login Script |
PHP Login script tutorial
Learn to create a simple login system with PHP + MySql script, this tutorial easy to follow, teach you step by step.
In this tutorial create 3 files
CREATE TABLE `members` ( -- INSERT INTO `members` VALUES (1, 'john', '1234'); This is how table looks in database: ![]()
Code: <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
This is how it looks in browser
Code: <?php $host="localhost";// Host name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); // username and password sent from form $myusername=$_POST['myusername']; // To protect MySQL injection $myusername = stripslashes($myusername); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); ?>
<?php ob_start(); // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; // To protect MySQL injection $myusername = stripslashes($myusername); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); ob_end_flush(); ?>
Code: // Check if session is not registered , redirect back to main page. <?php session_start(); ?>
If you want to logout, create this file Code: // Put this code in first line of web page. <?php session_start(); ?>
|
