博主信息
博文 34
粉丝 0
评论 0
访问量 33210
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
cookie和session会话控制应用
罗盼的博客
原创
897人浏览过
  1. 首页index.php

实例

<?php
header("content-type:text/html;charset=utf-8");
//加载头部文件
include 'top.php';

//加载中间主体文件
include 'middle.php';

//加载底部文件
include 'foot.php';
?>

运行实例 »

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

2.头部文件cookie存值

实例

<div style="width: 100%; height: 70px;">
<div style="float: left; width: 50%;height: 70px;">
    <style type = "text/css"> 
    a {font-size:16px} 
    a:link {color: blue; text-decoration:none;} 
    a:active:{color: red; }  
    a:visited {color:purple;text-decoration:none;} 
    a:hover {color: red; text-decoration:underline;} 
    </style>
<?php if( !isset($_COOKIE['account']) ){ ?>
<p>
<a style="color: red; margin-left: 10%;" href="login.php">亲,请登录</a>
<a style="color: #0080C0;; margin-left: 20px;" href="register.php">注册</a> 
<?php  }else{ ?>
<p >
<a  style="color: red; margin-left: 10%;" href="login.php">欢迎您,<?php echo $_COOKIE['account']?></a> 
<a  style="color: #0080C0;; margin-left: 20px;" href="quit.php">退出</a>
<?php } ?>
</p>
</div>
<div style="float: right;height: 70px;margin-right: 100px;"><p>联系电话:033-88652356 </p></div>

</div>

运行实例 »

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

3.头部文件session存值

实例

<!--session存值页面-->
<div style="width: 100%; height: 70px;">
<div style="float: left; width: 50%;height: 70px;">
    <style type = "text/css"> 
    a {font-size:16px} 
    a:link {color: blue; text-decoration:none;} 
    a:active:{color: red; }  
    a:visited {color:purple;text-decoration:none;} 
    a:hover {color: red; text-decoration:underline;} 
    </style>
<?php 
session_start();  

if( !isset($_SESSION['account']) ){ ?>
<p>
<a style="color: red; margin-left: 10%;" href="login.php">亲,请登录</a>
<a style="color: #0080C0;; margin-left: 20px;" href="register.php">注册</a> 
<?php  }else{ ?>
<p >
<a  style="color: red; margin-left: 10%;" href="login.php">欢迎您,<?php echo $_COOKIE['account']?></a> 
<a  style="color: #0080C0;; margin-left: 20px;" href="quit.php">退出</a>
<?php } ?>
</p>
</div>
<div style="float: right;height: 70px;margin-right: 100px;"><p>联系电话:033-88652356 </p></div>

</div>
运行实例 »

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

4.用户登录页面

实例

<!--登录页面-->
<?php header("content-type:text/html;charset=utf-8");?>
<form  method="post" action="login_In.php">
<label for="account">用户名:</label>
<input type="text" name="account" id="account"/>
<label for="password">密码:</label>
<input type="text" name="password" id="password"/>
<input type="submit"  value="登录" />
</form>

运行实例 »

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

5.处理用户提交数据页面

实例

<?php
 header("content-type:text/html;charset=utf-8");
//处理登录提交数据
if($_SERVER['REQUEST_METHOD'] == 'POST'){
   if($_POST){
    $account = trim($_POST['account']);//去掉两边空格
    $password = trim($_POST['password']);//去掉两边空格
    //非空判断
    if(strlen($account) == 0){
      echo '<script> alert(\'用户名不能为空!\');javascript:history.back(-1); </script>';
      exit;  
    }
    if(strlen($password) == 0){
      echo '<script> alert(\'密码不能为空!\');javascript:history.back(-1); </script>';
      exit;  
    }
    /*连接数据库验证数据*/
    //pdo方式连接
    include 'connect.php';
    //准备sql语句
    $sql = "SELECT `password` FROM `userinfo` WHERE `account`=:account";
    //创建预处理对象
    $stmt = $pdo->prepare($sql);
    //绑定参数
    $stmt->bindParam(':account',$account);
    $stmt->bindColumn('password',$pass);
   
    //执行并获取结果
    if($stmt->execute()){
       $stmt->fetch(PDO::FETCH_COLUMN);
       if($pass == $password){
        //密码正确,将账户和密码存入cookie中,跳转到首页页面
        setcookie('account',$account);
        //session 存数据
         //session_start();  
         //$_SESSION['account'] = $account; 
        header("location:index.php");
         
       }else{
        //密码不正确,返回登录页重新填写
        echo '<script> alert(\'密码错误!\');javascript:history.back(-1); </script>';
        exit;  
       }        
    }else{
       //用户名不正确重新输入,跳转到登录 
       echo '<script> alert(\'用户名错误!\');javascript:history.back(-1); </script>';
        exit;
    }   
   }else{
    echo '<script> alert(\'登录未成功,请稍后重试!\');javascript:history.back(-1); </script>';
    exit;
   }
    
}else{
    die('非法提交!');
}
?>

运行实例 »

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

6.连接数据库文件

实例

<?php
/*PDO连接数据库*/
$dsn = 'mysql:host=localhost;dbname=test';
$user = 'root';
$pass = 'root';
try{
    //设置数据库字符集
    $set_char = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
    //连接数据库
    $pdo = new PDO($dsn,$user,$user,$set_char); 
  
}catch(PDOException $e){
    //捕捉错误信息
    die('连接失败:'.$e->getMessage());
    
}
?>

运行实例 »

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

7.中间主体和底部文件

实例

<div style="width: 100%;min-height: 650px; background-color: lightblue;"></div>
<p style="text-align: center;">邮箱:kf@php.com    电话:033-88652356 </p>
<p style="text-align: center;">地址:陕西省西安市碑林区西北工业大学</p>
<p style="text-align: center;">陕ICP备15006021号-1    Copyright © 2018 亨氏牛网 All Rights Reserved</p>

运行实例 »

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

8.退出功能文件

实例

<?php
//退出销毁cookie
setcookie('account','',time()-3600);
header("location:index.php");
//退出销毁session
//session_start();  
//unset($_SESSION['account']);
?>

运行实例 »

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


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学