2016-07-01 121 views
0

這是我的登錄頁面,這並不驗證用戶登錄truefalse插入任何passwordusername它的用戶登陸頁面重定向再次後。標頭位置也不起作用。我是新來的PHP,所以我不知道這是什麼問題。爲什麼我登錄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> 
+0

我想從這裏出現的問題'$用戶名= $密碼= $ userError = $ passError =「」;' – MuthaFury

+0

你能PLZ詳細介紹正如我上面提到的,我是新的php –

+1

你有雙引號的問題,其中連接的問題。您需要轉義html中底部的那些部分。 – Rasclatt

回答

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條件

+0

Thnx對你的幫助很大 –