How to Make an Advance Level Login system Easily with PHP
Today I will tell you about something new. This will useful and simple login system based on COOKIES. So we will store few info about logged member directly in cookies of browser. This technology help us to remember member
Here is demo:
Live Demo
[sociallocker]
download package
[/sociallocker]
So download the example files and lets start coding!
Step 1. HTML
As usual, we start with the HTML. This is login form code.
login_form.html
<form class="login_form" method="post" action="index.php"> <div>Username: <input type="text" name="username" /></div> <div>Password: <input type="password" name="password" /></div> <div><input type="submit" value="Login" name="Login" /></div> </form> <div>You can use username "User1" of "User2" and password "qwerty" to login in system</div>
Step 2. CSS
Here are used CSS styles.
styles.css
.login_form { border: 1px solid #AAA; padding:10px; }
Step 3. PHP
Now that we followed all the code on the front-end, it is now time for the last part of this tutorial – the PHP back-end.
As we can see – this is just pure class with functions for login system.
I used next functions:
getLoginBox – function return login form. In case if member logged – it will return Hello member record and possibility for logout
simple_login – perform login to system (storing necessary information in cookies)
simple_logout – perform logout (clearing used cookies)
check_login – return true in case if username and password exists in system
index.php
// initialization of login system and generation code $oSimpleLoginSystem = new SimpleLoginSystem(); echo $oSimpleLoginSystem->getLoginBox(); // class SimpleLoginSystem class SimpleLoginSystem { // variables var $aExistedMembers; // Existed members array // constructor function SimpleLoginSystem() { $this->aExistedMembers = array( 'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4', //Sample: MD5('qwerty') 'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4' ); } function getLoginBox() { ob_start(); require_once('login_form.html'); $sLoginForm = ob_get_clean(); $sLogoutForm = '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>'; if ((int)$_REQUEST['logout'] == 1) { if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass'])) $this->simple_logout(); } if ($_REQUEST['username'] && $_REQUEST['password']) { if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password']))) { $this->simple_login($_REQUEST['username'], $_REQUEST['password']); return 'Hello ' . $_REQUEST['username'] . '! ' . $sLogoutForm; } else { return 'Username or Password is incorrect' . $sLoginForm; } } else { if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) { if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) { return 'Hello ' . $_COOKIE['member_name'] . '! ' . $sLogoutForm; } } return $sLoginForm; } } function simple_login($sName, $sPass) { $this->simple_logout(); $sMd5Password = MD5($sPass); $iCookieTime = time() + 24*60*60*30; setcookie("member_name", $sName, $iCookieTime, '/'); $_COOKIE['member_name'] = $sName; setcookie("member_pass", $sMd5Password, $iCookieTime, '/'); $_COOKIE['member_pass'] = $sMd5Password; } function simple_logout() { setcookie('member_name', '', time() - 96 * 3600, '/'); setcookie('member_pass', '', time() - 96 * 3600, '/'); unset($_COOKIE['member_name']); unset($_COOKIE['member_pass']); } function check_login($sName, $sPass) { return ($this->aExistedMembers[$sName] == $sPass); } }
Conclusion
I described one of easy and useful login system based on cookies. You can use this material to create own login systems into your startups. Good luck !
…. a tutorial for easily making an advance level login system…. cool !! Thanks Ayaz for sharing this amazingly helpful tutorial blog…. “Script-Tutorials.com” and I thank this site for all the really helpful help !! :)
I believe open id and fconnect is the way to go.
Very helpful! Thank you! Looking forward to other “Script Tutorials.” I also like the way you have included a demo of the things you have covered in the tutorial!
Dave
Thanks guys for comments :)
Yes, OpenID and OAuth is good technologies too, but it will more difficult for first reading. But hope that I will create such articles too.
This is great!
A little stuck on setting the password though.
‘User1’ => ‘d8578edf8458ce06fbc5bb76a58c5ca4’, //Sample: MD5(‘qwerty’)
How do you set your own password?
Any help greatly appreciated!
Hi Glen,
It is already written in comments, haven’t you noticed? If you want to get a hash code of your custom password, you can just print it on screen:
echo md5(‘my_custom_password’);
after- just put the echoed password into this array
how do you send the user to a different page when they log in (making the button a link doesn’t work).
Hi finty,
What ‘different page’ exactly? Do you want to redirect members once they login?
Dear Andrew,
Thank you for your great tutorial, I am looking forward using more of this great stuff. Can you help me out a bit, I would like to redirect the user after the login to somekind of a homefolder. Can i realise that with easy. Thanks in advance!
Hi Maurits,
Yes, of course, you may relocate your members to any page you need after they have logged in. You may use the ‘header’ PHP command to relocate them.
Hey! i like your code, but how do i put the login system ON MY WEBSITE, sure you gave me the code to MAKE it but how do i actually put it on my website??!! (i know its kind of a noobie question) i just cant figure that out everyone shows you HOW to make it but not how to put on your website. Please help me i’ve been searching the web for months but still can’t find an answer. REPLY QUICKLY PLEASE
Hello hashmat,
You will need to download the most important (to login system) files from your website, and integrate necessary codes into these files, finally – upload your changed files back to the server.
This is the best tutorial that I have seen so far to explain each part of the process. And I think I could probably add this to my site. But first I would like to ask, how would I create a button that allows users to create accounts on the website?
Hi,
I recommend that you check other our tutorials – because the registration system was already explained there.
andery can you post tutorial for how to let users edit their profile page and save them…
as we save and update our bio to social websites??
also sign up page with sending confirmation link
Hi Sudhanshu,
Yes, this is possible, but please send me this request using the contact us form below.
I want each user to go to a specific page how do I do that?
Thanks
E
Hi E,
In order to achieve your request – you can customize the ‘simple_login’ function