Home > Backend Development > PHP Tutorial > Detailed explanation and example code of using PHP Session variables_PHP tutorial

Detailed explanation and example code of using PHP Session variables_PHP tutorial

WBOY
Release: 2016-07-21 16:12:38
Original
895 people have browsed it

When you run an app, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state.
PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.

Copy the manual, try each one and write it out for your own reference. Who told us to just learn it? Session has about 12 functions:

session_start: initial session.
session_destroy: End session.
session_unset: Release session memory.
session_name: access the current session name.
session_module_name: access the current session module.
session_save_path: access the current session path.
session_id: access the current session code.
session_register: Register new variables.
session_unregister: Delete registered variables.
session_is_registered: Check whether the variable is registered.
session_decode: Session data decoding.
session_encode: Session data encoding.

There is also a global variable: $_SESSION


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

Copy code The code is as follows:



< ;html>




Storing Session variables

Copy code The code is as follows:

session_start();
// store session data
$_SESSION['views']=1;
?>



//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>



[html]

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

[code]
unset($_SESSION['views']);
?>

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

Copy code The code is as follows:

session_destroy();
?>

Example:

Copy code The code is as follows:

session_start();
switch ( $_GET ['action'] ){
case "loginif";
//Login verification, it is assumed that the secret stored in the session should be equal to 123 to be correct
if ($_SESSION['pass']=="123 "){echo "If the password is correct, you can log out";}else{echo "The password is incorrect, you can log in again";}
break;
case "logout";
//Logout
session_unset();
session_destroy();
echo "Logout successful! You can check whether the password is correct to see if the logout is successful";
break;
case "login";
//Write the session for verification,
$pass="123";//Password
$_SESSION['pass']=$pass;
echo "Write the login password to determine the password ";
break;
}
?>

Assume this page is called temp.php


< ;a href="temp.php?action=login">The user performs a login post, and the program handles writing to the session


Determine whether the user password is correct


Users who successfully log in log out Login


I summarized the usage of session in php.

(1) Start session
Before each use of session, add this sentence: "session_start();". As the name suggests, the function of this function is to start using the session.
(2) Register session
First, create a global (note, it must be defined as global, otherwise it cannot 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) Using variables in the session
Similar to registering a session, you must first create a global array, and then it is the same as using a normal array.
(4) Determine whether the session is registered
It is very simple, just use "if (session_is_registered(login))" to judge.
(5) Uninstalling session
It is also very simple, just "session_unregister(login);".
Note: Be sure to do (1) before doing (2) (3) (4) (5).


An example is given below:

index.htm

Copy code The code is as follows:



Test



Username:

Password:





login.php

Copy code The code is as follows:

global $login;
if ($_POST ['name']!="Victor" || $_POST['pwd']!="111111")
{
echo "Login failed";
echo "PleaseReturn";
exit;
}
$login = array('name'=>$_POST['name'],
'pwd'=> ;$_POST['pwd']);
session_start();
session_register(login);
echo "View information
";
echo "Logout
";
?>

info.php

Copy code The code is as follows:

session_start();
if (session_is_registered( login))
{
global $login;
echo "hello,".$login['name']."
";
echo "Log out
";
}
else
{
                                                                                                                                                                                                                                             ;
}
?>

logout.php

Copy code The code is as follows:
session_start();
session_unregister(login) ;
header("location:index.htm");
?>

http://www.bkjia.com/PHPjc/313634.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313634.htmlTechArticleWhen you run an application, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you launch the app and...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template