How To Set Cookie in PHP For Remember Me

This page contains detailed information about how to set cookie in PHP for remember me.

Let’s get started.

How to set cookie in PHP?

What is a Cookie?

First of all, you all need to know what actually the cookie is. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Creating/setting a cookie

to set a cookie there are many different ways, but I am showing you the only best way.
to create a cookie you need to write

 setcookie(cookie_name,cookie_value);

Note: you can also set the time for the cookie(The expiry time after the cookie value)

example:

[gistpen id="164"]

Yeah, the cookie is now set.

Printing/Displaying the cookie value

To print the cookie you need to write the following code:

echo $_COOKIE["MyName"]; 

for example:

[gistpen id=”161″]

Modifying the cookie

actually modifying the cookie means re-setting the cookie again. in this post i have already set a cookie now i am gonna edit the cookie by setting the cookie value in the same cookie.

so here we go:

[gistpen id=”171″]

Deleting the cookie

to delete the cookie you have to set the expiry time. and the expiry time should be in -(it means before now).

so let’s have a look

[gistpen id=”178″]

Note: You also have to set cookie value blank, so it will perform a better result.

here I have destroyed the cookie before 600 Nano Seconds of the execution time.

************************************************

Do you love this article on how to set cookie in php? Let’s share this

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.