Simple login application using php:Login application using php and mysql
Use the following steps create database called ‘testlogin’ and execute the following query
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE IF NOT EXISTS `tbl_login` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; -- -- Dumping data for table `tbl_login` -- INSERT INTO `tbl_login` (`id`, `username`, `password`) VALUES (1, 'admin', 'admin'); |
please save the below files into a folder under htdocs. index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<html> <head> <title>Simple Login using php-session</title> <script type="text/javascript"> function validate() { if(document.form1.username.value=='') { alert("please enter the user name"); document.form1.username.focus(); return false; } if(document.form1.password.value=='') { alert("please enter the password"); document.form1.password.focus(); return false; } return true; } </script> </head> <body> <form method="post" action="postdata.php" id="form1" name="form1" autocomplete="off"> <table border="1" cellspacing="0" cellpadding="0" align="center" width="350"> <tr> <td width="1px">User Name:</td> <td width="1px"><input type="text" name="username" value=""></td> </tr> <tr> <td width="1px">Password</td> <td width="1px"><input type="password" name="password" value=""></td> </tr> <tr> <td width="1px"></td> <td width="1px"><input type="Submit" name="Submit" value="Login" onclick="return validate();"></td> </tr> <tr> <td width="1px"></td> <td width="1px"><div style="text-color=red;"><?php echo @$_GET['msg'];?></div></td> </tr> </table> </form> </body></html> |
database.php
1 2 3 4 5 6 7 8 |
<?php $connection_string = mysql_connect('localhost', 'root', ''); if (!$connection_string) { die('Could not connect: ' . mysql_error()); } mysql_select_db('testlogin', $connection_string) or die('Could not select database.'); ?> |
postdata.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php session_start(); include"database.php"; $user=@$_POST['username']; $password=@$_POST['password']; $query="select * from tbl_login where username='$user' and password='$password'"; $sql_result=mysql_query($query,$connection_string); $num_rows=mysql_num_rows($sql_result); if($num_rows>0) { @$_SESSION['userdetails']=$user; header("Location:admin.php"); } else { $errormessage="Login failed"; header("Location:index.php?msg=$errormessage"); } ?> |
admin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php session_start(); if(@$_SESSION['userdetails']=="") { header("Location:index.php"); } ?> <html> <head> <title>Simple Login using php and mysql</title> </head> <body> <table border="1" cellspacing="0" cellpadding="0" align="center" width="350"> <tr> <td width="1px">Welocome : <?php echo $_SESSION['userdetails'];?></td> <td width="1px"></td> </tr> <tr> <td width="1px"><a href="logout.php">Logout</a></td> <td width="1px"></td> </tr> </table> </body></html> |
logout.php
1 2 3 4 5 6 7 |
<?php session_start(); session_destroy(); session_unset(); unset($_SESSION['userdetails']); header("Location:index.php"); ?> |