Home>Article>Backend Development> Detailed example of how to judge user operation permissions through Session

Detailed example of how to judge user operation permissions through Session

伊谢尔伦
伊谢尔伦 Original
2017-04-28 11:43:52 6065browse

In most website development processes, it is more practical to divide the permissions of administrators and ordinary users to operate the website. Let's give a comprehensive explanation through a specific code example. To give friends a more intuitive experience, you can combine the knowledge you have learned previously to improve your code and ideas.

First, verify the user's authority to operate the website through the user information submitted on the user login page.

The specific development steps are as follows:

(1) Design a login page, add a form, use POST method to transfer parameters, the data processing page pointed to by action is default.php , add a username text box and name it user, add a password field text box and name it pwd, and perform a submission jump through the submit button. The main code is as follows.

    Title  
用户名:
密 码:
超级用户: admin 密 码:111

普通用户: tom 密 码:000

(2) Under the click event of the "Submit" button, call the custom function check() to verify whether the form element is empty. The javascript code is used here for verification. Please supplement your knowledge about javascript by yourself. The custom function check() code is as follows:

(3) Submit the form elements to the data processing page default.php. First use the session_start() function to initialize the session variable, then use the POST method to receive the value of the form element, and assign the obtained user name and password to the session variable respectively. The code is as follows:

(4) To prevent Other users illegally log in to the system and use if conditional statements to judge the value of the session variable. The knowledge of javascript is continued here. The code is as follows:

alert("请使用正确途径登录"); history.back();'; } ?>

(5) In the data processing page default.php Add the following navigation bar code to determine the level of the current user to see whether the logged-in user is an administrator or an ordinary user, and then the differentiated output is displayed:

当前用户: 首页 |文章 |相册 |修改密码 |用户管理'; } ?> |注销用户

(6) In the default.php page above Add the "Logout User" hyperlink page safe.php, and write the following code in safe.php to delete the user Session and return to the login page:

(7) Run this example on the website Enter your username and password on the user login page, log in to the website as a super user or log in as an ordinary user and run it.

php Chinese website learning topic:php session(including pictures, texts, videos, cases)

The above is the detailed content of Detailed example of how to judge user operation permissions through Session. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn