美文网首页
PHP 调用模板生成临时网页

PHP 调用模板生成临时网页

作者: 萝卜头科技 | 来源:发表于2022-02-08 16:38 被阅读0次

1 主页:index.php

 <?php
    header("content-type:text/html;charset=utf-8");

 session_start(); //开启session
    //判断登录时的session是否存在 如果存在则表示已经登录
    if(!$_SESSION['islogin']){
        // !$_SESSION['islogin']  表示不存在 回到登录页面
        header("Location: login.php");exit;
    }
    
    include 'conn.php';
    //$link= mysqli_connect('localhost','root','root');
    //    mysqli_character_set_name();
     mysqli_query($link,"set names utf8");
      mysqli_select_db($link,'w_jiapu');
     $sql="select * from zixun where ZX_id='10001'";
     $result=mysqli_query($link,$sql);
     $records=array();
     while( $record = mysqli_fetch_array($result )){
         //mysql_fetch_array()函数的作用类似foreach对数组的作用:遍历(结果集)
         //它每次去的结果集的“一行数据”,并“装入”到数组 $record 中
         //该数组的下标就是该select的字段名,值就是对应行的数据值
       $records[]=$record;
     }
 //    var_dump($records);
 require 'moban.html';
   //require 'login.html';
 //为啥能那个顺利的展示,那个require  'login.html'; 就是把login.html页面照搬到这个b.php文件中
 
 ?>

2 模板HTML

 <!-- 模板文件,利用HTML代码展示数据 -->
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
     <title>比赛列表</title>
  </head>
 <body>
 
 <table>
     <tr>
<th>ZX_id</th><th>ZX_name</th><th>ZX_fenlei</th><th>ZX_zuozhe</th><th>更新时间</th><th>浏览次数</th><th>发布状态</th>
     </tr>
     <?php foreach($records as $row) : ?>
    <tr>
         <td><?php echo $row['ZX_id'];?></td>
         <td><?php echo $row['ZX_name'];?></td>
         <td><?php echo $row['ZX_fenlei'];?></td>
         <td><?php echo $row['ZX_zuozhe'];?></td>
         <td><?php echo $row['gengxin_time'];?></td>
         <td><?php echo $row['liulan_cishu'];?></td>
         <td><?php echo $row['fabu_zhuangtai'];?></td>
     </tr>
     <?php endForeach;?>
 </table>
 </body>
 </html>

3 数据库连接

<?PHP 
    //连接数据库
    
    $link = mysqli_connect("localhost","用户名","密码","数据库名");  //注意后面的dBname,不是数据表
    if (!$link) {
        die("连接失败: " . mysqli_connect_error());
    }
?> 

4 快速建立数据库

CREATE DATABASE `thkphp5` ;
use thkphp5 ;
create table zixun(
    ZX_id int  not null auto_increment primary key comment '咨询ID号',
    ZX_name VARCHAR(80) NOT NULL COMMENT '咨询标题',
    ZX_fenlei varchar(80) not null  comment '资讯分类',
    ZX_zuozhe varchar(80)  not null  comment '资讯作者',
    gengxin_time DATETIME NOT NULL DEFAULT '2016-01-01 01:01:01' COMMENT '更新时间',
     liulan_cishu int NOT NULL  COMMENT '浏览次数',
     fabu_zhuangtai VARCHAR(50) NOT NULL COMMENT '发布状态'
 )engine=MyISAM charset=utf8;
 INSERT  into zixun(ZX_id, ZX_name, ZX_fenlei, ZX_zuozhe, gengxin_time, liulan_cishu, fabu_zhuangtai) values(10001, 'PHP', '理论', '王超', '2017-08-07 11:58:01', 100, '草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10002,'C语言','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10003,'JAVA语言','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10004,'Mysql语言','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10005,'html','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10006,'spring','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10007,'scence','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10008,'computer','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10009,'math','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(100010,'english','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10011,'word','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10012,'jsp','理论','王超','2017-08-07 11:58:01',100,'草稿');
 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10013,'CSS','理论','王超','2017-08-07 11:58:01',100,'草稿');

5 需要登录的:

在index 开始加上 session_start();
并通过 $_SESSION['username'] 传递id,后面登录成功调用时,用ID 来筛选

    session_start(); //开启session
        $_SESSION['username'] = $username;
        
        $_SESSION['islogin'] = true;
                $_SESSION['user_id'] = $user_id;
        $_SESSION["code"]=mt_rand(0, 100000);//给session附一个随机值,防止用户直接通过调用界面访问welcome.php

相关文章

网友评论

      本文标题:PHP 调用模板生成临时网页

      本文链接:https://www.haomeiwen.com/subject/dlgbkrtx.html