This article mainly introduces the Php+redis implementation of registration, deletion, editing, paging, login, attention and other functions, combined with examples of the development of PHP+REDIS projects commonly used in the registration, deletion, editing, paging, login, pay attention to the function of relevant implementation skills, the need for friends can refer to the next
This article describes the Php+redis implementation of registration, deletion, editing, paging, login, attention and other functions. Share to everyone for your reference, as follows:
Main interface
Connect to Redis
redis.php
<?php //instantiation $redis = new Redis (); Connection server $a = $redis->connect ("localhost", 6379); Var_dump ($a); Authorized $redis->auth ("107lab");
Registration interface
add.php
<form action= "reg.php" method= "POST" > user name: <input type= "text" name= "username" ><br> password: <input type= "password" name= "password" ><br> ages: <input type= "text" name= "age" ><br> <input type= "Submit" value= "register" > <input type= "reset" value= "refill" ></form>
Registration implementation
reg.php
<?php require ("redis.php"); $username = $_post[' username '); $password = MD5 ($_post[' password '); $age = $_post[' age ']; echo $username. $password. $age; $uid = $redis->incr ("userid");//Set the self-increment ID equivalent to the primary key $redis->hmset ("User:". $uid, Array ("UID" and "= $uid," username "= $username," password "= $password," Age "and" $age) ");//Use hash type to store users easily //save user ID in a linked list for statistical data $ Redis->rpush ("UID", $uid); The ID is stored in the character type with the user name key, making it easy to see if the user exists. $redis->set ("Username:". $username, $uid); Header (' location:list.php ');
List page
list.php
<a href= "add.php" rel= "external nofollow" > Registered </a><?php require ("redis.php"); if (!empty ($_cookie[' auth '))) {$id = $redis->get ("auth:". $_cookie[' auth ']); $name = $redis->hget ("User:". $id, "username");? > Welcome you: <?php echo $name;? > <a href= "logout.php" rel= "external nofollow" > Exit </a> <?php} else {?> <a href= "login.php" rel = "External nofollow" > Login </a> <?php}? ><?php require ("redis.php"); The total number of users $count = $redis->lsize ("UID");//Get the length of the list//echo $count; Page size $page _size = 3; Current page number $page _num= (!empty ($_get[' page '))? $_get[' page ']:1; Total pages $page _count = ceil ($count/$page _size); $ids = $redis->lrange ("UID", ($page _num-1) * $page _size, (($page _num-1) * $page _size+ $page _size-1)); Var_dump ($ids); foreach ($ids as $v) {$data []= $redis->hgetall ("User:". $v); }/*//The following is the first thought of paging processing, put into an array, according to the maximum value of the UID when the total number, but after the deletion of individual users, the UID will not be smaller, it is recommended to use the linked list, because he has a lsize function can find the link table length//UserID Get all user for ($i =1; $i <= ($redis->get ("UseRid ") ($i + +) {$data []= $redis->hgetall (" User: ". $i); }//filter null value $data = Array_filter ($data); Var_dump ($data); */?><table border=1> <tr> <th>uid</th> <th>username</th> <th>age< ;/th> <th> Operations </th> </tr> <?php foreach ($data as $v) {?> <tr> <td><?php E Cho $v [' uid ']?></td> <td><?php echo $v [' username ']?></td> <td><?php echo $v [' Age ']?></td> <td> <a href= ' del.php?id=<?php echo $v [' uid '];? > "rel=" External nofollow "> Delete </a> <a href=" mod.php?id=<?php echo $v [' uid '];? > "rel=" External nofollow "> Edit </a> <?php if (!empty ($_cookie[' auth ']) && $id! = $v [' uid ']) {? &G T <a href= "addfans.php?id=<?php echo $v [' uid '];? >&uid=<?php echo $id;? > "rel=" External nofollow "> Add attention </a> <?php}?> </td> </tr> <?php}?> <tr> <td colspan= "4" > <?php if (($page _num-1) >=1) {?> <a href= "? page=<?php Echo ( $page _num-1);? > "rel=" External nofollow "> previous </a> <?php}?> <?php if (($page _num+1) <= $page _count) {? &G T <a href= "? page=<?php Echo ($page _num+1);? > "rel=" External nofollow "> Next </a> <?php}?> <a href="? page=1 "rel=" External nofollow "&G t; home </a> <a href= "? page=<?php Echo ($page _count);? > "rel=" External nofollow "> Last </a> current <?php echo $page _num;? > Page Total <?php echo $page _count;? > Total <?php echo $count;? > Users </td> </tr></table><!--Focus, it is recommended to implement the collection, because the collection element is unique, and it is easy to find the intersection and difference between the two user fans, and then the friend referral function- <table border=1> <caption> I was concerned about who </caption> <?php $data = $redis->smembers ("User:". $id. ": Follo Wing "); foreach ($data as $v) {$row = $redis->hgetall ("User:". $v); ?> <tr> <TD≫<?php echo $row [' uid '];? ></td> <td><?php echo $row [' username '];? ></td> <td><?php echo $row [' age '];? ></td> </tr> <?php} ><table><table border=1> <caption> my fans </caption> & lt;? PHP $data = $redis->smembers ("User:". $id. ": Followers"); foreach ($data as $v) {$row = $redis->hgetall ("User:". $v); ?> <tr> <td><?php echo $row [' uid '];? ></td> <td><?php echo $row [' username '];? ></td> <td><?php echo $row [' age '];? ></td> </tr> <?php}?><table>
Exit
logout.php
<?php Setcookie ("auth", "", Time ()-1); Header ("location:list.php");
Login
login.php
<?php require ("redis.php"); $username = $_post[' username '); $pass = $_post[' password '); Finds the user id $id = $redis->get ("username:"), based on the character type stored in the user name key at registration. $username); if (!empty ($id)) { $password = $redis->hget ("User:". $id, "password"); if (MD5 ($pass) = = $password) { $auth = MD5 (Time (). $username. Rand ()); $redis->set ("auth:". $auth, $id); Setcookie ("Auth", $auth, Time () +86400); Header ("location:list.php"); } ? ><form action= "" method= "POST" > user name: <input type= "text" name= "username"/><br> password: <input type= "password" name= "password" ><br> <input type= "Submit" value= "Login"/></form>
Delete
del.php
<?php require ("redis.php"); $uid = $_get[' id ']; echo $uid; $username = $redis->hget ("User:". $id, "username"); $a = $redis->del ("User:". $uid); $redis->del ("Username:". $username); $redis->lrem ("UID", $uid); Var_dump ($a); Header ("location:list.php");
Edit interface
mod.php
<?php require ("redis.php"); $uid = $_get[' id ']; $data = $redis->hgetall ("User:". $uid);? ><form action= "doedit.php" method= "POST" > <input type= "hidden" value= "<?php echo $data [' uid '];? > "name=" UID "> user name: <input type=" text "name=" username "value=" <?php echo $data [' username '];? > "><br> Age: <input type=" text "Name=" ages "value=" <?php echo $data [' aged '];? > "><br> <input type=" Submit "value=" Submission "> <input type=" reset "value=" refill "></form >
Editing features
doedit.php
<?php require (' redis.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); }
Add attention
addfans.php
<?php//focus on the function, it is recommended to implement the collection, because the collection element is unique, and can easily find two users of the intersection and the difference between fans, and then the friend referral function $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");
The above is the whole content of this article, I hope that everyone's study has helped.