2014-01-11 21 views
-2

我新的PHP和我寫了這個代碼:PHP代碼不工作 - 如果和重定向到一個頁面

<?php 

$usernametest="Testing"; 
$passwordtest="TestingPass"; 

if (isset($_POST['submit'])) 
{ 
    if ((isset($_POST['username']) == $usernametest) && (isset($_POST['password']) == $passwordtest)) 
    { include ('templates/main.php'); 
    } 
    else 
    { 
     echo "please enter the correct username and password combination"; 
    } 
    exit(); 
} 
?> 

我做了兩個文本框和一個提交按鈕,我希望用戶被定向到另一個頁面如果用戶名等於Testing並且密碼等於TestingPass,並且如果用戶沒有輸入正確的組合,我希望網站說用戶名和密碼不正確。另外,我應該在哪裏準確地粘貼此代碼?在文本框代碼上方?

+0

「在此處輸入代碼」 的一部分? –

+1

使用header()函數進行重定向,如下所示:header('location:templates/main.php'); –

回答

0

您想使用header()。因此,您不應該在這種情況下包含頭文件,因爲頭文件已經發送。

<?php 

$usernametest="Testing"; 
$passwordtest="TestingPass"; 

if (isset($_POST['submit'])) 
{ 
if ($_POST['username'] == $usernametest && $_POST['password'] == $passwordtest) 
{ 

header("Location: MY_PAGE.php"); 
} 
else 
{ 
echo "please enter the correct username and password combination"; 
} 
exit(); 
} 
?> 
+0

非常感謝!這樣做 – Nimara

+0

不客氣!如果你不介意的話,我可以隨心所欲地投票。 ;-) –

+0

我是新的,所以我的聲望水平不允許我,我檢查了綠色檢查thingy .. ;-) – Nimara

0

Isset()函數檢查變量是否存在並返回布爾值。你必須檢查的平等是這樣的:

if ($_POST['username'] == $usernametest && $_POST['password'] == $passwordtest) 
2

你有狀態檢查錯誤和重定向:

<?php 

    $usernametest="Testing"; 
    $passwordtest="TestingPass"; 

    if (isset($_POST['submit'])) 
    { 
     if ((isset($_POST['username']) && $_POST['username'] == $usernametest) && (isset($_POST['password']) && $_POST['password'] == $passwordtest)) 
     { 
      header('location: templates/main.php'); 
     } 
     else 
     { 
      echo "please enter the correct username and password combination"; 
     } 
     exit(); 
    } 
    ?> 
+0

@Nimara:你試過這個嗎?它應該現在工作:) –

0

始終存儲值的變量使代碼更易於理解

<?php 

$ usernametest = 「測試」; $ passwordtest =「TestingPass」;

如果你的代碼(isset($ _ POST [ '提交'])){

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

if ($username == $usernametest && $password == $passwordtest)) 
{  header("location:templates/main.php"); 
} 
else 
{ 
    echo "please enter the correct username and password combination"; exit(); 
} 

} ?>