On several complementary functions of session

Source: Internet
Author: User
Tags array functions garbage collection ini return string domain client
session| function in PHP, there are a lot of discussions about the session, in fact, there are several functions in the PHP4 we do not usually notice.

Let me introduce them to you.

The Session_set_save_handler () is really a good thing.

//********************
Session_unset (PHP4 >= 4.0b4)

void Session_unset (void);

This function can place all of the session variables that are of course registered as NULL. Note that it is not unregister, but also different from destroy.

The following example shows a good description of this function.

<?php

Session_register (' A ', ' ' B ', ' ' C '); Auto-session-start
$a = 1;
$b = 2;
$c = 3;
Session_unregister (' a '); Unregistrered $a
echo "A: $a-reg:". session_is_registered (' a '). "
"; But the global $a remains
Session_unset (); Unsets $b und $c
echo "B: $b-reg:". session_is_registered (' B '). "
"; The registration remains!
echo "C: $c-reg:". session_is_registered (' C '). "
";
Echo Session_encode ();
?>

Output:
A:1-REG:
B:-Reg:1
C:-Reg:1
!b|! c|

//********************************
Session_get_cookie_params (PHP4 >= 4.0rc2)

Array session_get_cookie_params (void);

Returns an array that records some information about the cookie for the current session.
Yes:

"Lifetime"-the lifetime of the cookie.

"Path"-the Save path for the cookie.

Domain-The field of the cookie.

//*******************************
Session_set_cookie_params (PHP4 >= 4.0b4)

void session_set_cookie_params (int lifetime [, String path [, string domain]])

Set some parameter parameters for the session cookie, similar to the setting in PHP.ini, but the settings made by this function are valid only for the current script file.


//*******************************

The following function should be useful for everyone, are you interested in customizing a session that is not saved with cookies? This function will achieve your vision.

Let me think, what's the advantage of not using cookies? At least one point, you don't have to worry about whether the client's cookie function is turned on, right.


Session_set_save_handler (PHP4 >= 4.0b4)

void Session_set_save_handler (string open, string close, string read, string write, string destroy, String gc)

This function can define the Save function (open, close, write, and so on) of the user-level session.
This function is useful, for example, when we want to save a session in a local database.

!!! Note: Before using this function, you must first configure the php.ini file, Session.save_hadler=user, otherwise, Session_set_save_handler () will not take effect.

Also, according to my tests, if you want this session to be used across pages, add your own functions and Session_set_save_handler to every script file that you use in the session, so the best way is to make a separate file, Include in every script that will use session.

The following example provides a basic session save method, similar to the default files method.
If you want to use the database to achieve, this is also very easy to do.

Example 1. Session_set_save_handler () example

<?php

function open ($save _path, $session _name) {
Global $sess _save_path, $sess _session_name;

$sess _save_path = $save _path;
$sess _session_name = $session _name;
return (true);
}

function Close () {
return (true);
}

function Read ($id) {
Global $sess _save_path, $sess _session_name;

$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "R")) {
$sess _data = fread ($fp, FileSize ($sess _file));
Return ($sess _data);
} else {
Return ("");
}

}

function Write ($id, $sess _data) {
Global $sess _save_path, $sess _session_name;

$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "W")) {
Return (Fwrite ($fp, $sess _data));
} else {
return (false);
}

}

function Destroy ($id) {
Global $sess _save_path, $sess _session_name;

$sess _file = "$sess _save_path/sess_$id";
Return (@unlink ($sess _file));
}

/*********************************************
* Warning-you'll need to implement some *
* Sort of garbage collection routine here. *
*********************************************/
Function GC ($maxlifetime) {
return true;
}

Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");

Session_Start ();

Proceed to use sessions normally
Now you can use the session as usual.


?>


//***************************************

Session_cache_limiter (PHP4 CVS only)

String Session_cache_limiter ([string cache_limiter])

This function can set or get the value of Session.cache_limiter.
You can also make the same settings in php.ini. Its value is nocache,public,private.

This function is sent to the client via the header of HTTP. If NoCache, the cache for any client is prohibited. Public will allow cache, but private is a little more secure than public.

The default value is the setting in php.ini, and if you want to use it, you must call this function before each call to Session_Start ().

This function can now only run in CVS mode, but PHP4.0.3 will support it.


Example 1. Session_cache_limiter () examples

<?php

# set the cache limiter to ' private '

Session_cache_limiter (' private ');
$cache _limiter = Session_cache_limiter ();

echo "The cache limiter is now set to $cache _limiter<p>";
?>

//*****************************



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.