redis+php实战之小型的用户管理系统

一.redis.php

Php代码:

//实例化

$redis=newRedis();

//连接服务器

$redis->connect("127.0.0.1");

//授权

$redis->auth("haitao");

//相关操作

$redis->set("name","haitao");

$data=$redis->key("*");

var_dump($data);


二.简单回顾redis的四种数据类型

a.string:最简单的数据类型

Unix代码

set user:001:name lijie

set user:001:age20

b.hash:可以当做表,hash table,比string速度快

Unix代码

hset user:001name lamp age20

hset user:001sex nan

hset user:002name lijie age20

hgetall user:001

c.list:栈、队列

d.set:并集、交集、差集

e.zset:set升级版,多了一个顺序

三.小型的用户管理系统(用户的增删改查、分页、登陆退出、加关注)

redis.php

//实例化

$redis=newRedis();

//连接服务器

$redis->connect("localhost");

//授权

$redis->auth("lamplijie");


add.php


redis+php实战之小型的用户管理系统_第1张图片

reg.php

require("redis.php");

$username=$_POST['username'];

$password= md5($_POST['password']);

$age=$_POST['age'];

echo$uid=$redis->incr("userid");

$redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));

$redis->rpush("uid",$uid);

$redis->set("username:".$username,$uid);

header("location:list.php");


list.php


redis+php实战之小型的用户管理系统_第2张图片


redis+php实战之小型的用户管理系统_第3张图片


redis+php实战之小型的用户管理系统_第4张图片


redis+php实战之小型的用户管理系统_第5张图片

del.php

require("redis.php");

$uid=$_GET['id'];

$redis->del("user:".$uid);

$redis->lrem("uid",$uid);

header("localhost:list.php");


mod.php


redis+php实战之小型的用户管理系统_第6张图片

doedit.php

$uid=$_POST['uid'];

$username=$_POST['username'];

$age=$_POST['age'];

$a=$redis->hmset("user:".$uid,array("username"=>$username,"age"=>$age));

if($a) {

header("location:list.php");

}else{

header(location:mod.php?id=".$uid);

}


login.php


redis+php实战之小型的用户管理系统_第7张图片

logout.php

setcookie("auth","",time()-1);

header("location:list.php");


addfans.php

$id=$_GET['id'];

$uid=$_GET['uid'];

require("redis.php");

$redis->sadd("user:".$uid.":following",$id);

$redis->sadd("user:".$id.":followers",$uid);

header("location:list.php");


当然,采用sdiff user:1:following user:2:following语句,用户1可以向用户2推荐关注(即用户1的关注与用户2的关注的差集)。

你可能感兴趣的:(redis+php实战之小型的用户管理系统)