Home  >  Article  >  Backend Development  >  Example code of how to modify the survival storage time of SESSION in php_php example

Example code of how to modify the survival storage time of SESSION in php_php example

怪我咯
怪我咯Original
2017-07-13 10:53:121609browse

PHP session Variables are used to store information about the user session, or to change the settings of the user session. The information held by the Session variable is single-user and available to all pages in the application.

PHP Session Variables

When 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 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.

The working mechanism of Session is to create a unique id (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or transmitted through the URL.

This article mainly introduces the example code of php how to modify the survival time of SESSION

How to modify the survival time of SESSION

Manually set the lifetime of the Session:

<?php
session_start(); 
// 保存一天 
$lifeTime = 24 * 3600; 
setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
?>

Session also provides a function session_set_cookie_params(); to set the lifetime of the Session. This function must be called before the session_start() function Call:

<?php 
// 保存一天 
$lifeTime = 24 * 3600; 
session_set_cookie_params($lifeTime); 
session_start();
$_SESSION["admin"] = true; 
?>

Session expiration time setting in php

Modify session.gc_maxlifetime in phpconfig file.

The above is the detailed content of Example code of how to modify the survival storage time of SESSION in php_php example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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