一、head.php
<?php
/*
1. head.php為前台每個程式都會引入的檔案
2. 所有檔案都必須執行的流程,請放到這裡
3. smarty_01
*/
session_start(); //啟用 $_SESSION,前面不可以有輸出
error_reporting(E_ALL);@ini_set('display_errors', true); //設定所有錯誤都顯示
$http = 'http://';
if (!empty($_SERVER['HTTPS'])) {
$http = ($_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
}
#網站實體路徑(不含 /)
define('_WEB_PATH', str_replace("\\", "/", dirname(__FILE__)));
#網站URL(不含 /)
define('_WEB_URL', $http . $_SERVER["HTTP_HOST"] . str_replace($_SERVER["DOCUMENT_ROOT"], "", _WEB_PATH));
#--------- WEB -----
#程式檔名(含副檔名)
$WEB['file_name'] = basename($_SERVER['PHP_SELF']); //index.php
//basename(__FILE__)head.php
#--------- WEB END -----
#
/*---- 必須引入----*/
#引入樣板引擎
require_once _WEB_PATH.'/smarty.php';
#引入資料庫設定
require_once _WEB_PATH.'/sqlConfig.php';
#引入設定檔
require_once _WEB_PATH . '/function.php';
二、sqlConfig.php
<?php
#資料庫伺服器
$db_host = "localhost";
#資料庫使用者帳號
$db_user = "root";
#資料庫使用者密碼
$db_password = "111111";
#資料庫名稱
$db_name = "web";
#PHP 5.2.9以後
$db = new mysqli($db_host, $db_user, $db_password, $db_name);
if ($db->connect_error) {
die('無法連上資料庫 (' . $db->connect_errno . ') '
. $db->connect_error);
}
#設定資料庫語系
$db->set_charset("utf8");
三、smarty.php
<?php
#引入smarty物件
require_once _WEB_PATH . '/class/smarty/libs/Smarty.class.php';
#實體化
$smarty = new Smarty;
#指定左標籤定義符
$smarty->left_delimiter = "<{"; //指定左標籤
#指定左標籤定義符
$smarty->right_delimiter = "}>"; //指定右標籤
#指定模版存放目錄
$smarty->template_dir = _WEB_PATH . '/templates/';
#指定編譯文件存放目錄
$smarty->compile_dir = _WEB_PATH . '/templates_c/';
#指定配置文件存放目錄
$smarty->config_dir = _WEB_PATH . '/configs/';
#暫存路徑
$smarty->cache_dir = _WEB_PATH . '/cache/';
#開啟調試控制台,輸出變數
//$smarty->debugging = true;
#開啟緩存
//$smarty->caching = true;
#設定緩存時間,詳見 https://www.smarty.net/docs/zh_CN/caching.tpl
//$smarty->cache_lifetime = 120;
#定義模板URL
$smarty->assign("xoImgUrl", _WEB_URL . '/templates/');
$smarty->assign("xoAppUrl", _WEB_URL."/");
四、function.php
<?php
/**
* Get variables passed by GET or POST method
*
* Comment by Taiwen Jiang (a.k.a. phppp): THE METHOD IS NOT COMPLETE AND NOT SAFE. YOU ARE ENCOURAGED TO USE PHP'S NATIVE FILTER_VAR OR FILTER_INPUT FUNCTIONS DIRECTLY BEFORE WE MIGRATE TO XOOPS 3.
*/
function system_CleanVars(&$global, $key, $default = '', $type = 'int')
{
switch ($type) {
case 'array':
$ret = (isset($global[$key]) && is_array($global[$key])) ? $global[$key] : $default;
break;
case 'date':
$ret = (isset($global[$key])) ? strtotime($global[$key]) : $default;
break;
case 'string':
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default;
break;
case 'int': default:
$ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : $default;
break;
}
if ($ret === false) {
return $default;
}
return $ret;
}
###############################################################################
# 轉向函數
###############################################################################
function redirect_header($url = "", $time = 3000, $message = '已轉向!!') {
$_SESSION['redirect'] = "\$.jGrowl('{$message}', { life:{$time} , position: 'center', speed: 'slow' });";
header("location:{$url}");
exit;
}
###############################################################################
# 取得目前網址
###############################################################################
if (!function_exists("getCurrentUrl")) {
function getCurrentUrl() {
global $_SERVER;
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : "";
$currentUrl = $protocol . '://' . $host . $script . $params;
return $currentUrl;
}
}
###############################################################################
# 獲得填報者ip
###############################################################################
if (!function_exists("getVisitorsAddr")) {
function getVisitorsAddr() {
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
}
#####################################################################################
# 建立目錄
#####################################################################################
if (!function_exists("mk_dir")) {
function mk_dir($dir = "") {
#若無目錄名稱秀出警告訊息
if (empty($dir)) {
return;
}
#若目錄不存在的話建立目錄
if (!is_dir($dir)) {
umask(000);
//若建立失敗秀出警告訊息
mkdir($dir, 0777);
}
}
}
五、index.php
<?php
/* 引入檔頭,每支程都會引入 */
require_once 'head.php';
/* 過濾變數,設定預設值 */
$op = system_CleanVars($_REQUEST, 'op', '', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', '', 'int');
/* 程式流程 */
switch ($op){
case "xxx" :
$msg = xxx();
header("location:index.php");//注意前面不可以有輸出
exit;
case "yyy" :
$msg = yyy();
header("location:index.php");//注意前面不可以有輸出
exit;
default:
$op = "op_list";
op_list();
break;
}
/*---- 將變數送至樣版----*/
$smarty->assign("WEB", $WEB);
$smarty->assign("op", $op);
/*---- 程式結尾-----*/
$smarty->display('theme.tpl');
/*---- 函數區-----*/
function xxx(){
global $smarty;
}
function yyy(){
global $smarty;
}
function op_list(){
global $smarty;
}
user.php
<?php
/* 引入檔頭,每支程都會引入 */
require_once 'head.php';
/* 過濾變數,設定預設值 */
$op = system_CleanVars($_REQUEST, 'op', 'op_form', 'string');
$sn = system_CleanVars($_REQUEST, 'sn', '', 'int');
// echo $op;die();
/* 程式流程 */
switch ($op){
case "op_form" :
$msg = op_form();
break;
case "yyy" :
$msg = yyy();
header("location:index.php");//注意前面不可以有輸出
exit;
default:
$op = "op_list";
op_list();
break;
}
/*---- 將變數送至樣版----*/
$smarty->assign("WEB", $WEB);
$smarty->assign("op", $op);
/*---- 程式結尾-----*/
$smarty->display('user.tpl');
/*---- 函數區-----*/
function op_form(){
global $smarty;
}
function yyy(){
global $smarty;
}
function op_list(){
global $smarty;
}
上課用:
"<{$xoImgUrl}>vendor/
"<{$xoImgUrl}>js/
"<{$xoImgUrl}>css/
"<{$xoImgUrl}>img/
Git
https://www.ugm.com.tw/modules/tad_book3/page.php?tbdsn=1108
web11
https://github.com/tawan158/web11