PHP Session variable usage and example tutorial

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:

The code is as follows: Copy code
<? Php session_start ();?>
 
<Html>
<Body>
 
</Body>
</Html>

 

Store Session variables

The code is as follows: Copy code
<? Php
Session_start ();
// Store session data
$ _ SESSION ['View'] = 1;
?>
 
<Html>
<Body>
 
<? Php
// Retrieve session data
Echo "Pageviews =". $ _ SESSION ['View'];
?>
 
</Body>
</Html>

 

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

The code is as follows: Copy code
<? Php
Unset ($ _ SESSION ['View']);
?>

 

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

The code is as follows: Copy code
<? Php
Session_destroy ();
?>

 

Example

The code is as follows: Copy code

<? 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

The code is as follows: Copy code

<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

The code is as follows: Copy code

<? 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

The code is as follows: Copy code

<? 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

The code is as follows: Copy code


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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.