2014-04-26 193 views
0

即使使用不正確的用戶名和密碼登錄,請幫助我找出問題出在哪裏。我希望它只能登錄,只有當它匹配用戶名和密碼,但它完全是錯誤的。使用錯誤的用戶名和密碼登錄

下面的代碼:

<form name="login" method="post" action="countries.php"> 
<p> </p> 
<table border=0 width=500px align=center> 
    <tr> 
<td>Enter User Name</td> 
</tr> 
<tr> 
<td><input type=text size=30 value="" name=t1></td> 
</tr> 
<tr> 
<td>Enter Password</td> 
</tr> 
<tr> 
<td><input type=password size=30 value="" name=t2> 
</td> 
</tr> 

<tr> 
<td><input type=reset value="Clear Form" name=b2> 
</td> 
<td><input type=submit value="Login Form" name=b4> 
</td> 
</td></tr> 
<tr> 
<td><input type=submit value="Guest User" name=b5> 
</td> 
</tr> 

</table> 

<hr /> 
</center> 

<?php 
$t1="Maha"; 
$t2="abc"; 
if($t1 == "Maha" && $t2 == "abc") 
{ 

     header("countries.php"); 
     exit; 
    echo " We are glad you are visiting us again. Lets plan yout tour together." ; 

} 
else { 

    header("final.php"); 
    exit; 
    echo " User name or password is incorrect. Try again."; 
} 
?> 

    </form> 
+2

**旁註:**您錯過了'header'函數的'location'關鍵字。 –

+0

你正在定義你的值 - >'$ t1 =「Maha」; $ t2 =「abc」;'所以他們永遠是正確的。更改爲'$ t1 = $ _ POST ['t1']; $ t2 = $ _ POST ['t2'];',除了'header(「Location:final.php」)中缺少'location'' – Sean

回答

1

你有一些其他的問題,這將導致該在其他方面突破,但它始終是打,因爲你是比較這些變量(硬編碼值的登錄狀態):

$t1="Maha"; 
$t2="abc"; 

硬編碼的憑據:

if($t1 == "Maha" && $t2 == "abc") 

在而不是通過$_POST讀取用戶輸入。

1

您張貼在countries.php

形式登錄的代碼應該是countries.php

而且,你缺少頭部代碼中的位置關鍵字:

header('Location: http://www.example.com/'); 

這裏是你的修改php代碼應該在countries.php

<?php 
$t1=$_POST['t1']; 
$t2=$_POST['t2']; 
if($t1 == "Maha" && $t2 == "abc") 
{ 

     header("Location: countries.php"); // probably some other page since countries.php has the login verification logic. 
     exit; 
    echo " We are glad you are visiting us again. Lets plan yout tour together." ; 

} 
else { 

    header("Location: final.php"); 
    exit; 
    echo " User name or password is incorrect. Try again."; 
} 
?> 
+0

我根據你的建議改變了我的代碼,但它仍然不工作在它仍然登錄不正確的用戶名和密碼。 – user3575757

1

您的變量通過POST發送。

if($_POST['t1'] == "Maha" && $_POST['t2'] == "abc") 
相關問題