Home > Backend Development > PHP Tutorial > Session study notes in php_PHP tutorial

Session study notes in php_PHP tutorial

WBOY
Release: 2016-07-13 10:50:16
Original
982 people have browsed it

Session refers to the time that elapses from entering the website to closing the browser when the user is browsing a website, that is, the time the user spends browsing the website. From the above definition, we can see that Session is actually a specific time concept.

When using PHP to apply a session, the data in the session is stored on the server, and then the client's information is identified through the sessionID passed by the client, and the information is extracted.

An instance of myself

The code is as follows Copy code
 代码如下 复制代码

session_start();//启动会话session,在session必须启动
$_SESSION['name']=”fangshan”;//设置session,name的值为fangshan
?>
next

(以上另存为session.php)

session_start();//启动会话
if(isset($_SESSION['name']))//判断是否存在
{
echo $_SESSION['name'];
echo “del session”;
}
else
{
 echo “Session has not been setted”;
}
?>

(以上另存为session2.php)

session_start();//启动session
unset($_SESSION['name']);//注销session变量
session_destroy();//清除session ID
header(“Location:session2.php”);//跳转回前面页面
?>

(以上另存为deletesession.php)

session_start();//Start the session, the session must be started
$_SESSION['name']=”fangshan”;//Set session, the value of name is fangshan ?> next

(Save the above as session.php)

session_start();//Start session

if(isset($_SESSION['name']))//Judge whether it exists

{

echo $_SESSION['name'];

echo “del session”;

}

else

{

echo “Session has not been setted”;

}

?>

(Save the above as session2.php)

代码如下 复制代码
session_start();
$_SESSION['keyword']= "php";
?>
session_start();//Start session

unset($_SESSION['name']);//Unregister session variable

session_destroy();//Clear session ID
 代码如下 复制代码
session_start();
echo $_SESSION['keyword'];
?>
header(“Location:session2.php”);//Jump back to the previous page

?>

(Save the above as deletesession.php)

Common operations of session:

Session writing, reading, registration and deletion.

Start of session

The function that marks the start of session use is session_start, and the session_start function is used to initialize session variables. The syntax is as follows:
 代码如下 复制代码

session_start();
if(isset($_SESSION['keyword']))
echo $_SESSION['keyword'];
else
echo "www.bKjia.c0m";
?>

session_start(); The return value is TRUE. Writing and reading of session In PHP, the use of session is completed by calling and reading the predefined array $_SESSION. In the website page, assign the $_SESSION array on the registration page, and read the $_SESSION array on other pages. session in the registration page, for example:
The code is as follows Copy code
session_start();<🎜> $_SESSION['keyword']= "php";<🎜> ?>
session in other pages, for example:
The code is as follows Copy code
session_start();<🎜> echo $_SESSION['keyword'];<🎜> ?>
Run in sequence, the result is: php Session detection and logout Remember the isset and unset functions? These two functions implement session detection and logout respectively. The isst function is used to detect whether the session already exists. The syntax is as follows: bool isset($_SESSION['session_name']) For example:
The code is as follows Copy code
session_start();<🎜> if(isset($_SESSION['keyword']))<🎜> echo $_SESSION['keyword'];<🎜> else<🎜> echo "www.bKjia.c0m";<🎜> ?>

Result:

php

The usset function is used to cancel the established session variable. The syntax is as follows:

unset($_SESSION['session_name']))

For example:

The code is as follows
 代码如下 复制代码

session_start();
unset($_SESSION['keyword']);
if(isset($_SESSION['keyword']))
echo $_SESSION['keyword'];
else
echo "www.bKjia.c0m";
?>

Copy code

session_start();

unset($_SESSION['keyword']);
if(isset($_SESSION['keyword'])) echo $_SESSION['keyword']; else

echo "www.bKjia.c0m";

?>




Result:

www.bKjia.c0m




Php has a total of 10 functions for processing sessions. Let’s introduce in detail a few functions that will be used.

1. session_start

Function: Start a session or return an existing session.
Function prototype: boolean session_start(void);
Return value: Boolean value
Function description: This function has no parameters and the return value is true. It is best to put this function first, and there must be no output before it, otherwise an alarm will be issued, such as: Warning: Cannot send session cache limiter - headers already sent (output started at /usr/local/apache/htdocs/cga /member/1.php:2) in /usr/local/apache/htdocs/cga/member/1.php on line 3

2. session_register


Function: Register a new variable as a session variable
Function prototype: boolean session_register(string name);
Return value: Boolean value.
Function description: This function adds a variable to the current SESSION in the global variable. The parameter name is the name of the variable you want to add. If successful, it returns the logical value true. You can use the form $_SESSION[name] or $HTTP_SESSION_VARS[name] to get or assign a value.
3. session_is_registered


Function: Check whether the variable is registered as a session variable.
Function prototype: boobean session_is_registered(string name);
Return value: Boolean value
Function description: This function can check whether the specified variable has been registered in the current session. The parameter name is the name of the variable to be checked. If successful, the logical value true is returned.
4. session_unregister

Function: Delete registered variables.
Function prototype: boolean session_session_unregister(string name);
Return value: Boolean value
Function description: This function deletes variables in global variables in the current session. The parameter name is the name of the variable to be deleted, and returns true if successful.
5. Session_destroy

Function: End the current session and clear all resources in the session.
Function prototype: boolean session destroy(void);
Return value: Boolean value.
Function description: This function ends the current session. This function has no parameters and the return value is true
The functions introduced above will be used below, but there are also some session-related functions:
6. session_encode


Function: session information encoding
Function prototype: string session_encode(void);
Return value: string
Function description: The returned string contains the name and value of each variable in the global variable, in the form: a|s:12:"it is a test";c|s:4:"lala"; a is the variable name s :12 represents the value of variable a. "The length of it is a test is 12. The variables are separated by semicolon ";".
7. session_decode


Function: decoding session information
Function prototype: boolean session_decode (string data)
Return value: Boolean value
Function description: This function can decode session information and return the logical value true
if successful. 8. session_name


Function: access current session name
Function prototype: boolean session_name(string [name]);
Return value: string
Function description: This function can obtain or reset the name of the current session. If there is no parameter name, it means to get the current session name. Adding the parameter means setting the session name to the parameter name

9. session_id
Function: Access the current session identification number Function prototype: boolean session_id(string [id]); Return value: string Function description: This function can obtain or reset the identification number of the currently stored session. If there is no parameter id, it means only getting the identification number of the current session. Adding the parameter means setting the session identification number to the newly specified id 10. session_unset Function: Delete all registered variables. Function prototype: void session_unset (void) Return value: Boolean value Function description: This function is different from Session_destroy in that it does not end the session. Just like using the function session_unregister to log out all session variables one by one.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632662.htmlTechArticleSession refers to the period of time when a user browses a website, from entering the website to closing the browser. Time, that is, the time spent by users browsing this website. From the above definition...
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