Blogger Information
Blog 39
fans 0
comment 0
visits 34190
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库管理网站数据和会话机制原理与实现--2019/07/25
LISTEN的博客
Original
997 people have browsed it

1、改写23日案例,用数据库管理网站内容

只需要修改header.php文件,让网站的数据来自数据库。

实例

<?php

// 网站数据来自数据表
// 1 连接数据库
require __DIR__."/connect.php";

//2、获取导航栏目
$sql='select `nav_id`,`name`,`alias`,`image` from `nav`';
$stmt=$pdo->prepare($sql);
$stmt->execute();
$navs=$stmt->fetchAll(PDO::FETCH_ASSOC);

//3、获取栏目详情
$sql='select * from `details`';
$stmt=$pdo->prepare($sql);
$stmt->execute();
$details=$stmt->fetchAll(PDO::FETCH_ASSOC);


// 4. 获取网站的系统设置
$sql='select * from `system` limit 1';
$stmt=$pdo->prepare($sql);
$stmt->execute();
$system=$stmt->fetch(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../static/css/header.css">
    <link rel="stylesheet" href="../static/css/footer.css">
    <link rel="stylesheet" href="../static/css/about.css">
    <meta name="description" content="<?php echo $system['desc']; ?>">
    <meta name="keywords" content="<?php echo $system['key']; ?>">
    <title><?php echo $system['title']; ?></title>
</head>
<body>
<!-- 头部 -->
<div class="header">
    <div class="content">
        <ul class="nav">
            <li class="item"><a href="index.php">首页</a></li>
            <?php foreach ($navs as $nav) : ?>
                <li class="item"><a href="list.php?nav_id=<?php echo $nav['nav_id']; ?>"><?php echo $nav['alias'];?></a></li>
            <?php endforeach;?>
        </ul>
    </div>
</div>

运行实例 »

点击 "运行实例" 按钮查看在线实例


2、cookie设置

实例

<?php

// `$_COOKIE`: 超全局变量数组
// `setcookie()`: 设置客户端cookie

// 1. 设置cookie
//setcookie('username','admin');
//setcookie('user_id',20,time()+20);//设置该cookie失效时间,20秒后失效消失

// 2. 读取cookie
//echo $_COOKIE['username'].'<br>';

// 3. 更新cookie,但浏览器未写入,即控制台仍然为原先数值
//$_COOKIE['username']='admin111';
//echo $_COOKIE['username'];

// 4. 销毁cookie
//unset($_COOKIE['username']);
//setcookie('username','',time()-3600);

setcookie('user[name]','admin');
setcookie('user[id]',100);
setcookie('user[email]','admin@php.cn');

if(isset($_COOKIE['user'])){
    foreach ($_COOKIE['user'] as $key=>$value){
        echo "{$key}=>{$value}<br>";
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


3、session设置

实例

<?php
// 1. 启动会话 查看文件位置:Extensions\tmp\tmp
session_start();

// 2. 设置session
$_SESSION['username']='admin';
$_SESSION['user_id']=111;
$_SESSION['email']='abc@qq***';

// 3 读取session
echo $_SESSION['username'].'<br>'.$_SESSION['user_id'].'<br>'.$_SESSION['email'];

// 4.销毁session
// 4.1. 只是清空服务器上的session文件内容
//session_unset();

// 4.2.将服务器保存的sesson文件删除(内容当然是清空的)
//session_destroy();

// 4.3. 将浏览器上保存到cookie中的PHPSESSID删除
setcookie('PHPSESSID','',time()-3600);

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:qualified

Teacher's comments:写得完整
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post