Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Creating and Reading Cookies | Working with Cookies in PHP
PHP Sessions and Cookies

bookCreating and Reading Cookies

Cookies in PHP allow you to store small pieces of data on the client's browser, making it possible to remember information between requests. You might use cookies for things like remembering a user's language preference, tracking visits, or keeping a user logged in. Unlike session data, which is stored on the server, cookies are stored on the client and sent with every HTTP request to your PHP scripts.

index.php

index.php

copy
1234567891011
<?php // Set a cookie named "username" with the value "Alice" setcookie("username", "Alice"); // Read the cookie value if it exists if (isset($_COOKIE["username"])) { echo "Hello, " . $_COOKIE["username"] . "!"; } else { echo "Hello, guest!"; } ?>

The setcookie() function is used to send a cookie from your PHP script to the browser. Its basic usage, as shown above, is setcookie(name, value), where name is the cookie's key and value is the data you want to store. In the example, the cookie is named "username" and its value is "Alice". After the cookie is set and the page is reloaded, you can read its value using the $_COOKIE superglobal array. Remember, cookies are not available in $_COOKIE until the next request, so you typically see the new value after a page reload.

index.php

index.php

copy
123456789101112
<?php // Set a cookie named "theme" with the value "dark" that expires in 1 hour $expiry = time() + 3600; // 3600 seconds = 1 hour setcookie("theme", "dark", $expiry); // Read the cookie value if it exists if (isset($_COOKIE["theme"])) { echo "Theme: " . $_COOKIE["theme"]; } else { echo "No theme set."; } ?>

Setting a cookie with an expiration time means the browser will keep it until that time is reached, unless the user clears their cookies. In the second example, the setcookie() function includes a third parameter: the expiration timestamp, calculated with time() + 3600 to set the cookie to expire in one hour. If you omit the expiration time, the cookie becomes a session cookie and is deleted when the browser closes. Persistent cookies, like the "theme" cookie above, remain available to your PHP scripts until their expiration, making them useful for remembering user preferences or login states across visits. Always remember that cookies are sent with every request to your domain until they expire or are deleted, so use them thoughtfully.

question mark

Which statement about setting cookies in PHP is correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookCreating and Reading Cookies

Pyyhkäise näyttääksesi valikon

Cookies in PHP allow you to store small pieces of data on the client's browser, making it possible to remember information between requests. You might use cookies for things like remembering a user's language preference, tracking visits, or keeping a user logged in. Unlike session data, which is stored on the server, cookies are stored on the client and sent with every HTTP request to your PHP scripts.

index.php

index.php

copy
1234567891011
<?php // Set a cookie named "username" with the value "Alice" setcookie("username", "Alice"); // Read the cookie value if it exists if (isset($_COOKIE["username"])) { echo "Hello, " . $_COOKIE["username"] . "!"; } else { echo "Hello, guest!"; } ?>

The setcookie() function is used to send a cookie from your PHP script to the browser. Its basic usage, as shown above, is setcookie(name, value), where name is the cookie's key and value is the data you want to store. In the example, the cookie is named "username" and its value is "Alice". After the cookie is set and the page is reloaded, you can read its value using the $_COOKIE superglobal array. Remember, cookies are not available in $_COOKIE until the next request, so you typically see the new value after a page reload.

index.php

index.php

copy
123456789101112
<?php // Set a cookie named "theme" with the value "dark" that expires in 1 hour $expiry = time() + 3600; // 3600 seconds = 1 hour setcookie("theme", "dark", $expiry); // Read the cookie value if it exists if (isset($_COOKIE["theme"])) { echo "Theme: " . $_COOKIE["theme"]; } else { echo "No theme set."; } ?>

Setting a cookie with an expiration time means the browser will keep it until that time is reached, unless the user clears their cookies. In the second example, the setcookie() function includes a third parameter: the expiration timestamp, calculated with time() + 3600 to set the cookie to expire in one hour. If you omit the expiration time, the cookie becomes a session cookie and is deleted when the browser closes. Persistent cookies, like the "theme" cookie above, remain available to your PHP scripts until their expiration, making them useful for remembering user preferences or login states across visits. Always remember that cookies are sent with every request to your domain until they expire or are deleted, so use them thoughtfully.

question mark

Which statement about setting cookies in PHP is correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1
some-alt