Apache SSI 使用

Apache SSI(Server Side Include),通常称为"服务器端嵌入"或者叫"服务器端包含",是一种类似于ASP的基于服务器的网页制作技术。
默认扩展名是 .stm、.shtm 和 .shtml。

在技术上,SSI就是在静态HTML文件中,根据需求插入不同的内容。

例如一个article的频道,每一个article内页都生成一个静态的HTML,如此时,header某个位置需要修改,则需要重新生成所有article的静态HTML文件。
如使用了SSI,可以在HTML文件中通过注释行嵌入经常会变化的共用部分,例如登入讯息等。可以不需要重新生成所有article,服务器会根据嵌入文件自动生成网页,输出到浏览器,如要修改则只需要修改嵌入的文件即可,无需重新生成所有HTML文件,服务器包含这种方式与php的include类似。

开启SSI

sudo a2enmod include
在站点配置文件的<Directory></Direcotry>中增加SSI 扩展名文件,使用.shtml
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
Options Includes // Includes 为追加 如include的内容不需要exec,则使用IncludesNoExec


例子1:简单嵌入

header.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title> SSI test </title> 
 </head>

 <body>
  <p style="color:#FFFFFF; background:#FF0000;">这是header,现在时间是:<?=date('Y-m-d H:i:s') ?></p>  
footer.php
  <p style="color:#FFFF00; background:#CCCCCC;">这是footer</p>
 </body>
</html>
index.shtml
<!--#include file="header.php"-->
<p>文章内容,如正常包含可以看到header及footer。</p>
<!--#include file="footer.php"-->
如正常会看到以下画面:


例子2:不同用户访问静态页面,显示用户讯息在header。

原理:访问domain/path/1001.shtml ,如文件不存在则生成静态文件1001.shtml,文件中使用ssi包含了header.php与footer.php,header.php中读取session显示用户名称。

.htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(\d+)\.shtml$ - [NC,L]

RewriteRule ^(\d+)\.shtml$ index.php?id=$1 [L,QSA]

</IfModule>
header.php 嵌入header
<?php
	if(!isset($_SESSION)){
	    session_start();
	}
	
	$username = isset($_SESSION['username'])? $_SESSION['username'] : 'guest';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> article </title>
 </head>

 <body>
 	<p>username:<?=$username ?></p>
footer.php 嵌入footer
 </body>
</html>
index.php 用于生成静态shtml
<?php
define('CACHE_PATH', dirname(__FILE__));

$id = isset($_GET['id'])? $_GET['id'] : 0;

$article = array(
	'1001' => '<p>article content 1</p>',
	'1002' => '<p>article content 2</p>',
	'1003' => '<p>article content 3</p>'
);

// check
if(!$id || !isset($article[$id])){
	echo $id;
	echo 'content not exists';
	exit();
}

// write cache
$cache_file = CACHE_PATH.'/'.$id.'.shtml';
$cache_content = implode("\r\n", array(
					'<!--#include virtual="header.php"-->',
					$article[$id],
					'<!--#include virtual="footer.php"-->'
				));


if(file_put_contents($cache_file, $cache_content, true)){
	include('header.php');
	echo $article[$id];
	echo 'no cache';
	include('footer.php');
}
?>
setsession.php 用于设置模拟用户登入,例如:setsession.php?username=fdipzone
<?php
session_start();

$username = isset($_GET['username'])? $_GET['username'] : 'guest';

$_SESSION['username'] = $username;
?>

源码下载地址:点击查看

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值