The better and simple solution for sending the user input values from one page to another page is using COOKIES. We can send data easily from page to page and can be easily retrieved and used.
Setting Cookies
setcookie():This method is used for creating the Cookies. Important note is that Cookies are sent with the HTTP headers,so setcookie() must be called before any output is generated.
Example:
setcookie('Variable','Value');
This is the default syntax declaration, We have another parameters which has its own importance but they are not mandatory.
The third argument to setcookie() is an expiration time,expressed as an epoch timestamp.For example, this cookie expires at noon GMT on December 3, 2004:
setcookie('flavor','chocolate chip',1102075200);
If the third argument to setcookie() is missing (or empty),the cookie expires when
the browser is closed. Also,many systems can’t handle a cookie expiration time
greater than 2147483647,because that’s the largest epoch timestamp that fits in a
32-bit integer
The fourth argument to setcookie() is a path. The cookie is sent back to the server
only when pages whose path begin with the specified string are requested. For example, the following cookie is sent back only to pages whose path begins with /products/:
setcookie('flavor','chocolate chip','','/products/');
The page that’s setting this cookie doesn’t have to have a URL that begins with /products/, but the following cookie is sent back only to pages that do.
The fifth argument to setcookie() is a domain. The cookie is sent back to the server
only when pages whose hostname ends with the specified domain are requested. For
example,the first cookie in the following code is sent back to all hosts in the example.com domain,but the second cookie is sent only with requests to the host test.example.com:
setcookie('variable','value','','','.example.com');
setcookie('variable','value','','','test.example.com');
If the first cookie’s domain was just example.com instead of .example.com,it would
be sent only to the single host example.com (and not www.example.com or test.
example.com).
The last optional argument to setcookie() is a flag that if set to 1,instructs the
browser only to send the cookie over an SSL connection. This can be useful if the
cookie contains sensitive information,but remember that the data in the cookie is
stored in the clear on the user’s computer.
Reading Cookie Values
A cookie’s value isn’t available in $_COOKIE during the request in which the cookie isset. In other words,the setcookie() function doesn’t alter the value of $_COOKIE. On subsequent requests,however,each cookie is stored in $_COOKIE. If register_globals
is on, cookie values are also assigned to global variables.
When a browser sends a cookie back to the server,it sends only the value. You can’t
access the cookie’s domain,path,expiration time,or secure status through $_COOKIE
because the browser doesn’t send that to the server.
To print the names and values of all cookies sent in a particular request,loop
through the $_COOKIE array:
foreach ($_COOKIE as $cookie_name => $cookie_value) {
print "$cookie_name = $cookie_value
";
}
Deleting Cookies
Call setcookie() with no value for the cookie and an expiration time in the past:
setcookie('flavor','',time()-86400);
It’s a good idea to make the expiration time a few hours or an entire day in the past,in case your server and the user’s computer have unsynchronized clocks. For example,if your server thinks it’s 3:06 P.M. and a user’s computer thinks it’s 3:02 P.M.,a cookie with an expiration time of 3:05 P.M. isn’t deleted by that user’s computer even though the time is in the past for the server.
The call to setcookie() that deletes a cookie has to have the same arguments (except
for value and time) that the call to setcookie() that set the cookie did,so include the path, domain, and secure flag if necessary.
Different browsers handle cookies in slightly different ways,especially with regard to how strictly they match path and domain strings and how they determine priority
between different cookies of the same name.
Tuesday, June 29, 2010
Cookies in PHP
Labels:
cookies in PHP,
php cookies
Subscribe to:
Post Comments (Atom)


23 comments:
Quality is better than quantity.................................................................
一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼............................................................
當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................
睇完之後覺得有d頓悟..感謝分享...................................................................
希望能有更多心得與我們分享~ ..................................................................
教育無他,愛與榜樣而已............................................................
死亡是悲哀的,但活得不快樂更悲哀。............................................................
如此活躍的文字, 妳好棒哦! ............................................................
很好很強大!祝你天天文思泉湧!..................................................
加油-不論如何都支持你..................................................................
不論做什麼事,相信自己,別讓別人的一句話,把你擊倒。..................................................
當我微笑時,世界和我一起微笑;當我快樂時,世界和我一起活躍。..................................................
一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼....................................................
所有的資產,在不被諒解時,都成了負債.................................................................
More haste, less speed..................................................................
不妄求,則心安;不妄作,則身安!......................................................
路過~很有趣吶...............................................................
來逛逛blog~~跟您打聲招呼............................................................
看看blog調整心情,又要來繼續工作,大家加油............................................................
人應該做自己認為對的事,而不是一味跟著群眾的建議走。..................................................
你的部落格不錯哦,支持!!!!@@ ..................................................................
肯定與支持你!!!加油囉~..................................................
要繼續發好文喔^^加油!...............................................
Post a Comment