Site icon T4Tutorials.com

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.

  1. index.html file
  2. logincookiebased.php

Code of index.html file

Let’s see the code of “Code of index.html file”.

<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”.

<?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.