html5 login php code example

Example 1: simple login form in php

<?php
session_start();
$errorMsg = "";
$validUser = $_SESSION["login"] === true;
if(isset($_POST["sub"])) {
  $validUser = $_POST["username"] == "admin" && $_POST["password"] == "password";
  if(!$validUser) $errorMsg = "Invalid username or password.";
  else $_SESSION["login"] = true;
}
if($validUser) {
   header("Location: /login-success.php"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <title>Login</title>
</head>
<body>
  <form name="input" action="" method="post">
    <label for="username">Username:</label><input type="text" value="<?= $_POST["username"] ?>" id="username" name="username" />
    <label for="password">Password:</label><input type="password" value="" id="password" name="password" />
    <div class="error"><?= $errorMsg ?></div>
    <input type="submit" value="Home" name="sub" />
  </form>
</body>
</html>

Example 2: login javascript php

<aside id="login" class="col-md-4">    
    <div class="card">
        <div class="card-body">
            <a href="" class="float-right btn btn-outline-primary">Registro</a>
            <h4 class="card-title mb-4 mt-1">Acceso</h4>
        
            <hr />
          
            <form method="post" name="login_form" id="login_form" action="" onsubmit="enviar(this); return false;">
                <div class="form-group">
                    <input class="form-control" placeholder="Email" type="email" id="email" name="email" value="[email protected]" autocomplete />
                </div>
                <div class="form-group">
                    <input class="form-control" placeholder="******" type="password" id="password" name="password" autocomplete />
                </div>                                    
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary btn-block" id="access" name="access">Acceso</button>
                        </div> 
                    </div>
                    <div class="col-md-6 pt-2 pr-4 text-right">
                        <a class="small" href="#" class="">¿Olvidó su contraseña?</a>
                    </div>                                            
                </div>      
            </form>
                
            <!-- messages -->
            <div class="row">
                <div class="col-12">
                    <div class="outer-loader">
                        <span class="inner-loader"><img src="assets/img/loader.gif" alt="consultando ..." /></span>     
                        <span class="messages"></span>
                    </div>    
                    <span class="debug"></span>    
                </div>
            </div> <!-- end messages -->     
        </div>
        
    </div> 
</aside>

Tags:

Php Example