include():載入指定的檔案與其全部的內容。
include_once():允許重複載入指定的檔案與其全部的內容。
require():載入指定的檔案與其全部的內容。
require_once():允許重複載入指定的檔案與其全部的內容。
<header><?php require_once("header.php"); ?> </header>
<div class="row">
<nav class="col-25 col-s-25 menu"><?php require_once("nav.php"); ?> </nav>
<article class="col-50 col-s-75"> <?php require_once("article.php"); ?> </article>
<div class="col-25 col-s-100">
<aside><?php require_once("aside.php"); ?>
</aside>
</div>
</div>
<footer><?php require_once("footer.php"); ?>
</footer>
<h1>header - h1 tag</h1>
<p>header - p tag<p>
<ul>
<li>nav - li tag 1</li>
<li>nav - li tag 2</li>
<li>nav - li tag 3</li>
<li>nav - li tag 4</li>
</ul>
<p>footer - p tag</p>
<h2>aside - h2 tag 1</h2>
<p>aside - p tag 1</p>
<h2>aside - h2 tag 2</h2>
<p>aside - p tag 2</p>
<h2>aside - h2 tag 3</h2>
<p>aside - p tag 3</p>
<h1>article - h1 tag</h1>
<p>article - p tag</p> <img src="https://lh5.googleusercontent.com/QYM9vqDuV8lyS-yIycEKX-cjN_Yd40YpnIA5n8J-Tbhd7pm4UEbzZ1dXAwdQLKWtp5gWhA" width="100%">
<table width="100%" border="1">
<?php for ($i=1; $i<=3;$i++){ include_once("checknum.php"); ?>
<tr><td><?php echo "i=".$i." 時, 總和: ".checknum(10,20,30); } ?>
</td></tr></table>
<h1><?php $i = $i-1; echo "include_once() :執行了 ".$i." 次"; ?></h1>
<?php function checknum($a,$b,$c) { return $a+$b+$c; } ?>
轉換網址:header("Location: http://www.google.com");
網頁每隔5秒執行網頁刷新:header("refresh:5");
網頁在5秒後執行轉址:header('refresh:5; url="http://www.google.com"');
註:執行 Header() 函數之前,請先執行 ob_start(); // 打開緩衝區;
<style type="text/css">
p { color: #FF0000; }
#remainder {
color: #0000FF; }
#JStime {
color: #008080; }
</style>
<p>倒數計時:倒數 30 秒範例</p><br/>
<?php header("refresh:30");
$currentTime = new DateTime("now", new DateTimeZone('Asia/Taipei'));
echo "目前的時間:".$currentTime->format("h:i:s")." (開始倒數的時間)<br/><br/>"; ?>
<div id="remainder"></div>
<div id="JStime"></div>
<script>
var Tlimit=30; var JStime;
function Tcount()
{ if (Tlimit!=0)
{ Tlimit -= 1;
nowtime = new Date();
JStime = nowtime.getHours()+":"+nowtime.getMinutes()+":"+nowtime.getSeconds();
document.getElementById("remainder").innerHTML = "倒數計時: " + Tlimit + " 秒 <br/><br/>";
document.getElementById("JStime").innerHTML = "JStime 顯示時間: " + JStime; }
// setTimeout( ) 是設定指定等候時間(單位是千分之一秒), 時間到了瀏覽器就會執行 Tcount()
setTimeout("Tcount()",1000); }
Tcount();
</script>
使用 header() 函數向客戶端瀏覽器發送「需要身份驗證 (Authentication)」的Dialog Box (帳密輸入框)。當用戶輸入帳密以後,PHP Script 將會添加預定義變量 「PHP_AUTH_USER、PHP_AUTH_PW 和 AUTH_TYPE」,分別被設置為「帳號、密碼和認證類型」。預定義變量保存在 $ _SERVER 陣列中。
註:支持 Basic 和 Digest 認證方法。
<?php
header("Content-type: text/html; charset=UTF-8");
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Secure Area"');
header('HTTP/1.0 401 Unauthorized');
echo '<br/><center><p><h1>如果您忘記帳密請來信確認!</h1>(Text to send if you hit Cancel button )</p><center>';
exit; } else {
$user="123"; $pwd="456" ;
if (($_SERVER['PHP_AUTH_USER'] != $user) or ($_SERVER['PHP_AUTH_PW'] != $pwd)) {
echo "<br/><center><p><h1>帳密錯誤, 請重啟瀏覽器再登入, 謝謝!</h1></p><center>"; } else {
echo "<br/><center><p><h1>歡迎您進入到本網站, ...</h1></p><center>"; }
}
?>
<?php
header("Content-Type:text/html; charset=utf-8");
header('Content-Type: application/pdf');
echo file_get_contents('帳號申請單.pdf');
?>
<?php
header("Content-Type:text/html; charset=utf-8");
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('帳號申請單.pdf');
?>