login.php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of login.php.php

  1. <?php session_start(); /* Starts the session */
  2.  
  3. /* Check Login form submitted */if(isset($_POST['Submit'])){
  4.        
  5.                 /* Define username and associated password array */
  6.                 $logins = array('Dean' => '123456','username1' => 'password1','username2' => 'password2');
  7.  
  8.                 /* Check and assign submitted Username and Password to new variable */
  9.                 $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
  10.                 $Password = isset($_POST['Password']) ? $_POST['Password'] : '';
  11.  
  12.                 /* Check Username and Password existence in defined array */
  13.                 if (isset($logins[$Username]) && $logins[$Username] == $Password){
  14.                        
  15.                         /* Success: Set session variables and redirect to Protected page  */$_SESSION['UserData']['Username']=$logins[$Username];
  16.                         header("location:index.php");
  17.                         exit;
  18.                         } 
  19.                        
  20.                 else {
  21.                         /*Unsuccessful attempt: Set error message */
  22.                         $msg="<span style='color:red'>Invalid Login Details</span>";
  23.                         }
  24.         }
  25. ?>
  26.  
  27. <html>
  28.  
  29. <form action="" method="post" name="Login_Form">
  30.   <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
  31.     <?php if(isset($msg)){?>
  32.     <tr>
  33.       <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
  34.     </tr>
  35.     <?php } ?>
  36.     <tr>
  37.       <td colspan="2" align="left" valign="top"><h3>Login</h3></td>
  38.     </tr>
  39.     <tr>
  40.       <td align="right" valign="top">Username</td>
  41.       <td><input name="Username" type="text" class="Input"></td>
  42.     </tr>
  43.     <tr>
  44.       <td align="right">Password</td>
  45.       <td><input name="Password" type="password" class="Input"></td>
  46.     </tr>
  47.     <tr>
  48.       <td> </td>
  49.       <td><input name="Submit" type="submit" value="Login" class="Button3"></td>
  50.     </tr>
  51.   </table>
  52. </form>
  53.  
  54. </html>
  55.  
File Description
  • login.php
  • PHP Code
  • 03 Feb-2021
  • 1.71 Kb
You can Share it: