前言
每个账号操作的内容不一样,代表了账号之间的隔离性
账号:cyg 123456 (token请到数据库中对应的账号的token字段中获取)
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 user where username='{$username}' and password='{$password}'"; $query=mysqli_query($link,$sql);//运行sql $result=mysqli_query($link,"UPDATE user set stoken='{$token}' WHERE username='{$username}' and password='{$password}'");//运行sql $result=mysqli_fetch_array($query); echo "<script>alert('请到数据库中的user表对应的账号获取token');location.href='3.php';</script>";
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) { exit(); } $sql="select id,username from user where stoken='{$tokenaa}'"; $aa=mysqli_query($link,$sql); $result=mysqli_fetch_array($aa); $id=$result['id']; if($result) { echo "登录成功"; echo "<a href='4.php?id=$id'>登录成功请点击</a>"; } else { echo "登录失败"; }
4.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>请问你登录要进行什么操作吗???不想操作请点击这里</h1> <a href="5.php?id=<?php echo $_GET['id']; ?>">点击进入您进行登录的账号的数据页面</a><br> <form action="4.php" method="POST"> <textarea style="height:150px;width:200px;" name="content"> </textarea> <input type="hidden" name="id" value="<?php echo $_GET['id'];?>"> <input type="submit" value="提交"> </form> </body> </html> <?php if(!$_POST['id']) { exit(); } if(!$_POST['content']) { exit(); } $content=$_POST['content']; $id=$_POST['id']; $link=mysqli_connect('localhost','root','root','a'); //然后是指定php链接数据库的字符集 mysqli_set_charset($link,'utf8'); $sql = "INSERT INTO content(userid,content) VALUES ('{$id}','{$content}')"; $result=mysqli_query($link,$sql); if($result) { echo "<script>alert('创建成功');location.href='5.php?id=$id';</script>"; } else { echo "插入失败"; } ?>
5.php
<?php $link=mysqli_connect('localhost','root','root','a'); //然后是指定php链接数据库的字符集 mysqli_set_charset($link,'utf8'); $sql="select * from content WHERE userid ='$_GET[id]'";//; //模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦 $result=mysqli_query($link,$sql);//运行sql ?> <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <table border="1" cellpadding="5"> <tr> <td>id</td> <td>种类</td> <td>物品</td> <?php while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦 # code... ?> <tr> <td><?=$row['id'];?></td> <td><?=$row['content'];?></td> <td><?=$row['type'];?></td> <td><a href="update.php?id=<?php echo $row['id']; ?>">更新</a></td> <td><a href="delete.php?id=<?php echo $row['id']; ?>">删除</a></td> </tr> <?php } ?> </tr> </table> </body> </html>
sql
-- phpMyAdmin SQL Dump -- version 4.8.5 -- https://www.phpmyadmin.net/ -- -- 主机: localhost -- 生成日期: 2022-10-09 19:40:15 -- 服务器版本: 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` -- -- -------------------------------------------------------- -- -- 表的结构 `content` -- CREATE TABLE `content` ( `id` int(255) NOT NULL, `userid` int(255) NOT NULL, `content` text COLLATE utf8_unicode_ci NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- 转存表中的数据 `content` -- INSERT INTO `content` (`id`, `userid`, `content`) VALUES (1, 1, '4444444'), (6, 2, 'aaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniodsaaaaadnodniods'), (2, 1, '66666'), (3, 1, '656666666666666666666666666666666666656666666666666666666666666666666666656666666666666666666666666666666666656666666666666666666666666666666666656666666666666666666666666666666666656666666666666666666666666666666666'), (4, 2, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), (5, 1, 'donds1nods1nos1nosdonds1nods1nos1nosdonds1nods1nos1nosdonds1nods1nos1nosdonds1nods1nos1nosdonds1nods1nos1nos'); -- -------------------------------------------------------- -- -- 表的结构 `user` -- CREATE TABLE `user` ( `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; -- -- 转存表中的数据 `user` -- INSERT INTO `user` (`id`, `username`, `password`, `stoken`) VALUES (1, 'cyg', '123456', '61dbd28e9d0ad58af53e35e1a6147df0'), (2, 'liwen', '123456', '11fd8365c03d39d520e0179663ed0fa4'); -- -- 转储表的索引 -- -- -- 表的索引 `content` -- ALTER TABLE `content` ADD PRIMARY KEY (`id`); -- -- 表的索引 `user` -- ALTER TABLE `user` ADD PRIMARY KEY (`id`); -- -- 在导出的表使用AUTO_INCREMENT -- -- -- 使用表AUTO_INCREMENT `content` -- ALTER TABLE `content` MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; -- -- 使用表AUTO_INCREMENT `user` -- ALTER TABLE `user` 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 */;
效果
下面的时cyg账号