answersLogoWhite

0

How do you use cookies in PHP?

Updated: 9/11/2023
User Avatar

Pandoga

Lvl 1
14y ago

Best Answer

You can set a cookie using the "setcookie()" function, which takes the following arguments:

setcookie($name, $value, $lifetime, $path, $domain, $secure, $httponly);

$name = The name of the cookie (String)

$value = (Optional) The value of the cookie, like a username or code (String)

$lifetime = (Optional) The lifetime of the cookie (Integer of seconds)

$path = (Optional) The directory / path of a server, in which the cookie will be available in (String)

$domain = (Optional) The domain name at which the cookie is available at (String)

$secure = (Optional) Determines whether the cookie should only be sent while there is a secure connection (If you don't know what this is, just make it false) (Boolean)

$httponly = (Optional) Determines whether the cookie can only be accessed through the HTTP protocol, and not by other scripting languages like JavaScript or VBScript (Boolean)

The only arguments you need to focus on here, for starters, is the $name, $value, and $lifetime. All other arguments are optional and can be left alone, just for the purpose of learning how to set a cookie.

Here is an example of how you could set up a cookie:

Setting a cookie


setcookie('mycookie', 'hello', time()+3600);

We set the name of the cookie to "mycookie".

We said the value of the cookie is "hello".

We said that the cookie should expire at the current time + 3,600 seconds. This means that the cookie should delete itself after 3,600 seconds.

Displaying a cookie's value

Now that you have set the cookie, your going to need to retrieve it. When you send a cookie, it won't be available on the same page - you must go to another page, or refresh the page, to have the cookie truly "set".

The $_COOKIE variable contains all the values of every cookie, stored on a user's computer, that is available to the website.

You can access a specific cookie by putting it's name into the $_COOKIE array. For example, in our case, we would say $_COOKIE['mycookie'] to retrieve our cookie information.

echo $_COOKIE['mycookie']; // Displays "hello"

Changing the value of a cookie

You can also alter the value of a cookie by "setting" the cookie again, but with a different value.

setcookie('mycookie', 'its a different value!', time()+3600);

Deleting a cookie

And, of course, you may want to delete cookies. There isn't a specific delete-cookie function. However, you can "set" the cookie again, and specify a time in the past - so then the browser should immediately dispense of the cookie.

You won't need a value, since it's going to delete itself anyway. You can go ahead and set it to the NULL constant (or, give it a value - either way works).

setcookie('mycookie', NULL, time()-3600);

// or

setcookie('mycookie', 'goodbye', time()-3600);

// or

setcookie('mycookie');

For more information about cookies, see the PHP and w3schools references, respectively listed.

[ http://www.php.net/setcookie ]

[ http://www.w3schools.com/PHP/php_cookies.asp ]

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use cookies in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Basic Math

Why we use javascript as we have a powerful scripting language php?

First of all, PHP is server side, Javascript is client side. You cannot detect mouse gestures or any clicks with PHP the same way you cannot read a file or modify it or process a form (unless you use AJAX of course). Also, although I love PHP, and it has useful extensions such as the GD library, PHP is not a very good language. Overall we use javascript and PHP because they are used for two completely different things.


What is syntax in php?

We can use php tags in different ways. <?php //php code to be written here ?> OR <? //php code ?> This tag will not work when we using editors such as macromedia dreamweaver. OR < script language="php"> //php code </script>


What character allows you to concatenate strings in PHP?

To concatenate strings in PHP, you use the . (dot) character. For example: $str = "String1" . "String2";


When should globals be used in PHP?

Problem with using global variables in php is that they lose the the assigned value in a different php file. They only keep the global variable value in the php file in which they are declared. Instead of globals try and use $_SESSION or $_COOKIE to keep value intact across different php files in a project


You use a Html button in Php on its onclick you made a function but its not responding?

this is not really a question... But I think you are asking why php functions inside a HTML button do not work. Well probably because php is rendered serverside and is passed to a user-browser afterwards.... it is possible to use php inside onclick... but only to display specific content (javascript function for instance)... it is not possible to let a user decide to run a specific php function by clicking a button.

Related questions

What are cookies in php?

This should help:


How do websites cookies?

In php programming language, you can set cookies like that: setcookie("username","demo");


Are cookies useful in PHP?

Yes because you can use them to create website logins, also to store temporary data and much more.


What is the use of a question mark in php?

It is used to tell the sever to use the PHP parser. To begin php you must use <?php, and to end it, it is ?>.


How do you use PHP in HTML?

You can't use PHP in an HTML document, but you can use HTML in PHP script.


How can you create a language converter for website in English to Spanish in PHP?

You can use PHP to create a language converter for a website by storing language translations in a database or files, and then using PHP to dynamically switch the content based on the selected language. You can create language files for each language, and then use PHP sessions or cookies to store the user's language preference across pages. Finally, use PHP functions to display the content in the appropriate language based on the user's selection.


What is the default extension that most Web servers use to process php scripts?

PHP files extension is .php


Why should you use PHP and not JSP?

Firstly, this is wrong question. The right one would be WHEN should you use PHP, not JSP? or, WHEN should you use PHP and WHEN JSP?


How do you use XML in PHP?

PHP 5 has a XML parser called SimpleXML that you could use.


How do you create a web mail script using PHP?

You can use phpMailer() Or, you can use mail() PHP function to send emails via PHP script.


What is a PHP certificate?

PHP certification is received after doing some sort of PHP course, there are many out there to do. You can use them in your CV to help you get a job in PHP.


Where could one find information about how to use PHP 5?

You can find information about how to use PHP 5 on the official PHP website, PHP.net, through the PHP 5 documentation section. Additionally, online tutorials, forums, and books dedicated to PHP programming are great resources to learn how to use PHP 5 effectively.