php 登录与退出登录逻辑
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 登录与退出登录逻辑相关的知识,希望对你有一定的参考价值。
退出登录
<?php
/**
* Created by PhpStorm.
* User: liuan
* Date: 2018-11-20 0020
* Time: 11:20
*/
header('content-type:text/html;charset=utf-8');
include_once 'lib/fun.php';
session_start();
setcookie("username_auto_login",'',time()-1);
setcookie("password_auto_login",'',time()-1);
setcookie('auto_login', false, time() + 3600 * 24 * 30);
unset($_SESSION['user']);
msg(1, '退出登录成功!', 'index.php');
主要是会对cookie进行清除。
登录逻辑
function login($username, $password, $isMd5 = true)
if (!$username)
return array(2, "用户名不能为空");
if (!$password)
return array(2, "密码不能为空");
$remember = isset($_POST['remember']) ? $_POST['remember'] : "";
$auto_login = isset($_POST['auto_login']) ? $_POST['auto_login'] : "";
// 数据库操作
$con = mysqlinit();
if (!$con)
return array(2, mysqli_error($con));
//根据用户名 查询用户
$sql = "SELECT * FROM `la_user` WHERE `username`='$username' or `email`='$username' LIMIT 1";
$obj = mysqli_query($con, $sql);
$reslut = mysqli_fetch_assoc($obj);
if (is_array($reslut) && !empty($reslut))
//array(4) ["id"]=> string(1) "1" ["username"]=> string(3) "123" ["password"]=> string(32) "c6db6ad7cb84d87426f9c620de3b7b68" ["create_time"]=> string(10) "1542452928"
$password = $isMd5 ? createPassword($password) : $password;
if ($password === $reslut['password'])
$_SESSION['user'] = $reslut;
if ($remember == 'on')
setcookie("username_remember", $username, time() + 3600 * 24 * 30, null, null, null, true);
setcookie("password_remember", $password, time() + 3600 * 24 * 30, null, null, null, true);
setcookie('remember', true, time() + 3600 * 24 * 30, null, null, null, true);
else
setcookie("username_remember", "", time() - 1, null, null, null, true);
setcookie("password_remember", "", time() - 1, null, null, null, true);
setcookie('remember', false, time() + 3600 * 24 * 30, null, null, null, true);
if ($auto_login == 'on')
setcookie("username_auto_login", $username, time() + 3600 * 24 * 30, null, null, null, true);
setcookie("password_auto_login", $password, time() + 3600 * 24 * 30, null, null, null, true);
setcookie('auto_login', true, time() + 3600 * 24 * 30, null, null, null, true);
else
setcookie("username_auto_login", "", time() - 1, null, null, null, true);
setcookie("password_auto_login", "", time() - 1, null, null, null, true);
setcookie('auto_login', false, time() + 3600 * 24 * 30, null, null, null, true);
//每次登录成功以后 就刷新登录时间
$sql = "update `la_user` set `login_time`='$_SERVER['REQUEST_TIME']' where `username`='$username'or `email`='$username' limit 1";
if (!$obj = mysqli_query($con, $sql))
return array(2, mysqli_error($con));
else
mysqli_close($con);
return array(1, "登录成功");
else
mysqli_close($con);
return array(2, "密码不正确,请重新输入");
else
mysqli_close($con);
return array(2, "用户不存在,请重新输入");
密码可以进行加密
/**
* 密码加密
* @param $password
* @return bool|string
*/
function createPassword($password)
if (!$password)
return false;
return md5(md5($password) . "wft");
以上是关于php 登录与退出登录逻辑的主要内容,如果未能解决你的问题,请参考以下文章