Php cookie application

Source: Internet
Author: User
In website construction, there are a lot of cookies, so how should we use cookies? How should we write cookies reasonably? below I will summarize the usage of cookies for your reference.


/**
@ Cookie learning
1. set cookie [setcookie (name, value, valid time, valid path, valid domain, and security)]
Setcookie () this function is part of the http header information, so there cannot be any output before use
Cookie: Array type
2. the cookie information can be the same as the normal array variable.
3. delete setcookie (name, value, Expiration Time ,'/')

Difference between session and cookie:
1. the former is stored on the server, and the latter is stored on the browser.
2. in terms of security, the former is safe, and the latter is not safe.
3. The latter has the path and domain name restrictions. the former does not
4. the use of Sessions depends on cookies. if cookies are disabled, a solution is required for the session.
5. the session is stored on the server. therefore, do not overuse the session to avoid heavy load on the server.
*/

Setcookie ('User _ name', 'hangsan ', time () + 3600 ,'/');
Setcookie ('User _ psd ', '000000', time () + 123456 ,'/');
Setcookie ('User _ addr ', 'Beijing', time () + 3600 ,'/');
// Setcookie ('User _ xueli ', 'daxue', time () + 3600, '/', '.phpone.com ');
// Setcookie ('User _ hobby', 'daxue ', time () + 3600,'/',' .phpone.com ', true );

// Use $ _ SERVER to view the system and Directory
// Print_r ($ _ SERVER );
Echo $ _ COOKIE ['User _ name'];
?>


 

 


/**
@ Delete cookie variable information
@ Give an expiration time
*/
Setcookie ('User _ name', 'abcd', time ()-1 ,'/');
Setcookie ('User _ addr ', 'abcd', time ()-1 ,'/');
Setcookie ('User _ psd ', 'abcd', time ()-1 ,'/');
?>


 

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.