log in web - 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 log in web.php

  1. <?php
  2. session_start();
  3.  
  4. // Check if the user is already logged in, if yes, redirect to home page
  5. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  6.     header("location: home.php");
  7.     exit;
  8. }
  9.  
  10. // Include config file
  11. require_once "config.php";
  12.  
  13. // Define variables and initialize with empty values
  14. $username = $password = "";
  15. $username_err = $password_err = "";
  16.  
  17. // Processing form data when form is submitted
  18. if($_SERVER["REQUEST_METHOD"] == "POST"){
  19.     
  20.     // Check if username is empty
  21.     if(empty(trim($_POST["username"]))){
  22.         $username_err = "Please enter username.";
  23.     } else{
  24.         $username = trim($_POST["username"]);
  25.     }
  26.     
  27.     // Check if password is empty
  28.     if(empty(trim($_POST["password"]))){
  29.         $password_err = "Please enter your password.";
  30.     } else{
  31.         $password = trim($_POST["password"]);
  32.     }
  33.     
  34.     // Validate credentials
  35.     if(empty($username_err) && empty($password_err)){
  36.         // Prepare a select statement
  37.         $sql = "SELECT id, username, password FROM users WHERE username = ?";
  38.         
  39.         if($stmt = $mysqli->prepare($sql)){
  40.             // Bind variables to the prepared statement as parameters
  41.             $stmt->bind_param("s", $param_username);
  42.             
  43.             // Set parameters
  44.             $param_username = $username;
  45.             
  46.             // Attempt to execute the prepared statement
  47.             if($stmt->execute()){
  48.                 // Store result
  49.                 $stmt->store_result();
  50.                 
  51.                 // Check if username exists, if yes then verify password
  52.                 if($stmt->num_rows == 1){                    
  53.                     // Bind result variables
  54.                     $stmt->bind_result($id, $username, $hashed_password);
  55.                     if($stmt->fetch()){
  56.                         if(password_verify($password, $hashed_password)){
  57.                             // Password is correct, start a new session
  58.                             session_start();
  59.                             
  60.                             // Store data in session variables
  61.                             $_SESSION["loggedin"] = true;
  62.                             $_SESSION["id"] = $id;
  63.                             $_SESSION["username"] = $username;                            
  64.                             
  65.                             // Redirect user to home page
  66.                             header("location: home.php");
  67.                         } else{
  68.                             // Display an error message if password is not valid
  69.                             $password_err = "The password you entered was not valid.";
  70.                         }
  71.                     }
  72.                 } else{
  73.                     // Display an error message if username doesn't exist
  74.                     $username_err = "No account found with that username.";
  75.                 }
  76.             } else{
  77.                 echo "Oops! Something went wrong. Please try again later.";
  78.             }
  79.  
  80.             // Close statement
  81.             $stmt->close();
  82.         }
  83.     }
  84.     
  85.     // Close connection
  86.     $mysqli->close();
  87. }
  88. ?>
  89.  
File Description
  • log in web
  • PHP Code
  • 20 Mar-2024
  • 3.13 Kb
You can Share it: