


Analysis of practical PHP member permission control implementation principles_PHP tutorial
My general permission system design is to try not to involve code modification when changing permissions. It comes from the chinaunix forum. I turned it over today to take a look. I hope it will be helpful to everyone and will be a great improvement for bkJia friends.
/*
*Control access table
* Acl value function
* 1 Requires login
* 2 Self-modification
* 4 Requires group permission set
* 8 Requires identity access set
🎜> * 64 Accessible Sundays
* 128 Accessible times
* 256 Enter password to access
* 512 Super management use
*/
class aclACL extends acl {
Public $routername="acl";
public $aclid='2'; //Permission resource ID, if the logged-in person does not have this permission, then the other values (below) are 0 and cannot be accessed
public $roledisable=array(9); //Disable identity
public $pwd=123456; //Password access ACL->noPwd();
public $date=array('begin'=>0 ,'end'=>0); //Allow dates between
public $hours=array('begin'=>0,'end'=>0); //Hour range within a day
Public $weeks=array('begin'=>0,'end'=>0); //Monday to Saturday within a week
public $aclgroup=array("create"=>"4, 45,8"); //create requires groups to create
public $aclrole=array("all"=>"6","create"=>"7,95,78"); // Create requires the role to be created. This group requires the role with ID 6 to access
public $acl=array("all"=>0,
"index"=>4, //Table 4 Indicates the combination of inspection groups
"delete"=>1, //Delete can only be deleted after logging in, of course it can be set to 2 or 4
"update"=>1, //Update submission can only be done by logging in It can be updated after doing it here to prevent illegal and post. Edit cannot access the display edit content page
"createForm"=>1, //It is also impossible to submit a new database
"edit"=>0, //The edit box is displayed only after logging in
"show"=>0, //It can be displayed without logging in
"create"=>1); //Innovation forms require login operations and can set a certain group to be able to Create
}
?>
This is the file module to be authenticated is acl
Then this class will perform authentication checks based on the value of all or index of $acl.
Just put this file in the router/acl directory. The framework will automatically authenticate. If the user does not have the corresponding forward authorization, he will not be able to access the corresponding restrictions.
For example, the negative permission of the crud create method is 17. According to the previous explanation, login and group authorization should be required, which are the three groups 4 45 8 of create in the $aclgroup array.
First, if the member is not logged in, he will be prompted to log in. If the member is not logged in, he will be prompted to log in. If you are not in these three groups and cannot access this method, you will be prompted that you do not have permission.
At present, the router can enable acl control according to the situation
The method is to add public function isAcl(){} in the xxxxRouter.class.php file
You can return the permission file name, such as returning curd, then it will be called automatically curdACL.class.php class and name
CurdRouter class setting verification
//You can not write this function, then the universal permission system will not be enabled.
public function isAcl(){}
public function index()
{
$booktype=M("booktype");
$this->pager=C("pager" ); //Get the category
$this->pager->setPager($booktype->count(),10,'page');//Get the total number of data, set each page to 10
$this->assign("list",$booktype->orderby("bookid desc")->limit($this->pager->offset(),10)->fetch() ->getRecord());
}
public function login(){ //Login page
}
public function logout(){ //Exit page
MY()-> ;logout(); //Log out
redirect(url_for("guestbook/index"),"Logout successful",3);
}
public function noAcl($mask) { //Process it If there is no permission, redirect to login
redirect(url_for("guestbook/login"),"Login required",3);
}
public function loginpost() { //Login submission place simply handles login authentication
if($_POST['author']=='queryphp'&&md5($_POST['pwd'])==md5('123456'))
{
MY()->setLogin (); //Set login status
redirect(url_for("guestbook/adminlist"),"Login successful",3);
}
redirect(url_for("guestbook/login"),"Login Failed",3);
}
/*
* Basic login information class
* Permission table can be cached Data is restored when logging in.
*/
class mybase {
public $options=array();
public $uid;
public $username;
public $isadmin;
public $role= array(); //The identity I use
public $group=array(); //The group I am in
public $grouprole=array(); //The identity of the group
public $mygroupMar=array (); //The group I own and manage
public $mygroupOwn=array(); //The group that belongs to me
public $acl=array(); // Active control table groupacl and myacl control permission collection content It is rbacid of rbac
public $groupacl=array(); //Control permissions owned by the group
public $myacl=array(); //Control permissions owned by my identity
public $loginfaild =0; //If the number of failed logins exceeds this number, how many minutes should the IP login be banned?
This is basic
You can put myUser.class.php in the project lib directory
Copy the code and use the MY() function to get myUser.


























You can view the framework file
There is a guestbookRouter.class.php in the project/router directory
In the background

Get the guestbookRouter.class.php class name and method.
Then add permissions to these methods

There is an application permission on the right and a cancellation permission. If you cancel the permission, it means there is no permission restriction
That is to delete the permission file
Apply permissions means adding permissions to this class will generate a permissions file.
Generate the guestbookACL.class.php file in project/router/acl/
When the program loads guestbookRouter.class.php, it will check whether there is a guestbookACL.class.php permission file
If yes, use permission verification, if not, then no. Adding and subtracting permissions in this way does not change the entry of the guestbookRouter.class.php file
So it will be very convenient to add permissions in the future.
http://queryphp.googlecode.com/files/queryphp_2011_01_27.zip

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
