Home  >  Article  >  Backend Development  >  PHP $_COOKIE Convert to PHP $_COOKIE

PHP $_COOKIE Convert to PHP $_COOKIE

WBOY
WBOYforward
2023-08-27 14:01:11737browse

PHP $_COOKIE 转换为 PHP $_COOKIE

Introduction

Super global $_COOKIEStores variables passed to the current script along with the HTTP request in the form of cookies. $HTTP_COOKIE_VARS also contains the same information, but is not super-global and is now deprecated.

What are cookies?

Cookies are text files stored on client computers by the server for usage tracking purposes. PHP transparently supports HTTP cookies. Cookies are usually set in HTTP headers. JavaScript can also set cookies directly on the browser.

The server script sends a set of cookies to the browser. It stores this information locally on your computer for future use. The next time the browser makes any request to the web server, it will send this cookie information to the server, which uses the information to identify the user.

PHP contains the setcookie function to create a cookie object and send it to the client along with the HTTP response.

setcookie

Syntax

setcookie(name, value, expire, path, domain, security);

Parameters

  • Name - The name of the cookie being stored.
  • Value - Sets the value of the specified variable.
  • Expiration - Specifies a time in the future (in seconds) since January 1, 1970 00:00:00 GMT.
  • Path - Cookie valid directory.
  • Domain - Specifies a domain name within a very large domain.
  • Security − 1 means HTTPS. Regular HTTP defaults to 0.

Cookie Example

<?php
if (isset($_COOKIE[&#39;username&#39;]))
echo "<h2>Cookie name is already set with value: " . $_COOKIE[&#39;username&#39;] . "</h2>";
else{
   setcookie("username", "Anil");
   echo "<h2>Cookie is now set </h2>";
?>

Retrieve cookies on subsequent visits from the client

Example

<?php
$arr=$_COOKIE;
foreach ($arr as $key=>$val);
echo "<h2>$key=>$val </h2>";
?>

Output

The browser will Display results similar to the following

username=>Anil

To delete a cookie, set the cookie to an expired date

The above is the detailed content of PHP $_COOKIE Convert to PHP $_COOKIE. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete