search
HomeBackend DevelopmentPHP ProblemHow to set expiration time for php cookies?

How to set expiration time for php cookies?

Jul 21, 2020 am 11:38 AM
cookiephpExpiration

You can use the setcookie() function in PHP to set the expiration time of cookies. The syntax is "setcookie(name,value,expire,path,domain,secure)"; the expire parameter is used to specify the validity period of the cookie, that is, the expiration timestamp.

How to set expiration time for php cookies?

# The setcookie() function sends an HTTP cookie to the client.

Cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the same computer requests a page through the browser.

[Related tutorial recommendation: "PHP Tutorial"]

The name of the cookie is automatically assigned to a variable with the same name. For example, if the cookie being sent is named "user", a variable named $user will automatically be created containing the cookie's value.

The cookie must be assigned before any other output is sent to the client.

If successful, this function returns TRUE. Returns FALSE on failure.

Syntax

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

Parameters:

Parameters Description
name Required. Specifies the name of the cookie.
value Required. Specifies the cookie value.
expire Optional. Specifies the cookie expiration time.

                time() 3600*24*30 will set the cookie expiration time to 30 days. If this parameter is not set, the cookie will automatically expire after the session ends (that is, when the browser is closed).

path Optional. Specifies the server path for cookies.

If the path is set to "/", then the cookie will be valid within the entire domain name. If the path is set to "/test/", then the cookie will be valid under the test directory and all its subdirectories. The default path value is the current directory where the cookie is located.

domain Optional. Specifies the domain name for the cookie.

In order for the cookie to be valid in all subdomains of example.com, you need to set the domain name of the cookie to ".example.com". When you set the domain name of the cookie to www.example.com, the cookie is only valid in the www subdomain.

secure Optional. Specifies whether cookies need to be transmitted over a secure HTTPS connection. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE.

Set cookie expiration time

Set cookie to expire after one day

setcookie ("mycookie", "123", time()+3600*24);
/* http://www.manongjc.com/article/1258.html */

time() represents the timestamp of the current time, time() 3600*24 represents the timestamp of the next 24 hours.

Similarly, set the cookie to expire after one year

setcookie ("mycookie", "123", time()+3600*24*365);

Delete the cookie

By setting the expiration date to a date/time in the past, Delete a cookie:

setcookie("username", "", time()-3600,"/amazon");
setcookie("lastlogin", "", time()-3600,"/amazon");
setcookie("userid", "", time()-3600,"/amazon");

Delete all cookies for the current session

foreach($_COOKIE as $key=>$val){
setcookie($key,"",time()-3600,"/amazon");

The above is the detailed content of How to set expiration time for php cookies?. For more information, please follow other related articles on the PHP Chinese website!

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

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.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor