2017-07-06 37 views
-2

如何在包含php之前執行html?因爲用戶輸入來自html。不應該包含腳本終於?怎麼在包含php的頭文件中執行?

<?php 
include('login.php'); // Includes Login Script 

if(isset($_SESSION['login_user'])){ 
header("location: profile.php"); 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Login Form in PHP with Session</title> 
<link href="style.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
<div id="main"> 
<h1>PHP Login Session Example</h1> 
<div id="login"> 
<h2>Login Form</h2> 
<form action="" method="post"> 
<label>UserName :</label> 
<input id="name" name="username" placeholder="username" type="text"> 
<label>Password :</label> 
<input id="password" name="password" placeholder="**********" type="password"> 
<input name="submit" type="submit" value=" Login "> 
<span><?php echo $error; ?></span> 
</form> 
</div> 
</div> 
</body> 
</html> 
+1

我覺得從上往下讀 –

+0

用PHP包括你主要複製內容的文件並將其粘貼到您已完成包含的位置。如果你希望在你的html之後執行它,只需將你的login.php包含在你的文件末尾。順便說一句,我想你可能應該看看'if(isset($ _ POST [「username」]))' - 因爲如果你的PHP寫得不錯,你不會有任何問題認爲它是,因爲似乎你的PHP鱈魚的一部分執行沒有填寫表格。 – Twinfriends

+3

沒有.... PHP代碼是從上到下執行的(就像在大多數編程語言中一樣),除了對函數的調用以及所有關於控制代碼執行流程的循環.....它不知道或關心首先執行的用戶輸入,甚至不知道該html用於用戶輸入 –

回答

2

PHP是服務器端語言,而HTML是客戶端。這意味着PHP代碼在傳遞到客戶端瀏覽器之前會在Web服務器上執行。

請參閱this question瞭解客戶端和服務器端編程之間差異的詳細信息。

0

PHP在服務器中處理您的請求並返回一個html文件(在這種情況下)。也就是說,您提出了您需要的所有信息的請求,並向您返回處理後的信息。

+0

因此,HTML首先被解析,然後在包含文件中的PHP? – webionDev

+0

@MohammadRahman php將首先讀取/執行頁面頂部的任何內容,然後移動到底部 –

+0

如何在包含php的程序中獲取ID和密碼,如果它首先執行@MasivuyeCokile – webionDev

0

「因爲用戶輸入是來自HTML」

我相信你的PHP腳本將輸出作爲響應的HTML之間的混淆,以及HTML <form>觸發請求之間。

您的問題中的HTML不會被PHP進行PARSED,而是將被輸出爲對觸發此PHP腳本的客戶端請求的響應。

可能會發生混淆,因爲您的PHP腳本正在輸出HTML表單,並且它處理請求來自該HTML表單。

PHP是關於構建最終的HTML輸出的工作。


例如:

在客戶端無論是HTML <form>通過瀏覽器,或C#機器人,或Java應用程序中運行,或什麼程序將HTTP請求發送到服務器(PHP腳本),假設你用這些參數

Request URL: "http://www.example.com/index.php" 
Request method: "POST" 
Request parameters: "username=myname&password=123" 

該請求將觸發PHP解析服務器上index.php腳本發送該請求


在服務器端您的Web服務器將在填寫請求參數後執行index.php,以便您可以在代碼中使用它們。

現在觸發index.php

$_POST['username'] = $_REQUEST['username'] = "myname"; 
$_POST['password'] = $_REQUEST['password'] = "123"; 

之前有您的請求參數,讓我們稱之爲index.php

<?php 
    include('login.php'); // Includes Login Script 

    if(isset($_SESSION['login_user'])){ 
     //check if user is authentic then redirect him to the profile page. 
     header("location: profile.php"); 
     //you should exit; your code here 
     //see: https://stackoverflow.com/questions/2747791 
    } 
    /*now any string that is not between the php open and close tags <> is parsed as 
    HTML that needs to be outputted*/ 
?> 
<!-- for example this HTML is going to be outputted ,and the browser is going to show it 
unless you used the location header --> 
<!DOCTYPE html> 
<html> 
    <!-- your log in form here --> 
</html> 
+0

OK所以這個HTML表單只有當輸入錯誤被賦予裝備..? – webionDev

+0

@MohammadRahman .........是 –

+0

@MohammadRahman如果'頭(位置)'條件不成立-'isset($ _ SESSION [ 'login_user'])' - ,那就是在案件的用戶沒有登錄 –