PHP Session variable usage and instance code

Source: Internet
Author: User
Tags php session

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:

Copy codeThe Code is as follows:
<? Php session_start ();?>

<Html>
<Body>

</Body>
</Html>

Store Session Variables

Copy codeThe Code is as follows:
<? Php
Session_start ();
// Store session data
$ _ SESSION ['view'] = 1;
?>
<Html>
<Body>

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

</Body>
</Html>
[Html]

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

[Code]
<? Php
Unset ($ _ SESSION ['view']);
?>

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

Copy codeThe Code is as follows:
<? Php
Session_destroy ();
?>

Instance:

Copy codeThe Code is as follows:
<? Php
Session_start ();
Switch ($ _ GET ['action']) {
Case "loginif ";
// 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! You can 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;
}
?>
<P> assume the name of this page is temp. php. </p>
<P> <a href = "temp. php? Action = login "> the user logs on to post and processes the data written to the session. </a> </p>
<P> <a href = "temp. php? Action = loginif "> determine whether the user password is correct </a> </p>
<P> <a href = "temp. php? Action = logout "> login successful user logout </a> </p>
 

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 sessions.
(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", then call the function "session_register (login);", and the session is successfully registered.
(3) Use the variables in the session
Similar to registering a session, you must first create a global array, and then use a general array.
(4) determine whether a session is registered
It is easy to use the "if (session_is_registered (login)" statement.
(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

Copy codeThe Code is as follows:
<Html>
<Head>
<Title> test </title>
</Head>
<Body>
<Form method = post action = "login. php">
Username: <input type = "text" NAME = "name"> <br/>
Password: <input type = "password" name = "pwd"> <br/>
<Input type = "submit" value = "submit">
</FORM>
</Body>
</Html>
 

Login. php

Copy codeThe Code is as follows:
<? Php
Global $ login;
If ($ _ POST ['name']! = "Victor" | $ _ POST ['pwd']! = "111111 ")
{
Echo "Logon Failed ";
Echo "Please <a href1_index.htm> return </a> ";
Exit;
}
$ Login = array ('name' = >$ _ POST ['name'],
'Pwd' = >$ _ POST ['pwd']);
Session_start ();
Session_register (login );
Echo "<a href = info. php> View information </a> <br/> ";
Echo "<a href = logout. php> log out </a> <br/> ";
?>
 

Info. php

Copy codeThe Code is as follows:
<? Php
Session_start ();
If (session_is_registered (login ))
{
Global $ login;
Echo "hello,". $ login ['name']. "<br/> ";
Echo "<a href = logout. php> log out </a> <br/> ";
}
Else
{
Echo "illegal operation <br/> ";
Exit;
}
?>


Logout. php

Copy codeThe Code is as follows:
<? Php
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.