在PHP中的Redis简单应用

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");


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值