您的位置:首页 > 数据库 > Redis

在PHP中的Redis简单应用

2013-11-19 17:56 393 查看
Redis连接页面

$redis = new Redis();
$redis -> connect("127.0.0.1","6379");


添加页面

<form action="reg.php" method="post">
用户名:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
年龄:<input type="text" name="age" /><br />
<input type="submit" value="注册" />
<input type="reset" value="重新填写" />
</form>

require_once("redis.php");
$username = $_POST['username'];
$password = md5($_POST['password']);
$getUid = 'uid'.md5($username);
$getRedisUid = $redis->get($getUid);
if( ! empty($getRedisUid) ){
echo "用户已经存在,<a href='add.php'>单击返回</a>";
}else{
$age = $_POST['age'];
$uid = $redis->incr("incrUid");
$redis->hmset("user".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));
$redis->rpush("uid",$uid);
$redis->set($getUid,$uid);
header("Location:list.php");
}


显示页面

<?php
require_once("redis.php");
//判断登录
if( isset($_COOKIE['loginAuth']) ){
$userArrray = $redis->hmget("user".$_COOKIE['loginAuth'],array('uid','username'));
}

//分页处理
$total = $redis->lsize("uid");
$psize = 3;
$pageCount = ceil($total/$psize);
$page = (! empty($_GET['page']))?$_GET['page']:1;
$uidArray = $redis->lrange("uid",($page-1)*$psize,($page-1)*$psize+$psize-1);
$data = array();
foreach($uidArray as $val){
$data[]=$redis->hgetall("user".$val);
}

?>
<a href="add.php">添加</a>      <?php if( isset($userArrray)) { ?>欢迎,<?php echo $userArrray['username']?>   <a href="logout.php">注销</a><?php } ?>    <a href="login.php">登录</a><br /><br />
<table border=1>
<tr>
<th>uid</th>
<th>username</th>
<th>age</th>
<th>操作</th>
<tr>
<?php foreach($data as $val) {?>
<tr>
<th><?php echo  $val['uid']?></th>
<th><?php echo  iconv("gbk","utf-8",$val['username'])?></th>
<th><?php echo  $val['age']?></th>
<th><a href="mod.php?uid=<?php echo $val['uid'];?>">修改</a>   <a href="del.php?uid=<?php echo $val['uid'];?>">删除</a></th>
<tr>
<?php } ?>
<tr>
<th colspan="4">
<a href="list.php?page=1">首页</a>
<?php if($page > 1) { ?>
<a href="list.php?page=<?php echo $page-1; ?>">上页</a>
<?php } ?>
<?php if($page < $pageCount) { ?>
<a href="list.php?page=<?php echo $page+1; ?>">下页</a>
<?php } ?>
<a href="list.php?page=<?php echo $pageCount?>">尾页</a>
</th>
<tr>
</table>


修改页面

<?php
require_once("redis.php");
$uid = $_GET['uid'];
if( $uid ){
$data = $redis->hgetall("user".$uid);
}
?>
<form action="edit.php" method="post">
<input type="hidden" value="<?php echo $data['uid']?>" name="uid" />
用户名:<input type="text" name="username" value="<?php echo $data['username']?>" /><br />
年龄:<input type="text" name="age" value="<?php echo $data['age']?>" /><br />
<input type="submit" value="修改" />
<input type="reset" value="重新填写" />
</form>

require("redis.php");
$username = $_POST['username'];
$age = $_POST['age'];
$uid = $_POST['uid'];
$getUid = 'uid'.md5($username);
$redis->set($getUid,$uid);
$bool = $redis -> hmset("user".$uid,array("username"=>$username,"age"=>$age));
if( $bool ){
header("Location:list.php");
}else{
header("Location:mode.php?uid=".$uid);
}


删除页面

require_once("redis.php");
$uid = $_GET['uid'];
if( isset($uid) ){
$username = $redis->hget('user'.$uid, 'username');
$getUid = 'uid'.md5($username);
$redis->del($getUid);
$redis->del("user".$uid);
$redis->lrem("uid",$uid);
}
header("Location:list.php");


登录页面

<?php
require_once("redis.php");
if( isset( $_POST['submit'] ) ){
$username = $_POST['username'];
$password = md5($_POST['password']);
$getUid = 'uid'.md5($username);
$uid = $redis->get($getUid);
if( ! empty($uid) ){
$userArray = $redis->hgetall("user".$uid);
if( ( $username != $userArray['username'] )  || ( $password != $userArray['password'] ) ){
echo "用户名或者密码不正确,<a href='login.php'>重新登录</a>";
}else{
//成功登录
setcookie("loginAuth",$uid,time()+ 3600*30);
header("Location:list.php");
}
}else{
echo "用户不存在,<a href='login.php'>重新登录</a>";
}
}
?>
<form action="" method="post">
用户名:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" name="submit" value="登陆" />
</form>


注销页面

setcookie("loginAuth",$uid,time() -1 );
header("Location:list.php");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: