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:
[sociallocker]
[/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.
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 > |
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
2 | border : 1px solid #AAA ; |
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
02 | $oSimpleLoginSystem = new SimpleLoginSystem(); |
03 | echo $oSimpleLoginSystem ->getLoginBox(); |
05 | class SimpleLoginSystem { |
09 | function SimpleLoginSystem() { |
10 | $this ->aExistedMembers = array ( |
11 | 'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4' , |
12 | 'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4' |
15 | function getLoginBox() { |
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(); |
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 ; |
29 | return 'Username or Password is incorrect' . $sLoginForm ; |
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 ; |
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 ; |
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' ]); |
55 | function check_login( $sName , $sPass ) { |
56 | 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!