What is a Cookie?A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. Show
Create Cookies With PHPA cookie is created with the Syntaxsetcookie(name, value, expire, path, domain, secure, httponly); Only the name parameter is required. All other parameters are optional. PHP Create/Retrieve a CookieThe following example creates a cookie named "user" with the value "John Doe". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer). We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the
Example<?php <?php </body> Run example » Note: The Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use Modify a Cookie ValueTo modify a cookie, just set (again) the cookie using the Example<?php <?php </body> Run example » Delete a CookieTo delete a cookie, use the Example<?php <?php </body> Run example » Check if Cookies are EnabledThe following example creates a small script that checks whether cookies are enabled. First, try to create a test cookie with the Example<?php <?php </body> Run example » Complete PHP Network ReferenceFor a complete reference of Network functions, go to our complete PHP Network Reference. PHP Exercises❮ PHP Network Reference ExampleThe following example creates a cookie named "user" with the value "John Doe". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer). We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set: <!DOCTYPE html> <?php </body> Try it Yourself » Definition and UsageThe setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. The name of the cookie is automatically assigned to a variable of the same name. For example, if a cookie was sent with the name "user", a variable is automatically created called $user, containing the cookie value. Note: The setcookie() function must appear BEFORE the <html> tag. Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead). Syntaxsetcookie(name, value, expire, path, domain, secure, httponly); Parameter Values
Technical Details
More ExamplesExampleSeveral expire dates for cookies: <?php // cookie will expire when the browser close // cookie will expire in 1 hour // cookie will expire in 1 hour, and will only be available ...some code... </body> Try it Yourself » ExampleTo modify a cookie, just set (again) the cookie using the setcookie() function: <?php <?php </body> Try it Yourself » ExampleTo delete a cookie, use the setcookie() function with an expiration date in the past: <?php <?php </body> Try it Yourself » ExampleCreate a small script that checks whether cookies are enabled. First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable: <?php <?php </body> Try it Yourself » ❮ PHP Network Reference How we can set cookie in PHP?The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.
How do you set cookies?Cookies are usually set by a web-server using the response Set-Cookie HTTP-header. Then, the browser automatically adds them to (almost) every request to the same domain using the Cookie HTTP-header.
What is cookie in PHP with example?PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the user. Cookie is created at server side and saved to client browser. Each time when client sends request to the server, cookie is embedded with request. Such way, cookie can be received at the server side.
What function makes cookies in PHP?Setting Cookie In PHP: To set a cookie in PHP, the setcookie() function is used.
|