General discussion
cookies in PHP?

To be honest, I do not know, watch some videos about it or read about it


To be honest, I do not know, watch some videos about it or read about it

Here are some infos about it:


To set a cookie in PHP, the setcookie() function is used.The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set.


Syntax :


setcookie(name, value, expire, path, domain, security);
Parameters: The setcookie() function requires six arguments in general which are:


  • Name: It is used to set the name of the cookie.
  • Value: It is used to set the value of the cookie.
  • Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
  • Path: It is used to specify the path on the server for which the cookie will be available.
  • Domain: It is used to specify the domain for which the cookie is available.
  • Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.

Here are some operations how Cookies can be performed in PHP:


Creating Cookies: Creating a cookie named Auction_Item and assigning the value Luxury Car to it.The cookie will expire after 2 days(2 days 24 hours 60 mins * 60 seconds).


<?php

setcookie("Auction_Item", "Luxury Car", time()+2*24*60*60)
;

?>

Note: Only the name argument in the setcookie() function is mandatory.To skip an argument,the argument can be replaced by an empty string(“”).


Checking Whether a Cookie Is Set Or Not: It is always advisable to check whether a cookie is set or not before accessing its value.Therefore to check whether a cookie is set or not, the PHP isset() function is used.
To check whether a cookie “Auction_Item” is set or not,the isset() function is executed as follows:


<?php

if(isset($_COOKIE["Auction_Item"])){
echo "Auction Item is a " . $_COOKIE["Auction_Item"];
} else{
echo "No items for auction.";
}

?>

Output:


Auction Item is a Luxury Car.
Accessing Cookie Values: For accessing a cookie value, the PHP $_COOKIE superglobal variable is used.It is an associative array that contains a record of all the cookies values sent by the browser in the current request.The records are stored as a list where cookie name is used as the key.
To access a cookie named “Auction_Item”,the following code can be executed:


<?php

echo "Auction Item is a " . $_COOKIE["Auction_Item"];

?>

Output:


Auction Item is a Luxury Car.
Deleting Cookies: The setcookie() function can be used to delete a cookie.For deleting a cookie, the setcookie() function is called by passing the cookie name and other arguments or empty strings but however this time, the expiration date is required to be set in the past.
To delete a cookie named “Auction_Item”,the following code can be executed:


<?php

setcookie("Auction_Item", "", time()-60)
;

?>

Best is to read some tutorials about it - there are a lot to find at: https://cutt.ly/cbQTSIM


Kind regards,


Andy smile


Here are some infos about it: ----------------------------- To set a cookie in PHP, the **setcookie()** function is used.The **setcookie()** function needs to be called prior to any output generated by the script otherwise the cookie **will not be set**. ### Syntax : setcookie(name, value, expire, path, domain, security); **Parameters:** The setcookie() function requires six arguments in general which are: - Name: It is used to set the name of the cookie. - Value: It is used to set the value of the cookie. - Expire: It is used to set the expiry timestamp of the cookie after which the cookie can&rsquo;t be accessed. - Path: It is used to specify the path on the server for which the cookie will be available. - Domain: It is used to specify the domain for which the cookie is available. - Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists. ### Here are some operations how Cookies can be performed in PHP: **Creating Cookies:** Creating a cookie named Auction_Item and assigning the value Luxury Car to it.The cookie will expire after 2 days(2 days * 24 hours * 60 mins * 60 seconds). ```` &lt;?php setcookie(&quot;Auction_Item&quot;, &quot;Luxury Car&quot;, time()+2*24*60*60); ?&gt; ```` **Note:** Only the name argument in the setcookie() function is mandatory.To skip an argument,the argument can be replaced by an empty string(&ldquo;&rdquo;). **Checking Whether a Cookie Is Set Or Not:** It is always advisable to check whether a cookie is set or not before accessing its value.Therefore to check whether a cookie is set or not, the PHP isset() function is used. To check whether a cookie &ldquo;Auction_Item&rdquo; is set or not,the isset() function is executed as follows: ```` &lt;?php if(isset($_COOKIE[&quot;Auction_Item&quot;])){ echo &quot;Auction Item is a &quot; . $_COOKIE[&quot;Auction_Item&quot;]; } else{ echo &quot;No items for auction.&quot;; } ?&gt; ```` **Output:** Auction Item is a Luxury Car. **Accessing Cookie Values:** For accessing a cookie value, the PHP $_COOKIE superglobal variable is used.It is an associative array that contains a record of all the cookies values sent by the browser in the current request.The records are stored as a list where cookie name is used as the key. To access a cookie named &ldquo;Auction_Item&rdquo;,the following code can be executed: ```` &lt;?php echo &quot;Auction Item is a &quot; . $_COOKIE[&quot;Auction_Item&quot;]; ?&gt; ```` **Output:** Auction Item is a Luxury Car. **Deleting Cookies:** The setcookie() function can be used to delete a cookie.For deleting a cookie, the setcookie() function is called by passing the cookie name and other arguments or empty strings but however this time, the expiration date is required to be set in the past. To delete a cookie named &ldquo;Auction_Item&rdquo;,the following code can be executed: ```` &lt;?php setcookie(&quot;Auction_Item&quot;, &quot;&quot;, time()-60); ?&gt; ```` Best is to read some tutorials about it - there are a lot to find at: https://cutt.ly/cbQTSIM Kind regards, Andy :)

Please read the message with the info about how to setup cookies right above your message. There is already every info included and also some PHP code snippets and explanations.


The topic of cookies is very broad, there are many possibilities and several approaches to achieve this.


What exactly do you want to achieve or to set in the cookies?


Maybe I can tell You a little bit more about it if You tell me what You like to achieve. smile


Kind regards,


Andy smile


**Please** read the message with the info about how to setup cookies **right above your message**. There is already every info included and also some PHP code snippets and explanations. The topic of cookies is very broad, there are many possibilities and several approaches to achieve this. **What exactly do you want to achieve** or to set in the cookies? Maybe I can tell You a little bit more about it if You tell me what You like to achieve. :) Kind regards, Andy ;)
edited Jun 2 at 6:33 pm
42
4
4
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft