:::

3-1 index.php

<?php
//引入檔案(設定)
session_start();
require_once "config.php";
require_once 'function.php';
require_once 'smarty/libs/Smarty.class.php';
//實體化
$smarty = new Smarty;
//連線資料庫
$db = link_db();
// die(var_dump($smarty));

//變數過濾或預設直
$name    = isset($_SESSION['name']) ? htmlspecialchars($_SESSION['name'], ENT_QUOTES) : '訪客';
$group   = isset($_SESSION['group']) ? $_SESSION['group'] : '';
$op      = isset($_REQUEST['op']) ? htmlspecialchars($_REQUEST['op'], ENT_QUOTES) : '';
$content = '';

//跑流程
switch ($op) {
    case 'login':
        login();
        header("location: index.php");
        exit;

    case 'logout':
        logout();
        header("location: index.php");
        exit;

    // case 'regist':
    // regist();
    // break;

    case 'save_regist':
        save_regist();
        header("location: index.php");
        exit;

    default:
        # code...
        break;
}

//函數區

//登入
function login()
{
    global $admin_id, $admin_pass;
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    if ($admin_id == $name and $admin_pass == $_POST['pass']) {
        $_SESSION['group'] = "admin";
        $_SESSION['name']  = $name;
        return true;
    } elseif ($user_id == $name and $user_pass == $_POST['pass']) {
        $_SESSION['group'] = "user";
        $_SESSION['name']  = $name;
        return true;
    }
    return false;
}

//登出
function logout()
{
    unset($_SESSION['group']);
    unset($_SESSION['name']);
}

//新增使用者
function save_regist()
{
    global $db, $admin_id;
    $name = $db->real_escape_string($_POST['name']);
    if (empty($name)) {
        die("姓名為必填!");
    }

    $email = $db->real_escape_string($_POST['email']);
    if (empty($email)) {
        die("Eamil為必填!");
    }
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!$email) {
        die("不合法的Email");
    }

    $pass = $db->real_escape_string($_POST['pass']);
    if (empty($pass)) {
        die("密碼為必填!");
    }
    $pass = password_hash($pass, PASSWORD_DEFAULT);

    $group = ($admin_id == $email) ? 'admin' : 'user';

    $sql = "INSERT INTO `users` (`name`, `email`, `pass`, `group`) VALUES('{$name}', '{$email}','{$pass}','{$group}')";
    $db->query($sql) or die($db->error);
    $uid = $db->insert_id;
    return $uid;
}

//結果送至樣板
$smarty->assign('my_name', $name);
$smarty->assign('group', $group);
$smarty->assign('page_title', '活動報名系統');
$smarty->assign('content', $content);
$smarty->assign('op', $op);
$smarty->display('index.tpl');

 


:::

書籍目錄

展開 | 闔起

快速登入


http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D767%26tbsn%3D28

計數器

今天: 1768176817681768
昨天: 8625862586258625
總計: 8032375803237580323758032375803237580323758032375