前言
带token的多用户登录功能实现
用户名+密码
cyg 123456
liwen 123456
2.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="2.php" method="POST"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="submit" value="提交"> </form> </body> </html> <?php //用户输入的(表单传过来的) $username=$_POST['username']; $password=$_POST['password']; if(!$username||!$password) { echo exit(); } //========================= $link=mysqli_connect('localhost','root','root','a'); //然后是指定php链接数据库的字符集 mysqli_set_charset($link,'utf8'); $token = md5(mt_rand(100000,999999).'#$@%!^*'.time()); //查询然后判断数据库中的用户名username+密码password是不是与表单传过来的$username+$password相匹配. $sql="select * from token where username='{$username}' and password='{$password}'"; $query=mysqli_query($link,$sql);//运行sql $result=mysqli_query($link,"UPDATE token set stoken='{$token}' WHERE username='{$username}' and password='{$password}'");//运行sql $result=mysqli_fetch_array($query); if($result) { echo $token; }
3.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="3.php" method="POST"> 只要提交token就能登录啦:<input type="text" name="token"> <input type="submit" value="提交"> </form> </body> </html> <?php $link=mysqli_connect('localhost','root','root','a'); //然后是指定php链接数据库的字符集 mysqli_set_charset($link,'utf8'); $tokenaa=$_POST['token']; if(!$tokenaa) { echo exit(); } $sql="select id,username from token where stoken='{$tokenaa}'"; $aa=mysqli_query($link,$sql); $result=mysqli_fetch_array($aa); if($result) { echo "登录成功".$result['username']; } else { echo "登录失败"; }
sql
-- phpMyAdmin SQL Dump -- version 4.8.5 -- https://www.phpmyadmin.net/ -- -- 主机: localhost -- 生成日期: 2022-10-08 15:17:42 -- 服务器版本: 5.7.26 -- PHP 版本: 7.3.4 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- 数据库: `a` -- -- -------------------------------------------------------- -- -- 表的结构 `token` -- CREATE TABLE `token` ( `id` int(11) NOT NULL, `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `stoken` text COLLATE utf8_unicode_ci NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- 转存表中的数据 `token` -- INSERT INTO `token` (`id`, `username`, `password`, `stoken`) VALUES (1, 'cyg', '123456', '3de253c36e4bb3e2a1068d2e46fcb537'), (2, 'liwen', '123456', '04a916677b626f3e6d58714d9536d692'); -- -- 转储表的索引 -- -- -- 表的索引 `token` -- ALTER TABLE `token` ADD PRIMARY KEY (`id`); -- -- 在导出的表使用AUTO_INCREMENT -- -- -- 使用表AUTO_INCREMENT `token` -- ALTER TABLE `token` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
效果