4. Creating Login Page (logon.php)
Login page is quite simple. It has 3 basic elements
a. Username Field
b. Password Field
c. Submit button
You can add “remember me” option if u like..
|
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 |
<!--<span class="hiddenSpellError" pre=""-->DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login Demo</title> </head> <body> <h1>Members Login</h1> <!--?<span class="hiddenSpellError" pre=""--><?php if(isset($_GET['msg'])) { echo "<h4>".$_GET['msg']."</h4>"; } ?> <form action="Login.php" method="post" name="login_form" id="login_form"> <label>Username : </label><input name="username" type="text" id="username" size="50" maxlength="50" /> <label>Password : </label><input name="password" type="password" id="password" size="50" maxlength="32" /> <input name="rememberme" type="checkbox" id="rememberme" /><label>Remember me</label> <input name="submit" type="submit" id="submit" value="Login" /> </form> </body> </html> |
5. The Login Script (login.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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<!--?<span class="hiddenSpellError" pre=""--><?php session_start(); // to use sessions Variables IMPORTANT // Connects to your Database mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("db_name") or die(mysql_error()); // Makes sure all are filled it in if(!$_POST['username'] || !$_POST['password']) { header('Location: logon.php?msg=You did not fill in a required field.'); } // Checks username in the database if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $check_users = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error()); //Gives error if user dosenot exist $users_found = mysql_num_rows($check_users); if ($users_found == 0) { header('Location: logon.php?msg=That user does not exist in our database. <a href="register.php">Click Here to Register</a>'); } while($user_info = mysql_fetch_array( $check_users )) { $_POST['password'] = stripslashes($_POST['password']); $user_info['password'] = stripslashes($user_info['password']); $_POST['password'] = md5($_POST['password']); //gives error if the password is wrong if ($_POST['password'] != $user_info['password']) { header('Location: logon.php?msg=Incorrect password, please try again.'); } else { // if login is ligitimate we update the SESSION Variables $_POST['username'] = stripslashes($_POST['username']); $_SESSION['website_userid']=$_POST['username']; $_SESSION['website_key']=$_POST['password']; $_SESSION['isLoggedIn']='1'; // if the user choosed the 'remember me' option , we set the cookies if(isset($_POST['rememberme']) && $_POST['rememberme']=='1') { // remembers the user for a year $hour = time() + (356*24); setcookie('website_userid', $_SESSION['website_userid'], $hour); setcookie('website_key', $_SESSION['website_key'], $hour); } //then redirect them to the members area header("Location: members.php"); } } // end of while loop ?> |
This login script authenticates
users to access the restricted Members Area. and also sets $_COOKIES and $_SESSION variables for future use..
6. Members Area (members.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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php session_start();?> <!--<span class="hiddenSpellError" pre=""-->DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Members Areas</title> </head> <body> <?php // Connects to your Database mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("db_name") or die(mysql_error()); //checks cookies and Session if(isset($_COOKIE['website_userid']) || (isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']=='1') ) { // If the user SESSION is still valid or he just logged in if(isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']=='1') { $username = $_SESSION['website_userid']; $pass = $_SESSION['website_key']; } // if the user had chosen the 'remember me' option else { $username = $_COOKIE['website_userid']; $pass = $_COOKIE['website_key']; } if (!get_magic_quotes_gpc()) { $cookie_username=addslashes($username); // makes Cookie sql injection impossible $cookie_pass=addslashes($pass); } // Checks with cookie data $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); // Fetches data if the User exists using either SESSION data or Cookie data while($user_info = mysql_fetch_array($check) ) { // First Checks if the cookie has the incorrect password, they are directed to the logon page if ($pass != $user_info['password']) { header('Location: logon.php?msg=Please login with your new password'); } //otherwise Members Content are displayed else { echo '<h1>Members Area</h1>'; echo '<p>Welcome '.$username.'</p>'; echo '<br /> <a href="logout.php">Logout</a>'; } } // end of while loop } // end of if condition to check cookies and sessions else //if both cookie or the Session does not exist, then .. they are directed to the logon screen { header("Location: logon.php?msg=Login First!"); } ?> </body> </html> |
This is the page you want only the authenticated users to access. In this page the cookies and Sessions are first checked and if the credentials are legit then the Members Area Contet is displayed or else it redirects the the visitors to the logon page.
7. Logout Script (logout.php)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php session_start(); $past = time() - 100; //this makes the time negative to destroy the cookie setcookie('website_userid', xOx, $past); setcookie('website_key', xOx, $past); // this destroyes all the session variables session_destroy(); header("Location: logon.php?msg=Logged out successfully"); ?> |
This Script destroys all the $_SESSION variables and unsets all the $_COOKIES to successfully
logout the user from the website..


