Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryCookies and Browser Interaction

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Setting and Viewing Cookies with PHP

Addition: Cookies should be set with the "secure" flag whenever a cookie is set from a web page with a secure connection (https:// vs. http://). The "https://www.willmaster.com/library/web-development/setting-secure-cookies.php library article talks about how to set a secure cookie with JavaScript and with PHP.

Here is a fun way to learn about cookies by setting them and then viewing them.

See also Setting and Reading Cookies with JavaScript .

First, let's set a cookie.

In a PHP web page, put these 14 lines of PHP code at the top of the web page source code. There may be no characters, no spaces, and no blank lines in the file above this PHP code:

<?php
$Name = "mycookie";       // Specify the cookie's name.
$Value = "hello, there";  // Specify the cookie's value.
$Directory = "/";         // Specify the valid directory.
$DaysCookieShallLast = 1; // Specify number of days.
$domain = $_SERVER{"SERVER_NAME"};
$domain = "." . preg_replace("/^www\./","",$domain);
$Directory = preg_replace("/\/+$/","",$Directory);
$Directory = preg_replace("/^\/+/","",$Directory);
$Directory = "/$Directory";
$lasting = time() + ($DaysCookieShallLast * 24 * 60 * 60);
if($DaysCookieShallLast < 1) { $lasting = ""; }
setcookie($Name,$Value,$lasting,$Directory,$domain);
?>

In the above code, you may change:

  • The cookie's name. Cookies must have a name. It can be any sequence of characters excluding semi-colon, comma and white space.

  • The cookie's value. The value may be empty or contain any sequence of characters. There is a limit to how many characters the value may contain. The entire cookie, cookie name, domain name, value, etc., may be no more than 4k in size. So the maximum size of the value by itself would be some less than 4k.

  • The valid directory. This is the directory (and its subdirectories) that the cookie may be read from. "/" represents the document root, a cookie that can be read from anywhere on the domain. "/books" would mean the cookie can only be read from directory /books and its subdirectories.

  • The number of days the cookie shall live. Specify 0 for a session cookie (a cookie that deletes when the browser is closed). Otherwise, specify the number of days.

The setcookie() function is the line that sets the cookie. The other code lines prepare the variables for the function.

If you are familiar with PHP, you may wish to put values directly into the function call and the variables dispensed with. It would give you more control over exactly what values are sent to the setcookie() function. For example, the cookie lifetime doesn't have to be in days, but can be specified in shorter blocks -- hours or even seconds. And additional parameters may be sent to the function, such as an indication that the cookie may be set and read only with a secure connection.

The above paragraph is only for those having good familiarity with PHP. For everybody else, simply pop the PHP code into the web page, as is, or with some of the variables changed.

Upload the web page with the cookie setting PHP code to your server and load the page into your browser. When the page loads into the browser, the cookie is set.

Note: When a cookie is set with PHP, the cookie will be available to the browser at the next page load, not the same page load as when the cookie was set. This is unlike JavaScript, where the cookie is available immediately.

OK, now let's view the cookie.

These 5 lines of PHP code put anywhere within the body area of a PHP web page will tell you what cookies the browser has available:

<?php
echo('<pre>');
print_r($_COOKIE);
echo('</pre>');
?>

Upload the page to your server and load the page into your browser. It will list any cookies available for the directory where the page is located.

The list of cookies is rather raw. But the information is there. Code could be written to pretty the cookie display.

The cookies specification can be studied for a deeper understanding. The specification is no longer at Netscape (which developed the specification). However, you can find a copy at https://curl.haxx.se/rfc/cookie_spec.html

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC