How do you use php session

  • 2020-10-23 20:04:10
  • OfStack

PHP session is a very simple way to use PHP. It saves the data submitted by the user as a global variable in one session and generates one unique session_id. In order to avoid confusion, there can only be one session_id in session with one browser and one site.
How to use session, session related, you must call the function session_start();
Assigning values to session is as simple as:


<?php
Session_start();
$Name = " This is a 1 a Session example ";
Session_Register("Name");// Pay attention to , Don't write: Session_Register("$Name");
Echo $_SESSION["Name"];
// after $_SESSION["Name"] for " This is a 1 a Session example "
?>

After php4.2, session can be assigned directly:

<?PHP
Session_Start();
$_SESSION["name"]="value";
?>

session can be cancelled as follows:

<?php
session_start();
session_unset();
session_destroy();
?>

Read session

PHP's built-in $_SESSION variable provides easy access to the set session variable.


<?php
session_start();
echo " Registered user name: ".$_SESSION["username"];    // The output   Registered user name: nostop
?>

Check that the variable is registered as session variable session_is_registered
Grammar: boobean session_is_registered(string name);
This function checks to see if a specified variable is registered in the current session. The parameter name is the variable name to check. Success returns the logical value true.

<?php
    session_start();
    if(!session_is_registered("gender")){ // Determines whether the current session variable is registered 
        session_register("gender");    // Registry variable 
    }
    $gender=" female ";
    echo $_SESSION['gender'];  // female 
?>

Access the current session name session_name
Grammar: boolean session_name(string [name]);
This function gets or resets the name of the current session. Without the parameter name, you get the current session name, and with the parameter, you set the session name to the parameter name.


<?php
$sessionName = session_name();   // Get the current  Session  Name, default is  PHPSESSID
$sessionID = $_GET[$sessionName];   // achieve  Session ID
session_id($sessionID);      // use  session_id()  Set acquired  Session ID
?>

Access the current session ID session_id
boolean session_id(string [id]);
This function retrieves or resets the id number that currently holds session. Without the parameter id, you get the current session identifier only, and with the parameter, you set the session identifier to the newly specified id.
Set the lifetime of Session


<?php
    session_start
    //  save 1 day 
    $lifeTime = 24 * 3600;
    setcookie(session_name(), session_id(), time() + $lifeTime, "/");
?>

session_set_cookie_params: Sets the lifetime of Session, which must be called before the session_start() function call.
If the client USES IE 6.0, session_set_cookie_params(); Setting up Cookie is a bit of a problem, so we'll manually call the setcookie function to create cookie.

<?php
//  save 1 day 
  $lifeTime = 24 * 3600;
  session_set_cookie_params($lifeTime);
  session_start();
  $_session["admin"] = true;
?>

Set the save path for the Session file
session_save_path() : Must be called before the session_start() function call.


<?php
  //  Set up the 1 A directory of storage 
  $savePath = "./session_save_dir/";
  //  save 1 day 
  $lifeTime = 24 * 3600;
  session_save_path($savePath);
  session_set_cookie_params($lifeTime);
  session_start();
  $_session["admin"] = true;
?>
<?php
session_start();    // Start the Session
$username='nostop';
session_register('username');    // registered 1 called username variable 
echo ' Registered Users: '.$_SESSION['username'];    // Registered Users: nostop    read Session variable 
$_SESSION['age']=23;    // The statement 1 called age Variables and assign values to 
echo ' Age: '.$_SESSION['age']; // Age: 23
session_unregister('username'); // The cancellation Session variable 
echo $_SESSION['username'];  // empty 
echo $_SESSION['age'];//23
unset($_SESSION['age']); // The cancellation Session variable 
echo ' Registered Users: '.$_SESSION['username']; // empty 
echo ' Age: '.$_SESSION['age']; // empty 
?>

Note:

1: There cannot be any output until Session_Start() is called. For example, the following is wrong.


1 row
2 rows < ?PHP
3 rows Session_Start (); // Already has output in line 1
Line 4...
Line 5? >

Hint 1:

Whatever comes along "... headers already sent.........." Session_Start() outputs the information to the browser.
Remove the output and it will be normal. (This error also occurs with COOKIE for one reason.)

Tip 2:

If your Session_Start() is in a loop and it is difficult to determine where to output the information to the browser, use the following method:
1 row < ?PHP Ob_Start(); ? >
. Here's your program...


2: What was the mistake

Warning: session_start(): open(/tmpsess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed:....
Because you did not specify the session file location.

Solutions:

(1) Create folder tmp in c disk
(2) Open ES177en.ini, find ES179en.save_ES181en, modify to ES182en.save_ES184en = "c:/tmp"


Related articles: