Friday, November 5, 2010

Create a very simple login session using PHP _session

1.  Run SQL script below in MySQL to create users table to :


CREATE TABLE `users` ( `id` INT UNSIGNED NOT NULL 
AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 20 ) NOT NULL ,
`password` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM;


2. Create connect.php to connect to database

<?php
     session_start();
//use @ to ignore warning message
     @mysql_connect('localhost', 'root', 'mypassword') or die("Unable to Connect to the MySQL Database at this time");;
     @mysql_select_db('userlogin') or die("Unable to Select the MySQL Database at this time");
 
  ?>

3. Create newsession.php to keep login active for a while 
<?php
$_SESSION['id']=session_id();
$_SESSION['userid']=$r_username;
?>


4.  create logincheck.php

<?php
include "connect.php";
if(isset($_REQUEST['submit']))
{
$r_username = @mysql_real_escape_string($_REQUEST['username']);
$r_password = @mysql_real_escape_string($_REQUEST['password']);

$query = @mysql_query("SELECT * FROM `users`
WHERE `username` = '".$r_username."'
AND `password` = '".$r_password."'");

if(@mysql_num_rows($query) > 0)
{
include "newsession.php" }
else {

      session_unset();

echo "Use your correct  UserName and Password and Try";
    }

?>
5. Final login.php
<?php
     session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Login Page</title>
</head>
<form action='logincheck.php' method=post>
 UserName
<input type ='text'  name='username' ><br>
password
<input type ='password'  name='password' > <br>
 <input type="submit" name="submit" value="Login" /> <input type='reset' value='Reset'>
</form>
<a href='signup.php'>New Member Sign UP</a>
</body>
</html>
The display of the results after modification can be found Here

No comments:

Post a Comment