How does php know who the poster is (which registered user)

Source: Internet
Author: User
How does php know who the poster is (which registered user) and studies the session () function:
1. a visitor enters the website to browse a post. he wants to comment! The login. php logon page is displayed! How is this implemented ?? How it works !!
2. post after a user logs on. how does php know which registered user posted the post! How is this implemented? How it works!


Reply to discussion (solution)

User registration, registration information is saved to the database by querying the database.

After a user logs on, the user name is stored in the session
Check whether logon information exists in the session before making a comment.

Php does not know which registered user posted the post
You need to associate the user name with the post by yourself

Not thoroughly studied
After logon, the session records the user id. In other words, this id does not exist in the session variable when you are not logged on. you can determine the jump.

The user is the user according to sessionID, and all information is reported in the session.

The user is the user according to sessionID, and all information is reported in the session. how does php know who posted the post according to sessionID? Thank you.


The user is the user according to sessionID, and all information is reported in the session. how does php know who posted the post according to sessionID? Thank you.

Read database fields...

The session () function is studied as follows:
1. a visitor enters the website to browse a post. he wants to comment! The login. php logon page is displayed! How is this implemented ?? How it works !!
2. post after a user logs on. how does php know which registered user posted the post! How is this implemented? How it works!

I have seen Han Shunping's php video telling you about this problem. in his video, I explained the session and gave a case study on how to prevent illegal user login, this is the question you are currently asking.

1. when a user logs on, the user's login name and password (the verification code is ignored first) are transmitted to the server, that is, the corresponding php verification script. The script obtains the user table from the database for verification.
2. if it is legal, that is, the user's login name and password are successfully logged on, the php program (specifically responsible for redirect) will open the session mechanism (session_start) and store the user's login name into the session, and jump. If it is illegal (incorrect logon), go to the logon page and ask the user to re-enter the user name and password.

The code is as follows:
$ AdminService = new AdminService ();
$ Name = $ adminService-> checkAdmin ($ id, $ password );
If ($ name ){
// Valid
Session_start ();
$ _ SESSION ['loginuser'] = $ name;
Header ("Location: empManage. php? Name = $ name ");
// If you want to jump, you 'd better exit ();
Exit ();
} Else {
// Invalid
Header ('Location: login. php? Errno = 1 ');
Exit ();
}

3. create a public script, for example, common. php. in this script, we define a method as follows:
Function checkUserValidate (){
Session_start ();
If (empty ($ _ SESSION ['loginuser']) {
Header ("Location: login. php? Errno = 1 ");
}

}
4. introduce the following two sentences (these two sentences are placed at the beginning of the page) in the pages to prevent unauthorized user logon (for example, the posting page you mentioned)
Require_once 'common. php ';
CheckUserValidate ();
Users who have not logged in cannot enter the posting page. if a successfully logged in user enters the page, the program can easily obtain the user information from the session.

I don't know if you understand it now. if you want to know more details, you can refer to Han Shunping's video lecture 119 and the second half of the case. I'm exhausted. Haha!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.