How to use setcookie in PHP

In this article I am going to explain about setcookie function in PHP.
  • 1315

PHP setcookie() Function

The HTTP setcookie function is used to sends http cookie to client. The function returns true on success and false on failure.

Syntax of setcookie  function

setcookie(name, value, expire, path, domain, secure)

Parameters in setcookie function

It have six parameter:

Parameter Description
name It specifies the name of the cookie.
value It specifies the value of the cookie.
expire It specifies when the cookie expires.
path It specifies server path of the cookie.
domain It specifies domain name of the cookie.
secure It indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.

Example of http setcookie function

<html>

<body>

<?php

setcookie("User", "MCN_Solution", time()+3600);

?>

</body> 

</html>


Example of retrive a cookie value
 

<html>

<body>

<?php

echo $_COOKIE["User"];

print_r($_COOKIE);

?>

</body>

</html>


Output:

setcookie-php.jpg

You may also want to read these related articles :
here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.