PHP Mysql Login Script
PHP Mysql Login Script
Simple PHP Mysql Login Script
. This login script can check whether user exist in the MySQL database, Create session and redirect user to the secure page. If session is not set it will automatically redirect to login page. I have also added a logout page where users can logout and the system will automatically clear user sessions.
First step in the process is to create a database named userlogin. In that we have 3 columns named ID, username and password. I am also inserting a test data to the database so that we can check whether the script is working as expected.
Our 1st step is to Create table “mydata” inside database “userlogin”.
———————————————————————–
CREATE DATABASE userlogin; USE userlogin; CREATE TABLE mydata ( id INT(4) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )
Inserting data for table `mydata`
INSERT INTO mydata (username, password) VALUES ('user1', 'pass123'); INSERT INTO mydata (username, password) VALUES ('user2', 'pass345');
Next step is to create an HTML file with the login fields.
Create file main_login.html.
———————————————————————–
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"/></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="password" id="mypassword"/></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"/></td> </tr> </table> </td> </form> </tr> </table>
———————————————————————–
Now we have the database and login page ready.
Next step in the process is to create a PHP files which has the ablity to check the username and password submitted from the HTML file. Create sessiong and redirect user to login page. Also remove session variables once the user is logged out. Each line of code below is properly commented for your convenience.
Create a file named checklogin.php and add the following codes
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Your Mysql password $db_name="userlogin"; // Database name $tbl_name="mydata"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect 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); // 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_start(); $_SESSION["user"] = $myusername; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?>
———————————————————————–
Create file login_success.php.
———————————————————————–
<?php session_start(); echo 'Welcome '.$_SESSION["user"]; //If session is not set if(!isset($_SESSION['user'])) { header("location:main_login.html"); } ?> <html> <body> You Have Successfully Logged In.<br /> <a href="logout.php">Log Out </a></body> </html> </php] ———————————————————————– Create file logout.php ———————————————————————– [php] <?php session_start(); //Remove all sessions and redirect to login page session_destroy(); header("location:main_login.html"); ?>
That is all you are done. Check this Demo on how this script works.
Note: I have updated this tutorial which was posted by us back in 2009. Some of the parameters like session_register used then has been depreciated by PHP. Hence I thought of updating this tutorial with more details with the depreciated part removed. The code above has been tested and everything is working as expected. I have also included a Demo for your convenience. Happy coding 🙂 .
PHP Mysql Login Script,Incoming search terms:
- inurl:/index php?action=register (48)
- inurl:login php if you do not have an account yet you may register here (36)
- simple php login script (26)
- inurl:index php?do=cat (26)
- submariner forum inurl:/index php?act=login intext:powered by invision power board (25)
- login php?register=1 plural (20)
- inurl:/subaction=userinfo (15)
- inurl:/wp-login php?action=register (14)
- racket intitle:new elgg site (12)
- inurl:index php?subaction=userinfo (12)
- login php?register=1 (10)
- inurl:member_excel php (9)
- New Elgg site (5)
- inurl:/index php?a=register (4)
- @papop com loc:US (3)
- make a simple loginscript with javascript and database (1)
- intext: powered by elgg (1)
- php simple login form to local host html (1)
- inurl:/member php?id= (1)
- Create file main_login php (1)
- simple secure login php script with database for website (1)
- inurl:/index php?Action= (1)
a good one..
Good a very simple script for loging in using php….nice
very simple login script i have ever seen..
gud one
Work very fine and it’s was so easy to use !
Thank’s a lot !!!
this login script is very helpful for new visitor must see this code
gud one!
TQ
it’s really awesome. i have never seen before…
Thanks for easy script
whaaaaaaaaaaa………….
your a Genius….. Thnks to made us life easier…..
First and easy script.