Creating a Modern Looking Animated Login System in PHP

Tutorials

Login system with PHP. Today we will continue PHP lessons, and our article will about creating modern php login system. Possible you already saw similar ways to display login forms, and today we will repeat this by self. In result – it will some small element in your page layout, and after clicking on it – will appear some area, where we will see some welcome message, login form and another useful information. All very user friendly. So, its time to try demo.

Here are sample and downloadable package:

Live Demo

[sociallocker]

download in package

[/sociallocker]


Ok, download the example files and lets start coding !


Step 1. HTML

As usual, we start with the HTML. This is main page code:

main_page.html

01 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
02 <head>
03     <link rel="stylesheet" href="css/main.css" type="text/css" media="all" />
04     <script src="js/jquery-1.4.4.min.js" type="text/javascript"></script>
05 </head>
06 <body>
07     <div class="example" id="main">
08         {login_form}
09     </div>
10 </body>
11 </html>

As we can see – all pretty easy here. I just put one key {login_form} here, which we will using after.

login_form.html

01 <div class="bar">
02     <a href="#" id="my_userarea">Log In</a>
03 </div>
04 <div style="display: none;" id="my_panel">
05     <div class="column">
06         <h3>My website title</h3>
07         <p>My website description. Our website was created for anybody. Here we can read recent news, tutorials, and find many useful information. Welcome !</p>
08         <p><a class="more" href="#about.php" rel="nofollow"><span>Read more</span></a></p>
09     </div>
10     <div class="column">
11         <form class="login_form" action="index.php" method="post">
12             <h3>Log In</h3>
13             <label>Nickname:</label><input type="text" name="username">
14             <label>Password:</label><input type="password" name="password">
15             <input type="submit" name="LogIn" value="Login">
16             <a class="more" href="#forgot.php" rel="nofollow"><span>Forgot password?</span></a>
17         </form>
18     </div>
19     <div class="column">
20         <h3>Still not our member? Join us!</h3>
21         <p>Which gives us a registration? This gives us opportunity to become full member of our website. You can communicate with another member etc..</p>
22         <p align="center"><a class="more" href="#registration.php" rel="nofollow"><span>Registration</span></a></p>
23     </div>
24     <div style="clear:both"></div>
25     <hr>
26     <div class="close"><a id="my_close" class="my_close">Hide this panel</a></div>
27 </div>
28 <script language="javascript" type="text/javascript">
29 $(document).ready(function() {
30     $('#my_userarea').click(function(){
31         $('div#my_panel').slideToggle('slow');
32         return false;
33     });
34     $('#my_close').click(function(){
35         $('div#my_panel').slideUp('slow');
36         return false;
37     });
38 });
39 </script>
40 <h3>You can use username "User1" of "User2" and password "qwerty" to login in system</h3>

This file is our floating login form with 3 columns. And, last one file – logout:

logout_form.html

1 <div class="bar">
2     <a href="index.php?logout" id="my_userarea">Log Out</a>
3 </div>

Step 2. CSS

Here are used CSS styles:

css/main.css

01 body{background-color:#888;margin:0;padding:0}
02 .example{position:relative;width:980px;height:700px;background-image:url(../images/background.jpg);border:1px #000 solid;margin:20px auto;padding:20px;-moz-border-radius:3px;-webkit-border-radius:3px}
03 .bar{background-color:#444;height:24px;padding:10px}
04 a#my_userarea{font-size:12px;position:relative;display:block;overflow:hidden;color:#909090;-webkit-border-radius:11px;-moz-border-radius:11px;text-decoration:none;border-radius:11px;background:url(../images/button-user-area.gif) repeat-x 0 0;text-shadow:#000 1px 1px;float:right;padding:4px 10px 3px}
05 #my_panel{background-color:#272727;border:1px solid #444;color:#999;font-size:.85em;z-index:1001;padding:15px;position:absolute;width:948px}
06 #my_panel .column{float:left;width:30%;padding:15px}
07 #my_panel .column h3{color:#fff}
08 #my_panel .login_form input,#my_panel .login_form label{margin-bottom:5px;display:block}
09 #my_panel input[type=text],#my_panel input[type=password]{width:200px}
10 #my_panel input[type=submit]{background:url(../images/button.png) no-repeat scroll right 0 transparent;width:72px;height:30px;color:#fff;border-width:0}
11 .more{background:url(../images/more-left.png) no-repeat scroll 0 0 transparent;cursor:pointer;display:block;float:right;padding-left:11px;text-align:center;text-decoration:none}
12 .more span{background:url(../images/more-right.png) no-repeat scroll right 0 transparent;color:#fff;display:block;height:30px;line-height:29px;padding:0 19px 0 8px}
13 .more:hover{text-decoration:none;background-position:0 bottom}
14 .more:hover span,#my_panel input[type=submit]:hover{background-position:right bottom}
15 #my_panel .close{text-align:center}
16 #my_panel .close a{color:#07BBE2;cursor:pointer}

Step 3. JS

For our demo I require only jQuery for sliding effect – nothing more 🙂

js/jquery-1.4.4.min.js

Step 4. PHP

Our main part of project – login functionality. Commonly, we will use our old system, but with new changed of course.

index.php

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

Our system working with cookies, we will check cookies data to determinate, are member logged or not. Of course, you can have your own login system if you already using some own CMS. But you always can use our login system too. One point, this is not necessary to keep whole array of members directly in this file. Possible better solution will keep it separated or in database as example.


Live Demo

Conclusion

Today we prepared new nice login system which you can use in your projects. If this was useful for you – do not forget to thank us. I would be grateful for your interesting comments. Good luck!

Rate article