Redis注册登录,留言

 登录的html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="test_r.php" method="post">
    <tr>
        <td>账号</td>
        <td><input type="text" name="uname"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="pwd"></td>
    </tr>
    <tr>
        <td><input type="submit" value="注册"></td>
    </tr>
</form>
</body>
</html>

后台php

<?php
//实例化
$redis=new Redis();
//建立场链接
$redis->pconnect("127.0.0.1","6379");

//接受传值
$uname=$_POST['uname'];
$pwd=$_POST['pwd'];

//判断为空
if(empty($uname) || empty($pwd)){
    header("Refresh:3;url=reg.html");
    die("账号密码不能为空");
}

//验证唯一性(判断用户名是否存在)
$res=$redis->sIsMember('username',$uname);
if($res==1){
    //证明被注册
    header("Refresh:3;ur=reg.html");
    die("账号被注册");
}

//添加用户
//1、账号密码的存储
//2.用户名的存储
$redis->set($uname,md5($pwd));
$redis->sAdd('username',$uname);

//注册成功
header("Refresh:3;url=login.html");
die("注册成功");

登录的php

<?php
$redis=new Redis();
$redis->pconnect('127.0.0.1','6379');
$user=$_POST['uname'];
$pwd=$_POST['pwd'];
if(empty($user)||empty($pwd)){
    header("Refresh:3;url:text_r.html");
    die("账号密码不能为空,3秒后跳转...");
}
$res=$redis->sIsMember('user',$user);
if($res==0){
    header("Refresh:3;url:text_r.html");
    die("账号不存在,3秒后跳转...");
}
$rpwd=$redis->get($user);
if($rpwd!=md5($pwd)){
    header("Refresh:3;url=text_r.html");
    die("账号或密码不正确5");
}

header("Refresh:3;url=text_r.html");
die("登录成功");

现在是留言

<?php
//实例化
$redis=new Redis();
//长链接
$redis->pconnect("127.0.0.1","6379");
session_start();
//获取数据的id
$count=$redis->get("ping:count");
$arr=[];
for($i=1;$i<=$count;$i++){
    $arr[]=$redis->hGetAll("ping:".$i);
}
?>
当前登录的账号为:<?php echo $_SESSION['uname'] ?>
<a href="logout.php">退出</a>
<form action="say.php" method="post">
    <textarea name="content" id="content" cols="30" rows="10"></textarea>
    <input type="submit" value="留言">
</form>

<?php foreach($arr as $k=>$v) {?>
    <p>
        <?php echo $v['uname']?>在<?php echo date("Y-m-d H:i:s",$v['time'])?>评论了:<br>
        <?php echo $v['content']?>
    </p>
<?php }?>

留言的接值

<?php
//实例化
$redis=new Redis();
//长链接
$redis->pconnect("127.0.0.1","6379");
session_start();

//获取登录的用户名
$uname=$_SESSION['uname'];
$content=$_POST['content'];
$time=time();

//获取id
$id=$redis->incr("ping:count");

//进行redis操作
$redis->hMset(
    'ping:'.$id,
    [
        'uname'=>$uname,
        'content'=>$content,
        'time'=>$time
    ]
);

header("Refresh:3;url=home.php");
die("评论成功");

猜你喜欢

转载自blog.csdn.net/qq_42780341/article/details/84965163