Blogger Information
Blog 48
fans 3
comment 1
visits 36983
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP+MYSQL开发一个简单的登录功能(cookie及session操作)——2018年4月20日
JackBlog
Original
1278 people have browsed it



GIF.gif

文件结构:

QQ截图20180421225840.png


根目录下的文件:

index.php实例

<?php
$page_title='首页';

include('inc/header.php');

if ((isset($_COOKIE['id'])) && basename($_SERVER['PHP_SELF']) != 'loginout.php'){
    echo '<a href="loginout.php">退出</a>';
}else{
    echo '<a href="login.php">登录</a>';
}

include ('inc/footer.php');

运行实例 »

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


login.php实例

<?php

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

require 'inc/mysql.php';
require 'inc/function.php';

    list($check,$data) = login_check($mysql,$_POST['username'],$_POST['password']);
        if ($check){
            setcookie('id',$data['id']);
            setcookie('name',$data['name']);
            jumpto('member.php');
        }else{
            $errors = $data;
            echo "<script>alert('".$errors[0]."');</script>";
        }
    mysqli_close($mysql);
}

require 'login_page.php';

运行实例 »

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



login_page.php实例

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

<div class="login">
    <h3>会员登录</h3>
    <form action="login.php" method="post">
        <p>
            <label for="username">账号:</label>
            <input type="text" name="username" id="username">
        </p>
        <p>
            <label for="password">密码:</label>
            <input type="password" name="password" id="password">
        </p>
        <p>
            <button type="submit" name="btn_login" id="btn_login">立即登录</button>
        </p>
    </form>
</div>
<?php
include ('inc/footer.php');
?>

运行实例 »

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


loginout.php实例

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

    jumpto('login.php');
}else{
    setcookie('id','',time()-3600);
    setcookie('name','',time()-3600);
}
$page_title='首页';

include('inc/header.php');
?>
<div class="main">
    <?php
    echo '<h1 style="color: #ff8bac;">退出成功</h1><a href="login.php">登录</a><br>';
    ?>
</div>
<?php


include ('inc/footer.php');
?>

运行实例 »

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

member.php

实例

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

    jumpto('login.php');
}
$page_title='会员中心';
include 'inc/header.php';?>
<div class="main">
    <?php
    echo '<h2>登录成功</h2><br>';
    echo '<span style="color: red;font-size: 2em">用户id:'.$_COOKIE['id'].'</span>';
    echo '<br>';
    echo '<span style="color: red;font-size: 2em">用户姓名:'.$_COOKIE['name'].'</span><a href="loginout.php">退出</a>';
?>
</div>
<?php
include ('inc/footer.php');
?>

运行实例 »

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

inc目录下的文件:

header.php实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../0420/css/style.css">
    <title>
        <?php echo $page_title ? $page_title:'未设置标题' ?>
    </title>
    <style type="text/css">

    </style>
</head>
<body>
<div class="header">
    <div class="header_top_bg">
        <div class="header_top">
        <div class="header_top_left">
            <span>欢迎访问本站!</span>
        </div>
            <div class="header_top_right">
                <?php
                if (isset($_COOKIE['id'])){
                    echo '<span>会员ID:'.$_COOKIE['id'].'  </span>';
                    echo '<span>会员姓名:'.$_COOKIE['name'].'  <a href="../0420/loginout.php">退出</a></span>';

                }else{
                    echo '<span><a href="../0420/login.php">登录</a></span>';
                }
                ?>
            </div>

        </div>
        <div class="header_nav">
            <h1>顶部</h1>
        </div>

    </div>

</div>

运行实例 »

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


footer.php实例

<div class="footer_bg">
    <div class="footer">
        <h2>底部信息</h2>
    </div>

</div>
</body>
</html>

运行实例 »

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


mysql.php实例

<?php
define('dbhost','127.0.0.1');
define('dbuser','xydb');
define('dbpass','123456');
define('dbname','xydb');
define('dbchar','utf8');

$mysql = mysqli_connect(dbhost,dbuser,dbpass,dbname);
if (mysqli_connect_errno($mysql)){
    echo '数据库连接失败'.mysqli_connect_errno($mysql);
}
mysqli_set_charset($mysql,dbchar);

运行实例 »

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

function.php

实例

<?php
function jumpto($page='index.php'){
    $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $url = rtrim($url,'/\\');
    $url .= '/'.$page;
    header('Location:'.$url);
    exit();
}
function login_check($mysql,$username_post,$password_post){
    $errors = [];
//    账号验证
    if (empty($username_post)){
        $errors[]='用户名不得为空!';
    }else{
        $username = mysqli_real_escape_string($mysql,trim($username_post));
    }
//密码验证
    if (empty($password_post)){
        $errors[]='密码不得为空!';
    }else{
        $password = mysqli_real_escape_string($mysql,trim($password_post));
    }
    if (empty($errors)){
        $sql= "select `id`,`name` from `xy_user` where `username` = '$username' and `password` = '$password'";
        $res = mysqli_query($mysql,$sql);
        if(mysqli_num_rows($res)==1){
            $row = mysqli_fetch_array($res,MYSQLI_ASSOC);
            return [true,$row];
        }else{
            $errors[]='账号或密码错误';
        }

    }
    return [false,$errors];
}
?>

运行实例 »

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


Correction status:Uncorrected

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!