Home >Backend Development >PHP Tutorial >PHP+MYSQL membership system development example tutorial, mysql example tutorial_PHP tutorial
This article completes the complete PHP+MySQL membership system function through a simple example. It is a very practical application. The specific implementation steps are as follows:
1. Principle of membership system:
Login-->Judgment-->Keep status (Cookie or Session)-->Verify status and permissions
2. Security of membership system:
1. Learn to use constants to improve md5 security
2. Cookie/Session use less plain text information
3. Session security is greater than Cookie
4. Use Cookie/Session to read information and try to increase the judgment information
5. Cookie/Session content should be streamlined
6. Destroy Cookie/Session in time for error messages
3. Database test, table user_list, and its fields
uid m_id username password
1 1 1 admin 291760f98414679e3fd3f9051b19b6f7
2 2 2 admin2 895785cfa5d8157f4d33c58ae0f55123
Password: md5 (admintest100) and md5 (admin2test100) respectively. That is, the password is bound to the constant test100 and then encrypted and stored in the database. This step can be set during registration.
4. Configuration page m_config.php:
<?php session_start(); //数据库连接 $conn=mysql_connect('localhost','root',''); mysql_select_db('test',$conn); //定义常量 define(ALL_ps,"test100"); //查看登录状态与权限 function user_shell($uid,$shell,$m_id){ $sql="select * from user_list where `uid`='$uid'"; $query=mysql_query($sql); $us=is_array($row=mysql_fetch_array($query)); $shell=$us ? $shell==md5($row[username].$row[password].ALL_PS):FALSE; if($shell){ if($row[m_id]<=$m_id){//$row[m_id]越小权限越高,为1时权限最高 return $row; }else{ echo "你的权限不足,不能查看该页面"; exit(); } }else{ echo "登录后才能查看该页"; exit(); } } //设置登录超时 function user_mktime($onlinetime){ $new_time=mktime(); echo $new_time-$onlinetime."秒未操作该页面"."<br>"; if($new_time-$onlinetime>'10'){//设置超时时间为10秒,测试用 echo "登录超时,请重新登录"; exit(); session_destroy(); }else{ $_SESSION[times]=mktime(); } } ?>
5. Login page m_user.php:
<?php include("m_config.php"); //echo md5("admin2".ALL_PS); if($_POST[submit]){ $username=str_replace(" ","","$_POST[username]"); $sql="select * from user_list where `username`='$username'"; $query=mysql_query($sql); $us=is_array($row=mysql_fetch_array($query)); $ps=$us ? md5($_POST[password].ALL_PS)==$row[password] : FALSE; if($ps){ $_SESSION[uid]=$row[uid]; $_SESSION[user_shell]=md5($row[username].$row[password].ALL_PS); $_SESSION[times]=mktime();//取得登录时忘该的时间 echo "登录成功"; }else{ echo "用户名或密码错误"; session_destroy();//密码错误时消除所有的session } } ?> <form action="" method="post"> 用户名:<input name="username" type="text" /><br /> 用户名:<input name="password" type="password" /><br /> 验证码:<input name="code" type="code" />5213<br /><br /> <input name="submit" type="submit" value="登录" /> </form>
6. Page m_zhuangtai.php with permissions and timeout set:
<?php include("m_config.php"); $arr=user_shell($_SESSION[uid],$_SESSION[user_shell],1);//设置该页面只有权限为1时即最高权限的才能访问 user_mktime($_SESSION[times]);//判断是否超时10秒 //echo $_SESSION[times]."<br>";//登录时该的时间 //echo mktime()."<br>";//当前日期 //echo $arr[username]."<br>"; //echo $arr[uid]."<br>"; ?>
Content that can only be viewed with permission
7. Test results:
1. Log in with sss, sssssss, prompt: wrong username or password. View m_zhuangtai.php, prompt: You must log in to view this page.
2. After logging in with admin admin, because the permission is 1, you can view the content of the m_zhuangtai.php page.
3. After logging in with admin admin, because the permission is 1, you can view the content of the m_zhuangtai.php page, but if you refresh it after 10 seconds, it will prompt: The page has not been operated for X seconds. The login has timed out, please log in again.
4. After logging in with admin2 admin2, because the permission is 2, the content of the m_zhuangtai.php page cannot be viewed. Tip: You have insufficient permissions and cannot view the page.
I hope the examples described in this article will be helpful to everyone in PHP program development.
The original book does not come with a CD or something. You may consider contacting the author and asking if he can give it to you. If it really doesn't work, you can follow the content in the book and you will gain a lot. If you have any questions, you can ask me at any time.
There are many online, do you need a framework?