How to save array to session in php

(*-*)浩
Release: 2023-02-25 19:44:02
Original
3977 people have browsed it

How to save array to session in php

Declaration and use of Session

Session settings are different from Cookie and must be started first. Session_start( must be called in PHP ).

session_start()The syntax format of the function is as follows: (Recommended learning: PHP video tutorial)

Bool session_start(void) //创建Session,开始一个会话,进行Session初始化
Copy after login

Note: session_start()There must be no output before the function

When you visit the website for the first time, the Seesion_start() function will create a unique Session ID and automatically pass the HTTP Response header, save this Session ID to the client cookie.

At the same time, a file named after the Session ID is also created on the server side to save the user's session information.

When the same user visits this website again, the Seesion ID saved in the cookie will be automatically brought over through the HTTP request header. At this time, the Session_start() function will not allocate a new one. Session ID, but to find the Session file with the same name as the Session ID in the server's hard disk, read out the session information previously saved for this user, and apply it in the current script to achieve the purpose of tracking this user.

Session is used in the form of an array, such as:

$_SESSION['session名']
Copy after login

Register a session variable and read Session

Use Session variables in PHP, except In addition to starting, you also need to go through the registration process. Registering and reading Session variables must be completed by accessing the $_SESSION array. The key names in the $_SESSION associative array have the same naming rules as ordinary variables in PHP.

After opening the session, then store data in it, which can be any type of data. For example:

<?php
session_start();
$a=array(1,2,3,4,5,6,"john","ok");
$_SESION["n"]=$a;
print_r($_SESION["n"]);
?>
Copy after login

Related topic recommendations: php session (including pictures, texts, videos, cases)

The above is the detailed content of How to save array to session in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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