Cookie-based Login Form in PHP
Cookie-based Login Form in PHP
Let’s see the code of “Cookie-based login form”.
The HTML code describes how will the page will look. To get the ID and password from the user we use forms. Then we post the data on to the logincookiebased.php file.
Program of Cookie-based Login Form in PHP
Let’s see the program of “Program of Cookie-based Login Form in PHP”.
We need to code two files.
- index.html file
- logincookiebased.php
Code of index.html file
Let’s see the code of “Code of index.html file”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <html> <head> <title>Login Page</title> </head> <body> <form name="1stform" method="POST" action="logincookiebased.php"> <table> <tr> <td colspan="3"> <div align="center"><b>Enter ID and Password.</b></div> </td> </tr> <tr> <td> <div align="left">ID</div> </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> <div align="left">Password</div> </td> <td> <input type="pass" name="pass"> </td> </tr> <tr> <td colspan="3"> <center> <input type="submit" name="Submit" value="Login"> </center> </td> </tr> </table> </form> </body> </html> |
Code of logincookiebased.php
Let’s see the code of “logincookiebased.php file”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php $CurrentTime = getdate(); $SaveTime= $CurrentTime["weekday"] . " " . $CurrentTime["month"] ." " . $CurrentTime["year"] ; $SaveTime.=" Time : "; if ($CurrentTime["hours"] < 10) { $SaveTime.= "0" . $CurrentTime["hours"]; } else { $SaveTime.= $CurrentTime["hours"]; } $SaveTime.= ":"; if ($CurrentTime["minutes"]<10) { $SaveTime.= "0" . $CurrentTime["minutes"]; } else { $SaveTime.= $CurrentTime["minutes"]; } $SaveTime.= ": "; if ($CurrentTime["seconds"] <10) { $SaveTime.= "0" . $CurrentTime["seconds"]; } else { $SaveTime.= $CurrentTime["seconds"]; } if (isset($data)) { $count=++$data[l]; setcookie("data[0]",$SaveTime,time() + (60*60*24)); setcookie("data[l]", $count,time() + (60*60*24)); setcookie("data[2]",$name,time() + (60*60*24)); echo "<b><center>Hello " . $data[2] . " ! !</center></b><br>\n"; echo "<b><center>Previous Login Time :" .$data[0] . "</center></b><br>\n"; echo "<b><center>Date :" .$SaveTime. "</center></b><br>\n"; echo "<b><center>Viewed pages count:" . $data[l]. "</center></b><br>\n"; echo "<b><center>logged in! successfully</center></b>"; echo ("<b><center>Now you can open this page without having to enter a password for the next 24 hours.</center></b>"); } else { if (isset($name) && isset($pass)) { if ($pass=="superpass") { $count=0; setcookie("data[0]",$SaveTime,time() + (60*60*24)); setcookie("data[l]",$count,time() + (60*60*24)); setcookie("data[2]",$name,time() + (60*60*24)); $url="Location: cookieimp.php"; header($url); }else{ echo "<hl><center>WRONG PASSWORD!!!</center></hl>"; } } } ?> |
Topic Covered
Cookie-based Login Form in PHP.