search
Homephp教程PHP开发PHP Cookies are often used to identify users

Cookies are often used to identify users.

What are Cookies?

Cookies are often used to identify users. Cookies are small files that a server leaves on a user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.

How to create cookies?

setcookie() function is used to set cookies.

Note: The setcookie() function must be placed before the tag.

Syntax

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


Example

In the following example, we will create a cookie named "user" and assign it the value "Alex Porter" . We also specify that this cookie will expire after one hour:

setcookie("user", "Alex Porter", time()+3600); ?>



Note: When sending a cookie, the cookie value will be automatically URL-encoded and automatically decoded when retrieved (to prevent URL encoding, please use setrawcookie() instead) .


How to retrieve the value of Cookie?

PHP’s $_COOKIE variable is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

echo $_COOKIE["user "];
// A way to view all cookies
print_r($_COOKIE); ?>

In the following example, we use the isset() function to confirm whether a cookie has been set:


echo "Welcome " . $_COOKIE["user"] . "!
"; else
echo "Welcome guest!
"; ?>

How to delete cookies?

When deleting a cookie, you should change the expiration date to a point in time in the past.

Example of deletion:

setcookie("user", "", time()-3600); ?>


If the browser What should I do if cookies are not supported?

If your application involves browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One way is to pass data from a form (we've covered forms and user input earlier in this tutorial).

The form below submits user input to "welcome.php" when the user clicks the submit button:



Name:
Age:


Retrieve the value in "welcome.php", like this:



Welcome .

You are years old.
> ;

The above is what PHP Cookies are commonly used to identify users. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software