2017-04-25 40 views
0

嗨,我是一個初學者,我試圖創建一個管理員登錄頁面使用HTML和PHP(PHP也在這裏爲其他幾個目的)一旦管理員登錄,一個HTML文件需要運行,我會在下面包含我的登錄頁面代碼,我需要在php文件中的if語句中插入命令我嘗試了不同的使用include函數的方式,也許我沒有正確使用它 預先感謝您! !如何從PHP調用一個html文件

代碼: PHP文件:

?php 

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

    if ($username =='admin' and $password =='Parthurnax') 
    { 
     include 'test.html'; 
    } 
    else 
    { 
     echo 'you are not the admin'; 
    } 
    ?> 

HTML文件:如果你想重定向到新的頁面

if(whatever_condition_set_true) { 
header('Location: whatever_page.html'); 
exit; 
} 

或 如果您想在下面

<html> 
<body> 


<div align="center"> 
<form action="page.php" method="POST"> 

<b>Username:</b><input type="text" name="username"><br> 
<b>Password:</b><input type="password" name="password"><br> 
<input type="submit"> 

</form> 
</div> 

</body> 
</html> 
+2

用戶'標題( 「位置:test.html的」);退出():' –

+5

PHP標記裏面的PHP標記是完全錯誤的 –

+0

這只是我嘗試過的很多命令中的一個,反正謝謝讓我知道標記不應該嵌套 – Niron

回答

1

變化

if ($username =='admin' and $password =='Parthurnax') 
{ 
    <?php include 'test.html';?> 
} 
else 
{ 
    echo 'you are not the admin'; 
} 

if ($username =='admin' and $password =='Parthurnax') 
{ 
    include 'test.html'; 
} 
else 
{ 
    echo 'you are not the admin'; 
} 

你在一個已經打開的PHP腳本雙頭呆PHP標籤。

不要忘記test.html頁面仍然可以訪問而不登錄。 如果我要直接在我的瀏覽器中輸入test.html,我會得到您的受保護頁面。

將其更改爲PHP腳本並檢查登錄的用戶。如果用戶沒有登錄301登錄頁面或死掉你的腳本。

+0

謝謝你讓我知道,但我也用包括沒有標籤,它仍然沒有迴應 – Niron

1

使用根據條件包含任何頁面,然後使用

if(whatever_condition_set_true) { 
    include_once('whatever_page.html'); 
} 
+0

非常感謝你這段代碼讓我去測試。 HTML,我錯過了我以前的版本退出,非常感謝你 – Niron

+0

很高興知道它有幫助。如果有幫助,請接受答案。 –

-1

使用header("yourlink.html");不要忘記exit()

+0

請包括更多信息給你的答案,在它的立場,它可能是一個評論。 – Script47

+1

這已經發布在評論'user header(「location:test.html」);退出();'你能否提出更多答案?請參閱:[提問,獲得答案,不要分心](https://stackoverflow.com/tour)。 – Teocci

相關問題