使用redis技术实现注册登录列表以及关注功能

redis命令中文网参考网址:http://www.redis.cn/commands.html

首先我们需要下载一个类文件那就是predis

git地址:git clone git://github.com/nrk/predis.git

zip地址:https://github.com/nrk/predis/archive/v1.0.1.zip

然后我是嘴边创建了个本地文件引用的

接下来一个页面一个页面的看代码:

先连接redis,来一个redis.php

<?php
    header("Content-type: text/html; charset=utf-8");
    //载入redis文件
    require ‘./predis/autoload.php‘;
    //连接redis
    $redis = new Predis\Client(array(
        ‘host‘=>‘127.0.0.1‘,
        ‘port‘=>6379
    ));
?>

注册:register.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>注册</title>
</head>
<body>
    <form action="register_a.php" method="post">
        <table>
            <tr>
                <th>用户名:</th>
                <td><input type="text" name="username" id="username" /></td>
            </tr>
            <tr>
                <th>邮箱:</th>
                <td><input type="text" name="email" id="email" /></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input type="password" name="password" id="password" /></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" value="注册" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

register_a.php

<?php
    include(‘./redis.php‘);
    if(!isset($_POST[‘username‘]) || !isset($_POST[‘email‘]) || !isset($_POST[‘password‘])){
        echo "填写信息不完整";
        exit;
    }

    if(!filter_var($_POST[‘email‘],FILTER_VALIDATE_EMAIL)){
        echo "邮箱格式不正确";
        exit;
    }

    if(strlen($_POST[‘password‘])<6){
        echo "邮箱密码不安全";
        exit;
    }

    //判断邮箱是否被注册
    if($redis->hexists(‘email.to.id‘,$_POST[‘email‘])){

        echo "邮箱已经被注册";
        exit;
    }

    //密码设置函数
    function bcryptHash($rawPassword,$round = 8){
        if($round < 4 || $round >31) $round = 8;
        $salt = ‘$2a$‘ . str_pad($round,2,‘0‘,STR_PAD_LEFT) . ‘$‘;
        $randomValue = openssl_random_pseudo_bytes(16);
        $salt .= substr(strtr(base64_encode($randomValue),‘+‘,‘.‘),0,22);
        return crypt($rawPassword,$salt);
    }

    $hashedPassword = bcryptHash($_POST[‘password‘]);

    //存储用户资料
    $userID = $redis->incr(‘user:count‘);//获取一个自增id
    //存储
    $redis->hmset(
        "user:{$userID}",
        array(
            ‘uid‘=>$userID,
            ‘email‘=>$_POST[‘email‘],
            ‘password‘=>$hashedPassword,
            ‘username‘=>$_POST[‘username‘]
        )
    );
    //记录一下邮箱和用户id的关系
    $redis->hset(‘email.to.id‘,$_POST[‘email‘],$userID);
    echo "注册成功";
    header(‘location:login.php‘);
?>

登录:login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>登录</title>
</head>
<body>
    <form action="login_a.php" method="post">
        <table>
            <tr>
                <th>邮箱:</th>
                <td><input type="text" name="email" id="email" /></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input type="password" name="password" id="password" /></td>
            </tr>
            <tr>
                <th><input type="button" value="注册" onclick="zhuce();" /></th>
                <td><input type="submit" value="登录" /> <a href="#">忘记密码?</a></td>
            </tr>
        </table>
    </form>
</body>
<script type="text/javascript">
    function zhuce(){
        location.href=‘register.php‘;
    }
</script>
</html>

login_a.php

<?php
    include(‘./redis.php‘);
    if(!isset($_POST[‘email‘]) || !isset($_POST[‘password‘])){
        echo "填写信息不完整";
        exit;
    }

    if(!filter_var($_POST[‘email‘],FILTER_VALIDATE_EMAIL)){
        echo "邮箱格式不正确";
        exit;
    }

    //获取用户id
    $userID = $redis->hget(‘email.to.id‘,$_POST[‘email‘]);

    if(!$userID){
        echo "用户名或者密码错误!";
    }

    //根据用户id获取密码
    $password = $redis->hget("user:{$userID}",‘password‘);//存储的密码

    //密码验证函数
    function bcryptVerify($rawPassword,$storedHash){
        return crypt($rawPassword,$storedHash) == $storedHash;
    }

    if(!bcryptVerify($_POST[‘password‘],$password)){
        echo "密码错误!";
        exit;
    }

    echo "登录成功";
    setcookie(‘myuid‘,$userID);
    header(‘location:home.php‘);

?>

登录成功进入home.php

<?php
    include(‘./redis.php‘);
    $count = $redis->get(‘user:count‘);
    for($i=1;$i<=$count;$i++){
        $res[] = $redis->hgetall(‘user:‘.$i);
    }

    //我的关注
    $myguanzhu = $redis->smembers("user:{$_COOKIE[‘myuid‘]}:following");
    foreach ($myguanzhu as $key => $v) {
        $gzinfo[] = $redis->hget("user:{$v}",‘username‘);
    }

    //我的粉丝
    $myfen = $redis->smembers("user:{$_COOKIE[‘myuid‘]}:followers");
    foreach ($myfen as $kkk => $vv) {
        $fsinfo[] = $redis->hget("user:{$vv}",‘username‘);
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>欢迎页</title>
    <style type="text/css">
        li{
            float: left;
            margin-left:20px;
            list-style-type:none;
        }
    </style>
</head>
<body>
<h3>当前用户</h3>
<p><?php
$myuid = $_COOKIE[‘myuid‘];
echo $redis->hget("user:{$myuid}",‘email‘);
?><span><a href="loginout.php">退出</a></span></p>
<hr/>
    <h3>用户列表</h3>
    <table>
        <tr>
            <th>用户ID</th>
            <th>用户</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
        <?php foreach($res as $v){ ?>
            <tr>
                <td><?php echo $v[‘uid‘];?></td>
                <td><?php echo $v[‘username‘];?></td>
                <td><?php echo $v[‘email‘];?></td>
                <td>
                    <a href="addfans.php?uid=<?php echo $v[‘uid‘];?>&myuid=<?php echo $_COOKIE[‘myuid‘];?>">加关注</a>
                    <!-- <a href="del.php?uid=<?php echo $v[‘uid‘];?>&myuid=<?php echo $_COOKIE[‘myuid‘];?>">删除</a> -->
                </td>
            </tr>
        <?php } ?>
    </table>
<hr/>
<li>
    <h3>我的关注</h3>
    <?php if($myguanzhu==null):?>
        暂无关注
    <?php else:?>
        <?php foreach($gzinfo as $k=>$va){ ?>
        <p><?php echo $va;?></p>
        <?php } ?>
    <?php endif;?>
</li>

<li style="margin-left:100px;">
    <h3>我的粉丝</h3>
    <?php if($myfen==null):?>
        暂无粉丝
    <?php else:?>
        <?php foreach($fsinfo as $a=>$b){ ?>
        <p><?php echo $b;?></p>
        <?php } ?>
    <?php endif;?>
</li>
</body>
</html>

实现关注和被关注:addfans.php

<?php
    include(‘./redis.php‘);
    $uid = $_GET[‘uid‘];
    $myuid = $_GET[‘myuid‘];
    if($uid==$myuid){
        echo ‘不能给自己点关注‘;
        exit;
    }

    //添加redis
    $redis->sadd("user:".$myuid.":following",$uid);
    $redis->sadd("user:".$uid.":followers",$myuid);
    echo "关注成功";
?>

最后是退出登录 loginout.php

<?php
    setcookie(‘myuid‘,null);
    header(‘location:login.php‘);
?>

基本上数据类型都是字符串或者hash

redis数据表预览

原文地址:https://www.cnblogs.com/findher/p/10610599.html

时间: 2024-08-30 17:41:54

使用redis技术实现注册登录列表以及关注功能的相关文章

redis+thinkphp5的注册、登陆、关注基础例子

最近初步接触redis,结合thinkphp5与redis,写了一个用户注册的基础例子,用于学习. 这个例子是结合了兄弟连的redis视频,最后两节的内容写的:https://study.163.com/course/courseMain.htm?courseId=265010 这个例子实现了以下功能: (1)用户注册(2)用户登陆(3)redis+thinkphp5的数据的增加,修改,删除(4)redis翻页功能(5)用户的关注列表+粉丝列表 案例需要搭建的环境需求:(1)thinkphp5(

Jquery+PHP实现简单的前后台数据交互实现注册登录,添加留言功能

页面样式应用了BootStrap框架. 首先看登录页(登录页用于账号登录,也可以跳转到注册账号页): <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录--杰瑞教育图书管理系统</title> <link rel="stylesheet" type="text/css" href="

一个基于Unix套接字的注册登录系统

2016/5/5 今天,我参考<Unix网络编程-卷1>第5章的TCP回射客户/服务器程序写了一个简单的注册登录系统,其功能如下:(1)注册.客户端向服务器发送个人信息请求注册,服务器查询MySQL数据库以检查该客户是否已存在,若是则禁止注册,并返回“用户已存在,注册失败”的错误信息,否则将新用户信息添加到MySQL数据库,并返回“注册成功”的信息.(2)登录.客户端向服务器发送个人账号和密码等两项信息,服务器查询MySQL数据库以检查账号是否存在.账号和密码是否匹配,若不存在或不匹配则禁止登

Web存储—简易注册登录

Web Storage是HTML5引入的一个非常重要的功能,可以在客户端本地存储数据,类似HTML4的cookie,但可实现功能要比cookie强大的多,cookie大小被限制在4KB,cookie只能存储一些短的字符串,Web Storage官方建议为每个网站5MB. 数据仓库 - 用于存储数据 Web Storage分为两种: 1.会话存储:sessionStorage ----替代Session(类似于内存),本地存储信息只在会话过程中使用,浏览器关闭也就没了 2.本地存储:localSt

RxSwift 实战操作【注册登录】

前言 看了前面的文章,相信很多同学还不知道RxSwift该怎么使用,这篇文件将带领大家一起写一个 注册登录(ps:本例子采用MVVM)的例子进行实战.本篇文章是基于RxSwift3.0写的,采用的是Carthage第三方管理工具导入的RxSwift3.0,关于Carthage的安装和使用,请参考Carthage的安装和使用. 最终效果 下载Demo点我 前提准备 首先请大家新建一个swift工程,然后把RxSwift引入到项目中,然后能够编译成功就行. 然后我们来分析下各个界面的需求: 注册界面

如何提高移动端注册登录体验

有多少用户愿意注册登录,决定了一款产品的活跃度.我们来谈一谈如何提高移动端的登录体验. 一.登录类型 用户通常有三种不同的方式来登录一个APP: 第三方授权登录的方式,优势是,省去用户注册这一流程,让用户可以在第三方授权下迅速登录.劣势是用户不是你的用户,是第三方的用户,流量可能只是暂时的,而且转化起来比较难.但我个人还是倾向于第三方授权登录的方式,因为第三方大多数都拥有海量的活跃用户,而且我们还可以在后期进行有针对性的转化—给用户一系列个性化设置.绑定手机号的操作流程. 最反感用第三方授权登录

一步步搭建自己的博客 .NET版(3、注册登录功能)

前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做个插件,任何网站上的技术文章都可以转发收藏 到本博客. 所以打算写个系类:<一步步搭建自己的博客> 一.一步步搭建自己的博客  .NET版(1.页面布局.blog迁移.数据加载) 二.一步步搭建自己的博客  .NET版(2.评论功能) 三.一步步搭建自己的博客  .NET版(3.注册登录功能) 四

python3学习--注册登录小程序

一.需求:#1.实现注册功能输入:username.passowrd,cpassowrd #最多可以输错3次 #3个都不能为空 #用户名长度最少6位, 最长20位,用户名不能重复 #密码长度最少8位,最长15位 #两次输入的密码要一致 #注册成功之后,要写到文件里面#2.登陆功能实现: #3次 用户名和密码你去文件里面取 二.代码 ''' 注册登录小程序 Author:Test_Lin ''' import datetime #先访问文件,将文件中的内容存到列表当中 f = open('Regi

express+vue+mongodb+session 实现注册登录

上个月写了一篇文章是 express+mongodb+vue 实现增删改查. 只是简单的实现了增删改查功能,那么今天是在那个基础之上做了扩展,首先实现的功能有如下: 1. 支持注册,登录功能,用户可以注册完成后,进行登录,登录完成后会进入到列表增删改查页面.2. 支持session会话,也就是说设置了多长时间登录过期,如果用户没有登录,直接进查询列表页面,会重定向到登录页面去,如果用户登录了后,把浏览器关掉,直接输入列表查询页面,会直接进入列表页面的.3. 列表数据加入了分页功能.4. 对数据库