How to Easily Make an Advance Level Login system

Tutorials

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

1 <form class="login_form" method="post" action="index.php">
2     <div>Username: <input type="text" name="username" /></div>
3     <div>Password: <input type="password" name="password" /></div>
4     <div><input type="submit" value="Login" name="Login" /></div>
5 </form>
6 <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

1 .login_form {
2     border1px solid #AAA;
3     padding:10px;
4 }

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

01 // initialization of login system and generation code
02 $oSimpleLoginSystem new SimpleLoginSystem();
03 echo $oSimpleLoginSystem->getLoginBox();
04 // class SimpleLoginSystem
05 class SimpleLoginSystem {
06     // variables
07     var $aExistedMembers// Existed members array
08     // constructor
09     function SimpleLoginSystem() {
10         $this->aExistedMembers = array(
11             'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4',  //Sample: MD5('qwerty')
12             'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4'
13         );
14     }
15     function getLoginBox() {
16         ob_start();
17         require_once('login_form.html');
18         $sLoginForm = ob_get_clean();
19         $sLogoutForm '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>';
20         if ((int)$_REQUEST['logout'] == 1) {
21             if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass']))
22                 $this->simple_logout();
23         }
24         if ($_REQUEST['username'] && $_REQUEST['password']) {
25             if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password']))) {
26                 $this->simple_login($_REQUEST['username'], $_REQUEST['password']);
27                 return 'Hello ' $_REQUEST['username'] . '! ' $sLogoutForm;
28             else {
29                 return 'Username or Password is incorrect' $sLoginForm;
30             }
31         else {
32             if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) {
33                 if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) {
34                     return 'Hello ' $_COOKIE['member_name'] . '! ' $sLogoutForm;
35                 }
36             }
37             return $sLoginForm;
38         }
39     }
40     function simple_login($sName$sPass) {
41         $this->simple_logout();
42         $sMd5Password = MD5($sPass);
43         $iCookieTime = time() + 24*60*60*30;
44         setcookie("member_name"$sName$iCookieTime'/');
45         $_COOKIE['member_name'] = $sName;
46         setcookie("member_pass"$sMd5Password$iCookieTime'/');
47         $_COOKIE['member_pass'] = $sMd5Password;
48     }
49     function simple_logout() {
50         setcookie('member_name''', time() - 96 * 3600, '/');
51         setcookie('member_pass''', time() - 96 * 3600, '/');
52         unset($_COOKIE['member_name']);
53         unset($_COOKIE['member_pass']);
54     }
55     function check_login($sName$sPass) {
56         return ($this->aExistedMembers[$sName] == $sPass);
57     }
58 }

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!

Rate article