Current Location: Home> Function Categories> session_start

session_start

Start a new session or resume an existing session
Name:session_start
Category:Session Session
Programming Language:php
One-line Description:Start a new session or resume the current session

Function name: session_start()

Applicable version: PHP 4, PHP 5, PHP 7

Usage: The session_start() function is used to start a new session or restore the current session. This function must be called before using a session variable.

Example:

 // 开始一个新的会话session_start(); // 设置会话变量$_SESSION['username'] = 'John'; // 输出会话变量echo "用户名:" . $_SESSION['username'];

In the above example, we first call the session_start() function to start a new session. Then, we use the $_SESSION hyperglobal array to set the value of a session variable $_SESSION['username'] to 'John'. Finally, we output the value of the session variable through echo statement.

Notes:

  • There cannot be any output before calling the session_start() function, including spaces, line breaks, etc. Otherwise, it will cause a "Headers already sent" error.
  • Before using session variables, the session_start() function must be called.
  • The session_start() function should be called at the top of the script, even if the session variable is not used.
Similar Functions