[英]Alternate Headers with SSI and PHP
我有兩個標題:一個用於顯示已登錄用戶,另一個用於在注銷時顯示/不是成員。 我還有一個頁腳應該在每個頁面上重復。我有個想法使用SSI來包含頁眉和頁腳。
到目前為止,我們還沒有開始太多的服務器端處理,因此,不跟蹤已登錄/已注銷的用戶。 因此,現在,我只想使用包含標題的頁面來確定要顯示的頁面。 我的主意是使用PHP文件而不是SHTML文件作為標題,因此我可以進行一些處理以確定要顯示的標題。
那么是否可以確定哪個頁面正在調用PHP的include?
我要解決所有這些錯誤嗎? 如果是這樣,哪種解決方案更合適?
例如,每個html頁面都適合以下一般布局:
<html>
<header>
<!-- relevant header calls -->
<header>
<body>
<div id="body">
<!--#include virtual="header.php"-->
<!-- actual page content -->
</div>
<!--#include virtual="footer.shtml"-->
</body>
</html>
在header.php中,我想要類似以下內容:
<?php
if(/*page is a non-logged in page*/){
echo(/*logged out header*/);
} else {
echo(/*logged in header*/);
}
?>
那么是否可以確定哪個頁面正在調用PHP的include?
不知道。 但是,如果可能的話,可以通過$_SERVER
。 將其放在您的header.php中進行測試:
<?php
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
但是,如果頁面被請求為*.html
且帶有Server-Side Includes,我什至無法開始預測PHP會話將對這種情況造成什么樣的破壞。 我懷疑session_start()
是否能夠在這種情況下設置適當的標頭,或者PHP會話cookie是否會發送到客戶端或通過SSI傳遞回PHP。
據我所知/有關,SSI只能用於包含不依賴於與用戶的任何形式的交互的靜態內容或動態內容,包括一些基本的內容,例如是否已登錄。 SSI介於靜態頁面和動態頁面之間,應被稱為“ kinda-sorta-dynamic-but-not-really”。
簡短的答案:SSI將是一個巨大的難題,拋棄它,只使用PHP include()
。
編輯:您的頁面在最基本的層次上看起來像這樣,實際上並沒有比使用SSI更復雜。 如果您采用更面向MVC的方法(即C和V部分),它將變得更加易於管理:
<?php
session_start();
// other initialization
?><html>
<head>
<!-- relevant header calls -->
<head>
<body>
<div id="body">
<?php
if($_SESSION['is_logged_in']){
echo(/*logged out header*/);
} else {
echo(/*logged in header*/);
}
?>
<!-- actual page content -->
</div>
<?php include("footer.php"); ?>
</body>
</html>
為了便於編程,最好使用其中一個。 最好只使用PHP,因為:
如果您使用功能virtual()
將PHP作為Apache模塊運行,則可以在PHP中包含SSI,但是為什么要這么做呢? 您可以include()
幾乎所有內容都包含到PHP中。
例
我將以一個帳戶管理站點為例。 為了使頭動態,你需要找到$var
的頁面調用它(我將使用$_SERVER['REQUEST_URI']
PHP中有幾個保留的服務器變量 ,您可以根據情況引用這些變量來進行調用。 因此,假設所有登錄頁面進入的授權目錄稱為“身份驗證”,您的通用外殼文件可能如下所示:
<?php
//Check for the page the person is asking for
session_start();
$root = $_SERVER['DOCUMENT_ROOT'];
//Check for the "auth" directory
if(preg_match('!^/?auth!',$_SERVER['REQUEST_URI'])){
//Do some check to see if they've been authenticated... this one is not secure, but you get the idea
if($_SESSION['logged_in']){
//Require the correct header
require_once($root.'/includes/logged-in-header.php');
} else {
//They don't belong or they're not logged in, kick them back to the login page.
header("Location: /login.php?e=1");
die();
}
} else {
//It's not an authorization required page, so show the standard header.
require_once($root.'/includes/non-auth-header.php');
}
//let's find out the page that's loading the shell.
$pageName = preg_replace('!/([^/]+)$!',"$1",$_SERVER['SCRIPT_NAME']);
switch($pageName){
/*Auth pages*/
case "billing.php":
require_once($root.'/includes/billing.php');
break;
case "account.php":
require_once($root.'/includes/account.php');
break;
case "logout.php":
require_once($root.'/includes/logout.php');
break;
default:
//show the login page
require_once($root.'/includes/login.php');
}
require_once($root.'/../shell.php');
require_once($root.'/includes/footer.php');
?>
因此,如果您位於auth
目錄中,但尚未登錄,則將獲得主頁。 如果您位於billing.php
頁面上的auth
目錄中,並且已登錄,則該站點將加載計費頁面。
auth / billing.php代碼可能如下所示:
require_once("$_SERVER['DOCUMENT_ROOT'].'/../shell.php');
include / billing.php代碼將包含頁面的所有工作,並且可以用HTML格式化,但是您可能會從數據庫中提取這些內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.