關(guān)于session的幾個補充函數(shù)
Jun 21, 2016 am 09:14 AMsession|函數(shù)
在PHP下,關(guān)于session的討論很多,其實在PHP4中還有幾個函數(shù)是我們平時沒有注意到的。
下面我把它們介紹給大家吧。
其中的session_set_save_handler()可真是個好東西。
//********************
session_unset (PHP4 >= 4.0b4)
void session_unset(void);
這個函數(shù)可以把當(dāng)然注冊的所有的session變量置為空。注意它不是unregister,也不同于destroy。
下面這個例子,對此函數(shù)做了很好的說明。
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();
?>
輸出:
A: 1 - reg:
B: - reg:1
C: - reg:1
!b|!c|
//********************************
session_get_cookie_params (PHP4 >= 4.0RC2)
array session_get_cookie_params (void);
返回一個數(shù)組,記錄了當(dāng)前session的cookie的一些信息。
有:
"lifetime" - cookie的生存期。
"path" - cookie的保存路徑。
"domain" - cookie的域。
//*******************************
session_set_cookie_params (PHP4 >= 4.0b4)
void session_set_cookie_params (int lifetime [, string path [, string domain]])
設(shè)置session的cookie的一些參參數(shù),類似于php.ini中的設(shè)置,但本函數(shù)所作的設(shè)置,只對當(dāng)前腳本文件有效。
//*******************************
下面要介紹的這個函數(shù)應(yīng)是對大家都很有用的,你是不是對自定義一個不用cookie來保存的session有興趣呢?這個函數(shù)就可以實現(xiàn)你這樣的設(shè)想。
讓我想想,如果不用cookie的好處是什么?至少一點,你不用擔(dān)心客戶端的的cookie的功能是否打開了,對吧。
session_set_save_handler (PHP4 >= 4.0b4)
void session_set_save_handler (string open, string close, string read, string write, string destroy, string gc)
這個函數(shù)可以定義用戶級的session的保存函數(shù)(打開、關(guān)閉、寫入等)。
比如,我們想把session保存在本地的一個數(shù)據(jù)庫中時,本函數(shù)就很有用了。
?。。∽⒁猓菏褂帽竞瘮?shù)前,先要配置php.ini文件,session.save_hadler=user ,否則,session_set_save_handler()不會生效。
此外,根據(jù)我的測試,你如果想讓這樣的session跨頁面使用,還要在每一個用到session的腳本文件中加入你自定的函數(shù)及session_set_save_handler,所以,最好的方法是做成一個單獨的文件,在每一個要用到session的腳本中用include來包含進(jìn)來。
下面這個例子提供了一個最基本的session保存法,類似于默認(rèn)的files方法。
如果你想用數(shù)據(jù)庫來實現(xiàn),這也是很容易做到的。
Example 1. session_set_save_handler() example
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 will 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
// 現(xiàn)在你就可以象往常一樣地使用session了。
?>
//***************************************
session_cache_limiter (PHP4 CVS only)
string session_cache_limiter ([string cache_limiter])
本函數(shù)可以設(shè)置或取得session.cache_limiter的值。
在php.ini中也可以做同樣的設(shè)置。其值有 nocache,public,private。
本函數(shù)是通過HTTP的header發(fā)送到客戶端的。如果為nocache,將禁止任何客戶端的cache。而public將允許cache,但是private相對public而言,更為安全一點。
缺省值是在php.ini中的設(shè)置,如果你要使用它,必須在每次調(diào)用session_start()之前調(diào)用本函數(shù)。
此函數(shù)現(xiàn)在只能在CVS模式下運行,但是PHP4.0.3將支持它。
Example 1. session_cache_limiter() examples
# 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
";
?>
//*****************************

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

Solutions for document.cookie not being obtained: 1. Browser privacy settings; 2. Same-origin policy; 3. HTTPOnly Cookie; 4. JavaScript code error; 5. Cookie does not exist or expires; 6. Cross-domain issues; 7. Viewer mode; 8. Server problems; 9. JavaScript execution timing; 10. Check console log, etc.

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.
