Blogger Information
Blog 16
fans 0
comment 1
visits 18707
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 的会话控制,cookie session-2018-8-31
安丰的博客
Original
792 people have browsed it

实例

<?php
session_start();

if(!isset($_SESSION['id'])){
    require 'inc/function.php';
    redirect_user();
}else{
    session_destroy();
    setcookie('PHPSESSID',time()-3600);
}


$page_title ='退出成功';

include 'inc/header.php';

echo <<< "WELCOME"
<h2 style="color: palegreen">
<p>请常回家看看</p>
<p><a href="index.php">返回首页</a></p>
</h2>
WELCOME;

include 'inc/footer.php';

运行实例 »

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

实例

<?php
//开始session
session_start();

if ($_SERVER['REQUEST_METHOD']=='POST'){

    //加载函数库
    require 'inc/function.php';

    //连接数据库
    require 'inc/connect.php';
    //验证登录

   list ($check,$data)=check_login($dbc,$_POST['email'],$_POST['password']);

   if($check){
       $_SESSION['id']=$data['id'];
       $_SESSION['name']=$data['name'];
       //进行跳转
       redirect_user('loggedin.php');
   }else{
       $errors=$data;
   }
    mysqli_close($dbc);

}

include 'login_page.php';

运行实例 »

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


实例

<?php
$page_title='我是首页';
include 'inc/header.php';
echo '<H2 style="color: red">我是首页</H2>';

//判断用户是否登录
//获取当前页面地址 basename($_SERVER['PHP_SELF'])
//echo basename($_SERVER['PHP_SELF']);
if(isset($_COOKIE['id']) && basename($_SERVER['PHP_SELF'])!='login.php'){
    echo '<a href="logout.php">退出</a>';
}else{
    echo '<a href="login.php">登录</a>';
}






include 'inc/footer.php';

运行实例 »

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

实例

<?php
session_start();
if(!isset($_SESSION['id'])){
    require 'inc/function.php';
    redirect_user();
}


$page_title ='登录成功';

include 'inc/header.php';

echo <<< "WELCOME"
<h2 style="color: aquamarine">
<p>欢迎您:亲爱的{$_SESSION['name']}</p>
<p><a href="logout.php">退出</a></p>
</h2>
WELCOME;

include 'inc/footer.php';

运行实例 »

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


实例

<?php
if(!isset($_COOKIE['id'])){
    require 'inc/function.php';
    redirect_user();
}


$page_title ='登录成功';

include 'inc/header.php';

echo <<< "WELCOME"
<h2 style="color: aquamarine">
<p>欢迎您:亲爱的{$_COOKIE['name']}</p>
<p><a href="logout.php">退出</a></p>
</h2>
WELCOME;

include 'inc/footer.php';

运行实例 »

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

实例

<?php
if ($_SERVER['REQUEST_METHOD']=='POST'){

    //加载函数库
    require 'inc/function.php';

    //连接数据库
    require 'inc/connect.php';
    //验证登录

   list ($check,$data)=check_login($dbc,$_POST['email'],$_POST['password']);

   if($check){
       setcookie('id',$data['id']);
       setcookie('name',$data['name']);
       //进行跳转
       redirect_user('loggedin.php');
   }else{
       $errors=$data;
   }
    mysqli_close($dbc);

}

include 'login_page.php';

运行实例 »

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

实例

<meta charset="utf-8">

<?php
$page_title='用户登录';
include 'inc/header.php';

if(isset($errors) &&!empty($errors)){
    $errors_msg='<p style="color: red">';
    foreach($errors as $msg){
        $errors_msg.=$msg.'<br>';
    }
    echo $errors_msg,'</p>';
}
?>

<h2 style="color: aquamarine">用户登录</h2>

<form action="login.php" method="post">
    <p>
        <label for="email">邮箱:</label>
        <input type="email" name="email" id="email" value="<?php  echo isset($_POST['email'])? $_POST['email']:'' ?>">
    </p>
    <p>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" value="<?php  echo isset($_POST['password'])? $_POST['password']:'' ?>">
    </p>
    <p>
        <button type="submit" name="submit" id="submit">登录</button>
    </p>
</form>

<?php
include 'inc/header.php';
?>

运行实例 »

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

实例

<?php
if(!isset($_COOKIE['id'])){
    require 'inc/function.php';
    redirect_user();
}else{
    setcookie('id','',time()-3600);
    setcookie('name','',time()-3600);
}


$page_title ='退出成功';

include 'inc/header.php';

echo <<< "WELCOME"
<h2 style="color: palegreen">
<p>请常回家看看</p>
<p><a href="index.php">返回首页</a></p>
</h2>
WELCOME;

include 'inc/footer.php';

运行实例 »

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

实例

<?php
//连接数据库
$dbc=mysqli_connect('127.0.0.1','root','root','php');

if(mysqli_connect_errno()){
    die('连接失败'.mysqli_connect_errno());
}

运行实例 »

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

实例

<?php
function redirect_user($page='index.php'){

// $_SERVER['HTTP_HOST']; //当前主机名称
//dirname($_SERVER['PHP_SELF']; //当前脚本目录名称

    $url ='http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);


    //为满足各平台目录分割符号的不统一 删除不一致的右侧斜线
    $url=rtrim($url,'/\\');
    //跳转到制定地址
    $url.='/'.$page;

//跳转到制定地址
    header('Location:'.$url);
    exit();
}
//验证用户登录信息
function check_login($dbc,$email,$password){
    $errors =[];

    if(empty($email)){
        $errors[]='邮箱不能为空';
    }else{
        $e = mysqli_real_escape_string($dbc,trim($email)) ;
    }
    if(empty($password)){
        $errors[]='密码不能为空';
    }else{
        $p = mysqli_real_escape_string($dbc,trim($password));
    }
//从表中进行数据验证
    if(empty($errors)){
        //根据邮箱和密码进行验证,并返回id,name
       $sql ="SELECT `id`,`name` FROM `user` WHERE `email` ='$e' AND `password`=sha1('$p') ";
//       die($sql);
        //执行查询
        $res=mysqli_query($dbc,$sql);
//        var_dump($res);die;  查询结果对象出现
        if(mysqli_num_rows($res)==1) {
//                查询结果解析,解析长量关联部分
            $row = mysqli_fetch_array($res, MYSQLI_ASSOC);
//            print_r($row);die; 打印查询解析结果 确保能得到数据
            //返回结果查询结果
            return [true, $row];
        }else{
            $errors[]='邮箱和密码不正确,请重新输入';
        }

    }
    return[false,$errors];
}

运行实例 »

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


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