Skip to content
PHP Logo

How can a cookie be set using $_POST['language'] in PHP?

To set a cookie using $_POST['language'] in PHP, you need to first check if the 'language' key is set in the $_POST superglobal array. If it is set, you can then set a cookie with the language value obtained from $_POST['language']. This can be done by using the setcookie() function in PHP.

php
if(isset($_POST['language'])){
    $language = $_POST['language'];
    setcookie('language', $language, time() + 3600); // Cookie expires in 1 hour
}