<?php /*引入檔案(初始設定)*/ session_start(); //引入設定檔 require_once 'config.php'; //引入共同函數檔 require_once 'function.php'; //引入樣板引擎的 class 檔 require_once 'smarty/libs/Smarty.class.php'; //實體化樣板引擎物件(下面才能使用樣板引擎功能) $smarty = new Smarty; // die(var_dump($smarty)); //清除樣板快取 // $smarty->clearAllCache(); /*過濾變數*/ $group = isset($_SESSION['group']) ? $_SESSION['group'] : ""; $op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op'], FILTER_SANITIZE_SPECIAL_CHARS) : ""; $name = isset($_SESSION['name']) ? $_SESSION['name'] : "訪客"; if (isset($_SESSION['group'])) { if ($_SESSION['group']) { $content = "登入成功"; } else { $content = "登入失敗"; } } else { $content = '主內容區'; } /*執行流程*/ switch ($op) { //登入 case 'login': login(); header("location: index.php"); exit; case 'logout': logout(); header("location: index.php"); exit; case "regist": break; case "insert_user": insert_user(); header("location: index.php"); exit; //預設動作 default: # 取得活動列表 break; } /*輸出結果*/ $smarty->assign('name', $name); $smarty->assign('content', $content); $smarty->assign('group', $group); $smarty->assign('op', $op); $smarty->assign('page_title', '活動報名系統'); $smarty->display('index.tpl'); /*自訂函數區*/ //登出 function logout() { unset($_SESSION['group']); unset($_SESSION['name']); } //登入 function login() { global $admin_id, $admin_pass; $name = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_SPECIAL_CHARS) : "訪客"; $pass = isset($_POST['pass']) ? $_POST['pass'] : ""; if ($name == $admin_id and $pass == $admin_pass) { $_SESSION['group'] = 'admin'; $_SESSION['name'] = $name; } else { $_SESSION['group'] = ''; $_SESSION['name'] = '訪客'; } } //新增使用者 function insert_user() { $db = link_db(); // 過濾變數 $name = $db->real_escape_string($_POST['name']); if (empty($name)) { die("姓名為必填!"); } $pass = $db->real_escape_string($_POST['pass']); if (empty($pass)) { die("密碼為必填!"); } $email = $db->real_escape_string($_POST['email']); if (empty($email)) { die("email為必填!"); } $email = filter_var($email, FILTER_VALIDATE_EMAIL); if (!$email) { die("不合法的Email"); } $group = 'admin'; // 連線資料庫 $sql = "insert into `users` (`name`, `pass`, `email`, `group`) values('$name', '$pass','$email','$group')"; $db->query($sql) or die($db->error); // $uid = $db->insert_id; // return $uid; }