Php setcookie usage

Source: Internet
Author: User
Tags md5 php tutorial setcookie strcmp

Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. Through the php Tutorial, you can create and retrieve the cookie value.
How to create a cookie?
The setcookie () function is used to set the cookie.

Note: The setcookie () function must be located before the

Syntax
Setcookie (name, value, expire, path, domain); example
In the following example, we will create a cookie named "user" and assign it "alex porter ". We also stipulate that the cookie will expire in an hour.


Simplest cookie creation method

<? Php

Echo ("cookie created? : ". Setcookie (" cookie_name "," cookie_data "));

?>

Instance II cookie storage array

<?
Setcookie ("php [uid]", "1111111111111111", time () + 3600 );
Setcookie ("php [color]", "black", time () + 3600 );
Setcookie ("php [preference]", "english", time () + 3600 );

If (isset ($ php )){
While (list ($ name, $ value) = each ($ php )){
Echo "$ name = $ value <br> n ";
   }
}
?>

Delete cookie

<? Php
Setcookie ("cookie_user", "test", time () + 60*60*24*30 );
Setcookie ("cookie_pass", md5 ("test"), time () + 60*60*24*30 );

Function logout (){
Setcookie ("cookie_user", "", time () + 60*60*24*30 );
Setcookie ("cookie_pass", "", time () + 60*60*24*30 );
}
Logout ();
Echo $ _ cookie ['cookie _ user']. "<br/> ";
Echo "you have successfully logged out .";
?>


 
The following example uses cookies to record user names to automatically log on to function instances

 

<? Php
$ Globals ['username'] = "test ";
$ Globals ['password'] = "test ";
 
Function validatelogin ($ username, $ password ){
If (strcmp ($ username, $ globals ['username']) = 0 & strcmp ($ password, $ globals ['password']) = 0 ){
Setcookie ("cookie_user", $ username, time () + 60*60*24*30 );
Setcookie ("cookie_pass", md5 ($ password), time () + 60*60*24*30 );
Return true;
} Else {
Return false;
    }
  }
If (validatelogin ("test", "test ")){
Echo "successfully logged in .";
} Else {
Echo "sorry, invalid login .";
  }
?>
 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.