PHPSession variable usage and sample code

Source: Internet
Author: User
PHPSession variable usage and instance code (conversion )???? In php, Session is often used to verify user registration or verification after login. next I will summarize some common examples and usage of session variables ?? When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, on the Internet, the usage of PHP Session variables and instance code (to) exist)


? ? ? ? In php, Session is often used to verify user registration or verification after logon. next I will summarize some common examples and usage of session variables.

?

?

When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.

Copy the manual and try every one and then write it out so that you can check the manual easily. who asked me to learn it. The Session has about 12 functions:

Session_start: initial session.
Session_destroy: ends the session.
Session_unset: releases the session memory.
Session_name: name of the current session to be accessed.
Session_module_name: Access the current session module.
Session_save_path: the current session path.
Session_id: Access the current session code.
Session_register: registers new variables.
Session_unregister: deletes registered variables.
Session_is_registered: Check whether the variable is registered.
Session_decode: decodes Session data.
Session_encode: Session Data encoding.

Another global variable is $ _ SESSION.


Before you store user information in a PHP session, you must start the session.
Note: The session_start () function must be located before the tag:

?

The code is as follows:









?

?

Store Session variables

?

The code is as follows:


Session_start ();
// Store session data
$ _ SESSION ['View'] = 1;
?>?



// Retrieve session data
Echo "Pageviews =". $ _ SESSION ['View'];
?>



? [Html]

?

End Session
The unset () function is used to release the specified session variable:

[Code]
Unset ($ _ SESSION ['View']);
?>
?

?

You can also use the session_destroy () function to completely terminate the session:

?

The code is as follows:


Session_destroy ();
?>
?

?

Instance:

?

The code is as follows:


Session_start ();?
Switch ($ _ GET ['action']) {?
Case "loginif ";?
// For login verification, it is assumed that the secret stored in the session should be 123 to be correct?
If ($ _ SESSION ['pass'] = "123") {echo "the password is correct, you can cancel the operation";} else {echo "the password is incorrect, you can log on again ";}?
Break ;?
Case "logout ";?
// Log out?
Session_unset ();?
Session_destroy ();?
Echo "logout successful! Check whether the password is correct to see if it is successfully logged out ";?
Break ;?
Case "login ";?
// Write the session for verification ,?
$ Pass = "123"; // password?
$ _ SESSION ['pass'] = $ pass ;?
Echo "the login password is written to determine whether the password is successful or not. ";?
Break ;?
}?
?>?

Assume the name of this page is temp. php.

?

The user logs on to the post, and the program processes and writes data to the session.

?

Determine whether the user password is correct

?

Login successful user logout login


?

?

I have summarized the usage of session in php.

(1) start session?
Before each session is used, add the following sentence: "session_start ();". As the name suggests, this function is used to start using session .?
(2) register a session?
First, create a global (be sure to define it as global, otherwise it will not be used on other pages) array, such as $ login, where $ login ['name'] = "Victor ", $ login ['pwd'] = "111111", and then call the function "session_register (login);". The session is successfully registered .?
(3) use the variables in the session?
Like registering a session, you must first create a global array, and then use a general array .?
(4) determine whether the session is registered?
It's easy. you can use "if (session_is_registered (login)" to judge it .?
(5) uninstall the session?
It is also very simple. "session_unregister (login);" is enough .?
Note: Before (2) (3) (4) (5), you must first perform (1 ).


The following is an example:

Index.htm

?

The code is as follows:


?
?
Test?
?
?
?
?

?

?

Login. php

?

The code is as follows:


Global $ login ;?
If ($ _ POST ['name']! = "Victor" | $ _ POST ['pwd']! = "111111 ")?
{?
??????? Echo "login failed ";?
??????? Echo "Please return ";?
??????? Exit ;?
}?
$ Login = array ('name' = >$ _ POST ['name'],?
?????????????????????????? 'Pwd' => $ _ POST ['pwd']);?
Session_start ();?
Session_register (login );?
Echo "view information
";?
Echo "log out
";?
?>
?

?

Info. php

?

The code is as follows:


Session_start ();?
If (session_is_registered (login ))?
{?
??????? Global $ login ;?
??????? Echo "hello,". $ login ['name']."
";?
??????? Echo "log out
";?
}?
Else?
{?
??????? Echo "illegal operation
";?
??????? Exit ;?
}?
?>

?


Logout. php

?

The code is as follows:


Session_start ();?
Session_unregister (login );?
Header ("location: index.htm ");?
?>
?

?

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.