How to Create a Cookie in PHP
To create a cookie using PHP, you have to use the setcookie() function.
The setcookie() function is used to set a cookie using certain parameters like the name, the value and the expiration time.
The exact syntax of the setcookie() function is
setcookie(cookie_name, cookie_value, expiration_time);
The following is an explanation of the variables used in this function.
cookie_name: The name of your cookie
cookie_value: The value stored in your cookie
expiration_time: The time in which your cookie will expire
For example, to create a cookie which stores the name of the visitor, you can use this code
<?php// getting the name from the form variable
$username = $_POST['username'];
// creating the expiration time for the cookie to 10 days from now
$exptime = 60 * 60 * 24 * 10 + time();
//creating a cookie with the name 'username'
This creates a cookie username which stores the user's name from the form and sets the expiration date to 10 days from now. The time() function is used to return the current time.