Table of Contents
Introduction
What are cookies?
setcookie
Syntax
Parameters
Cookie Example
Example
Output
Home Backend Development PHP Tutorial PHP $_COOKIE Convert to PHP $_COOKIE

PHP $_COOKIE Convert to PHP $_COOKIE

Aug 27, 2023 pm 02:01 PM

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.
&lt;?php
if (isset($_COOKIE[&#39;username&#39;]))
echo &quot;&lt;h2&gt;Cookie name is already set with value: &quot; . $_COOKIE[&#39;username&#39;] . &quot;&lt;/h2&gt;&quot;;
else{
   setcookie(&quot;username&quot;, &quot;Anil&quot;);
   echo &quot;&lt;h2&gt;Cookie is now set &lt;/h2&gt;&quot;;
?&gt;
Copy after login

Retrieve cookies on subsequent visits from the client

Example

&lt;?php
$arr=$_COOKIE;
foreach ($arr as $key=&gt;$val);
echo &quot;&lt;h2&gt;$key=&gt;$val &lt;/h2&gt;&quot;;
?&gt;
Copy after login

Output

The browser will Display results similar to the following

username=&gt;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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles