为什么我的登录php页面无效

时间:2016-07-01 03:02:15

标签: php html

这是我的登录页面,在插入任何truefalse之后,它不会验证用户登录是password还是username将用户重定向到登录页面再次。标题位置也不起作用。我是php新手,所以我不确定是什么问题。

<?php
session_start();
$username = $password = $userError = $passError = '';
if(isset($_POST['sub'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    if($username === '9155499248' && $password === 'Ben 10'){
        $_SESSION['login'] = true;
        header('LOCATION:congratulation.php');
        die();
    }

    if($username !== '9155499248')
        $userError = 'Invalid Username';
    if($password !== 'Ben 10')
        $passError = 'Invalid Password';
}
echo "<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
   <head>
     <meta http-equiv='content-type' content='text/html;charset=utf-8' />
     <title>Login</title>
     <link rel="stylesheet" href="css/normalize.css">
     <link rel="stylesheet" href="css/style.css"/>
     <script src="js/prefixfree.min.js"></script>
     </head>
 <body>
    <div class="login">
<h1><b>Login</b></h1>
     <form name='input' action='{$_SERVER['PHP_SELF']}' method='post'>
    <label for='username'></label><input type='text' value='$username' id='username' name='username' />
    <div class='error'>$userError</div>
    <label for='password'></label><input type='password' value='$password' id='password' name='password' />
    <div class='error'>$passError</div>
    <button type="submit" class="btn btn-primary btn-block btn-large" name="submit">Let me in.</button>
  </form>
  </div>
        <script src="js/index.js"></script> 

  </body>
</html>

1 个答案:

答案 0 :(得分:2)

你去了

<?php
session_start(); 
$username = '';
$password = '';
$userError = ''; 
$passError = '';
if(isset($_POST['submit'])){
  $username = $_POST['username']; $password = $_POST['password'];
  if($username === '9155499248' && $password === 'Ben 10'){
    $_SESSION['login'] = true; header('LOCATION:congratulation.php'); die();
  }
  if($username !== '9155499248')$userError = 'Invalid Username';
  if($password !== 'Ben 10')$passError = 'Invalid Password';
}
echo "<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
   <head>
     <meta http-equiv='content-type' content='text/html;charset=utf-8' />
     <title>Login</title>
     <link rel='stylesheet' href='css/normalize.css'>
     <link rel='stylesheet' href='css/style.css'/>
     <script src='js/prefixfree.min.js'></script>
     </head>
 <body>
    <div class='login'>
<h1><b>Login</b></h1>
     <form name='input' action='".$_SERVER['PHP_SELF']."' method='post'>
    <label for='username'></label><input type='text' value='".$username."' id='username' name='username' />
    <div class='error'>".$userError."</div>
    <label for='password'></label><input type='password' value='".$password."' id='password' name='password' />
    <div class='error'>".$passError."</div>
    <button type='submit' class='btn btn-primary btn-block btn-large' name='submit' value='1'>Let me in.</button>
  </form>
  </div>
        <script src='js/index.js'></script> 

  </body>
</html>";

我已经更新了一些内容,

首先是那些引号"',你使用的不正确,你可能会重新检查我提供的答案。

第二个是之前您将if(isset($_POST['sub']))设为错误,因为您输入的按钮为submit而非sub因此无效。我还在按钮中添加了值value='1'以满足 isset 条件