Note: Download the script Below
Are you looking for a very simple login script which does not interact with the database?.
No database used in this script. This script is useful if you want to develop a simple admin page / report page.
The workflow of the script is like True or False concept. If the session value is present then show the secret page or go to the login page!
Here is the entire login and logout script!
<?php //simple PHP login script using Session //start the session * this is important session_start(); //login script if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'login'){ //give your login credentials here if($_REQUEST['uname'] == 'admin' && $_REQUEST['pass'] == 'admin123') $_SESSION['login_user'] = 1; else $_SESSION['login_msg'] = 1; } //get the page name where to redirect if(isset($_REQUEST['pagename'])) $pagename = $_REQUEST['pagename']; //logout script if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'logout'){ unset($_SESSION['login_user']); header('Location:login.php'); } if(isset($_SESSION['login_user'])){ if(isset($_REQUEST['pagename'])) header('Location:'.$pagename.'.php'); else header('Location:index.php'); }else{ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Simple PHP Login Script using Session</title> </head> <body> <table width="850" border="0" align="center" cellpadding="0" cellspacing="0"> <tr><td> </td></tr> <tr><td> <form name="login_form" method="post" action=""> <table align="center" style="background-color:#fff;"> <tr><td colspan="2" align="center"><strong>Login Details</strong></td></tr> <tr><td>Username</td><td><input type="text" name="uname" id="uname"></td></tr> <tr><td>Password</td><td><input type="password" name="pass" id="pass"></td></tr> <tr> <td colspan="2" align="center"> <p style="color:red;"> <?php //display the error msg if the login credentials are wrong! if(isset($_SESSION['login_msg'])){ echo 'Wrong username and password !'; unset($_SESSION['login_msg']); } ?> </p> </td></tr> <tr><td align="center" colspan="2"><input type="submit" value="Login"></td></tr> </table> <input type="hidden" name="ch" value="login"> </form> </td></tr> </table> <?php } ?> </body> </html>
How to use this script?.
Save the above code as ‘login.php’ . Then just use the below lines of code in any of your php page!
<?php session_start(); //check logged in or not! if(!isset($_SESSION['login_user'])){ header('Location:login.php?pagename='.basename($_SERVER['PHP_SELF'], ".php")); } ?> <html> <head><title>Simple PHP Login Script Using Session - Index page</title></head> <table width="850" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> <div style="float:left;"><a href="login.php?ch=logout">Logout</a></div> </td> </tr> <tr> <td> <h1>Your Content will come here!</h1> </td> </tr> </table> </head> </html>
You can download this script from the below link and can use it in any of your project immediately!.
Here is another advanced tutorial – jQuery Ajax Login and Signup form – PHP Script!
how to add new users because the above code specifies username and password in the login.php
This script is not for multiple user!. Anyway If you want to use more than one, you can add one more username and pass in the if condition like this!
if(($_REQUEST[‘uname’] == ‘admin’ && $_REQUEST[‘pass’] == ‘admin123’) or ($_REQUEST[‘uname’] == ‘gunju’ && $_REQUEST[‘pass’] == ‘gunju123’))
Thank you so much!!!
Thank you so much for this code working without a database.
I will add a multiple user function through an array of [user,pass].
I will use it in my little wiki opensource project alphawiki.
Alain Marty
How do you get it so once you logon it redirects you to another page?
remove line no. 26 to 28.
change this line
header(‘Location:index.php’);
to
header(‘Location:yourpage.php’);
thats very nice
Thanks for this – works perfectly. A smal question – how long does the session last?
It depends on the server configuration. Minimum 20 to 30 minutes.
how can i check the user and password from database?
How can you echo the username?
store the $_REQUEST[‘uname’] in a session like this
$_SESSION[‘username’] = $_REQUEST[‘uname’];
then print it in any file
echo $_SESSION[‘username’];
Thank you but that doesn’t work. Started the session at the top of the page and echo’d what you pasted. Just doesn’t show up.
When i run this in wamp the code runs fine but when i run it on my server live it returns a blank page instead. any ideas why?
Hi Dave, There is no special function used in this code. Have you tried any other php script in your server?. Is that working?.
your are genius my dear.
tnx.U too much.
Hi Please let me know how to use more than one User & password
Can you explain to me how to echo the username in another file? For example, at the admin panel.
Hi, Store the username in a session
$_SESSION[‘uname’] = $_REQUEST[‘uname’]; (in this example may be after line no 11)
echo this $_SESSION[‘uname’] wherever you want.
Thank you very much! This tutorial is just awesome!
The script I have downloaded a while ago and since my service provider migrated to PHP 5.4.4 it does no longer work. After the initial login, witch works, it shows “This web page has a redirect loop” at the second page I call up.
Hi, I have tested with 5.5 and even this site is running with 5.4.2, I don’t have any problem. If you think version is the problem try this https://www.tutorialsmade.com/switch-php-version-5-2-5-3-htaccess/
Thank you I was getting frustrated because I couldn’t find a login script that worked!