Use of php-session (study notes, for reference only)

Source: Internet
Author: User

1,session is a server-side technology, using this technology, the server can be run for each user's

To create a shared session file, because the session is exclusive to the user 's browser, the user

to access other web shared resources on the server , the other Web resources are then removed from the user's respective session

Remove data for User Service

When a user opens a browser and accesses a Web site , the server will divide the browser

A file that holds session information, which is accessed by the browser alone

2.Details

2.1 By default the session file will be saved in c:/window/temp;

2.2 Session Data default 1440s( five minutes) failure, this expiration time can be through the php

Set the php.ini file settingsession.gc_maxlifetime= the time (default in seconds

for the unit); this time can also be a session of survival time, it means

The session is not used in a continuous 1440s will fail, but once used, this time

it will start counting again from 0.

2.3 Modify the storage path of the session file and modify it in the php.ini file:

; session.save_path= next line input for "/tmp"

Session.save_path= " Custom path "Note This code must not have a semicolon before it ; Appache

Restart only takes effect "

2.4 in Web Development, the server can create a hyper-global variable for each user browser

volume,$_session, Note: A browser exclusive of one $_session(by default

)。 Therefore, when you need to save the user data, the server program can write user data to the user browser alone

in a session file, when a user accesses another PHP page using a browser , other php pages can be

$_session the user data and serve the user

3 . Basic usage of Session:

3.1 Session must be initialized before it can be used

Session_Start ();

Session file can hold multiple key-value pairs key<=>value, but key values cannot be the same

If you want to get the session data is an object, you must first declare the object's class of the letter

Interest

A session corresponding to a sessions , the browser from open to close is a session

3.2 How to save session data

<?php

1. Initialize session:

Session_Start ();

Name is the key value key;shunping refers to value;

$_session[' name ']= "shunping"; the session file can be saved

Double,integer,

Boolarray, Data of type object

$_session[' age ']=100;// save integer bool type

$-session[' Isboy ']=true;

save N. An array

$arr =array (' Beijing ', ' tianjjing ', ' Nanjing ');

$_session[' ARR ']= $arr;

save an object

Class dog{

Private $name;

Private $age;

Private $intro;

function _construct ($name, $age, $intro) {

$this->name= $name;

$this->age= $age;

$this->intro= $intro;

}

}

$dog =new Dog (' Huanhuan ', ' 5 ', ' He's a cute puppy ');

$_session[' Mydog ']= $dog;

echo " saved successfully! ";

?>

3.3 How to get session data

<?php

Initialize

Session_Start ();

get all session data and output

Print_r ($_session);

get the corresponding session value by key value

echo $_session[' key value '];

It is important to declare the definition of a class when getting the data is the session data of the object.

declare the definition of the class first

Class dog{

Private $name;

Private $age;

Private $intro;

function _construct ($name, $age, $intro) {

$this->name= $name;

$this->age= $age;

$this->intro= $intro;

}

Public Function GetName () {

return $this->name;

}

}

$object =$_session[' key value '];

echo $object->getname ();

?>

3.4 Updating session data

Session_Start ();

$_session[' key value ']=value;// to update the corresponding value according to the key value

3.5 manually deleting session data

Session_Start ();

Delete a key-value pair

unset ($_session[' key value ']);

Delete all session files that correspond to this browser , one session

, the browser from open to close is a session

Session_destrory ();

3.6 System Delete session data

Session Data default 1440s( five minutes) failure, this expiration time can be through the php

Set the php.ini file settingtosession.gc_maxlifetime= the time (default

seconds); "This time can also be a session of survival time, it means

The session is not used in a continuous 1440s will fail, but once used, the

the time will start counting again from 0.

4. the difference between session and cookie :

4.1 Cookie is the user's data is written down to the user's browser.

4.2 Session is to write the user's data in the user exclusive $_session , there is a server

a path

4.3 $ before using $_session to ensure that the SESSION is initialized, the specific method is:

4.3.1 First Call session_start ();

4.3.2 Configuring The session.auto_start=1 in the php.ini file ( not recommended, will affect the effective

rate );

4.4 Server implementation, The principle of a session File sessions service:

5, each session file has its own SessionID;

5.1 Method of obtaining sessionid :$sessionid =session_id ();

5.2 To set the SessionID methodfor the current session file :session_id ("id value ”);

5.3 to create a session file with its own definition ID , the code is as follows

SESSION_ID ("id value ");

Session_Start ();

Use of php-session (study notes, for reference only)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.