使用PHP进行简单的硬编码登录


Simple Hard coded logon using PHP

session_start();
$username = $password = $userError = $passError = '';
if(isset($_POST['sub'])){
$username = $_POST['username']; $password = $_POST['password'];
if($username === 'admin' && $password === 'password'){
$_SESSION['login'] = true; header('LOCATION:trylog.php'); die();
}
if($username !== 'admin')$userError = 'Invalid Username';
if($password !== 'password')$passError = 'Invalid Password';
}
?>
<div id="right">
   <div id="login">
  <form name="form1" method="post" action="">
     <br />
     <label for="me">Username</label>
     <br />
     <input name="user" type="text" label="me">
     <br />
     <label for="Hello">Password</label>
     <br />
     <input id="pass" label="Hello" type="password" onclick="javascript:gginput();">
     <br />
     <br />
     <input name="submit" type="submit" value="Log in">
     or <a href="#" id="reg" >Register </a>
  </form>
   </div>
   <div id="blurb">
  <p id="blurbtxt">Welcome message here!</p>
   </div>
</div>

我也有一个大学作业,用一个简单的硬编码登录创建一个网站,它不必很复杂,只需要简单地工作。这就是我目前所拥有的,我无法让它发挥作用。有人有什么想法吗?非常感谢。

您的post值和输入名称不匹配。尝试更改(第5行):

$username = $_POST['username']; $password = $_POST['password'];

$username = $_POST['user']; $password = $_POST['pass'];

而且您的密码输入字段不包含name标记。请将其更改为以下内容:

<input name="pass" id="pass" label="Hello" type="password" onclick="javascript:gginput();">

示例登录页面应如下所示,并根据会话工作。如果用户关闭会话,它将擦除会话数据

<?
   // error_reporting(E_ALL);
   // ini_set("display_errors", 1);
?>
<html lang="en">
   <head>
      <link href="css/bootstrap.min.css" rel="stylesheet">
      <style>
         body {
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #ADABAB;
         }
         .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
            color: #017572;
         }
         .form-signin .form-signin-heading,
         .form-signin .checkbox {
            margin-bottom: 10px;
         }
         .form-signin .checkbox {
            font-weight: normal;
         }
         .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
         }
         .form-signin .form-control:focus {
            z-index: 2;
         }
         .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
            border-color:#017572;
         }
         .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
            border-color:#017572;
         }
         h2{
            text-align: center;
            color: #017572;
         }
      </style>
   </head>
   <body>
      <h2>Enter Username and Password</h2> 
      <div class="container form-signin">
         <?php
            $msg = '';
            if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {
               if ($_POST['username'] == 'tut' && $_POST['password'] == '1234') {
                  $_SESSION['valid'] = true;
                  $_SESSION['timeout'] = time();
                  $_SESSION['username'] = 'tut';
                  echo 'You have entered valid use name and password';
               }
               else 
               {
                  $msg = 'Wrong username or password';
               }
            }
         ?>
      </div> <!-- /container -->
      <div class="container">
         <form class="form-signin" role="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
            <h4 class="form-signin-heading"><?php echo $msg; ?></h4>
            <input type="text" class="form-control" name="username" placeholder="username = tut" required autofocus></br>
            <input type="password" class="form-control" name="password" placeholder="password = 1234" required>
            <button class="btn btn-lg btn-primary btn-block" type="submit" name="login">Login</button>
         </form>
         Click here to clean <a href="logout.php" tite="Logout">Session.
      </div> 
   </body>
</html>

注销.php它将擦除会话数据

<?php
   session_start();
   unset($_SESSION["username"]);
   unset($_SESSION["password"]);
   echo 'You have cleaned session';
   header('Refresh: 2; URL=login.php');
?>