2015-11-08 274 views
1

我希望當點擊提交按鈕時,如果admin已經登錄了html頁面,應該重定向到admin.php頁面並且學生已經登錄,它應該重定向到student.php頁面。對於admin(硬編碼),將只有'一個'id密碼組合,所以我想知道如果在php腳本中使用if-else語句是否可以重定向到新的php腳本? 是否可以僅使用一個SUBMIT按鈕來完成此操作?PHP-Admin,student LogIn

 <?php 
    if($_POST['username'] === 'admin' and $_POST['password'] === 'password'){ 
     go to admin.php; //Admin login page 
    } 
    else{ 
    go to student.php; //Student has logged in => go to student login page 
    } 
    ?> 

回答

1

header()

您可以重定向做到這一點:

header('Location: /student.php'); 

請注意:您可能不頭功能否則它不工作之前輸出任何東西。

或者你可以用JavaScript

<script> 
    window.location = "student.php"; 
</script> 

或者與元刷新

<meta http-equiv="refresh" content="0; url=student.php"> 

0做到這一點的延遲,當它應該重定向。

1

你想要做的可能是以下內容:

<?php 
    if($_POST['username'] === 'admin' and $_POST['password'] === 'password'){ 
    header('Location:admin.php'); 
    } 
    else{ 
    header('Location:student.php'); 
    } 
?> 

這將重定向用戶到你想要的頁面。

而且洛基說,你不能有你的頭之前任何輸出otherwhile你會得到錯誤:頭部已經發送

http://php.net/manual/en/function.header.php

1

如果像你說的值是硬編碼,那麼這段代碼應該工作:

<?php 
if($_POST['username'] === 'admin' && $_POST['password'] === 'password'){ 
    header("Location: admin.php"); //Admin login page 
} 
else{ 
header("Location: student.php"); //Student has logged in => go to student login page 
} 
?> 

硬編碼值是不是在大多數情況下,一個很好的選擇,但是這個代碼是什麼將做到這一點。

你甚至可以使用下列元刷新:

<meta http-equiv="refresh" content="0; url=student.php"> 

還有其他的方法來做到這一點很好,但我不認爲你需要他們。希望這有助於:)

+0

我知道,我問要做到這一點在小型項目的語句。但是,只有管理員是硬編碼的,我會爲學生ID密碼組合檢查寫一個腳本。 –

1

嘗試這個

<?php 
if (isset($_POST['submit'])){ 
if($_POST['username'] === 'admin' && $_POST['password'] === 'password'){ 
    header('Location:admin.php'); 
} 
else if($_POST['username'] === 'student' && $_POST['password'] === 'password'){ 
    header('Location:student.php'); 
    } 
else{ 
    header('Location:login.php'); 
    } 
} 
?> 

提交按鈕

<input type="submit" name="submit" value="Submit">