zoukankan      html  css  js  c++  java
  • 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("
    ", 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;
    ?>

    源码下载地址:点击查看

  • 相关阅读:
    性能调优利器之strace
    如何写出优雅的Python(二)
    c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇
    使用Javascript无限添加QQ好友原理解析
    微信公众账号开发之微信登陆Oauth授权-第一篇
    WPF下的仿QQ图片查看器
    不用写软件,纯JS 实现QQ空间自动点赞
    软件分层架构下的另类设计框架-工厂模式的使用
    Javascript实现Linq查询方式
    c# 使用正则表达式 提取章节小说正文全本篇
  • 原文地址:https://www.cnblogs.com/fdipzone/p/3715091.html
Copyright © 2011-2022 走看看