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

PHP $_COOKIE Convert to PHP $_COOKIE

WBOY
Release: 2023-08-27 14:02:02
forward
757 people have browsed it

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);
Copy after login

Parameters

  • Name - The name of the cookie being stored.
  • Value - Sets the value of the specified variable.
  • < li>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>";
?>
Copy after login

Retrieve cookies on subsequent visits from the client

Example

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

Output

The browser will Display results similar to the following

username=>Anil
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template