PHP Login validation and logout Snippet
Verify.php
Login.php
- This is the login HTML page wit a Username and Password text field and a submit button
<form action="verify.php" method="post" > <tr> <td>UserName :</td><td><input type="text" name="username" value=""/></td></tr> <tr> <td>Password:</td><td><input type="password" name="password" value="" /></td></tr> <tr><td /><td><input type="submit" name="login" value="Login" /></td></tr> </form> |
Verify.php
- This is the PHP script specified in the Form Action of the HTML Page,whose primary purpose is to authenticate a user is a verified user or not.
<?php if(isset($_POST['login'])){ $dbHost = "localhost"; //Location Of Database usually its localhost $dbUser = "root"; //Database User Name $dbPass = ""; //Database Password $dbDatabase = ""; //Database Name $db = mysql_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database."); //Connect to the databasse mysql_select_db($dbDatabase, $db)or die("Couldn't select the database."); //Selects the database The Above code can be in a different file, then you can place include'filename.php'; instead. $usr = mysql_real_escape_string($_POST['username']); $pas = mysql_real_escape_string($_POST['password']); $sql = mysql_query("SELECT * FROM login WHERE username='$usr' AND password='$pas' LIMIT 1"); if(mysql_num_rows($sql) == 1){ $row = mysql_fetch_array($sql); session_start(); $_SESSION['username'] = $row['username']; $_SESSION['logged'] = TRUE; echo "Login Successful"; // Modify to go to the page you would like exit; }else{ $_SESSION['logged']= FALSE; echo "Login Failed"; exit; } }else{ //If the form button wasn't submitted go to the index page, or login page header("Location: index.html"); exit; } ?> |
Logout.php
- This is the logout PHP Script used to unset and destroy the created Session
<?php//Start sessionsession_start();//Unset the variables stored in sessionunset($_SESSION['username']);unset($_SESSION['logged']);session_destroy();//unset($_SESSION['SESS_LAST_NAME']);?>
- Note: Start the session again before destroying the variables in the logout page else an error will be specified
Include these in the your html pages you need to use.Cheers !
Thank u for reading this Article hope it was useful ! -Regards ScriptsSnippets
0 comments:
Post a Comment