PHP and Cookies; a good mix!

Written by Dennis Pallett

Continued from page 1

<?php setcookie ('name', FALSE, time()-1000); ?>

Checking if cookies are enabled Before you start using cookies, you must make sure your visitor has cookies enabled. This can be done with a simply PHP checking script. Unfortunately,repparttar PHP page needs to reload to check for cookies. But this can be done very transparently, and your visitor should hardly notice anything.

The following example will first set a test cookie, then reloadrepparttar 105079 page, and finally check whether cookies are enabled.

<?php error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);

// Check if cookie has been set or not if ($_GET['set'] != 'yes') { // Set cookie setcookie ('test', 'test', time() + 60);

// Reload page header ("Location: checkcookies.php?set=yes"); } else { // Check if cookie exists if (!empty($_COOKIE['test'])) { echo "Cookies are enabled on your browser"; } else { echo "Cookies are <b>NOT</b> enabled on your browser"; } } ?>

Runrepparttar 105080 code above, and see whatrepparttar 105081 output is. Check if cookies are enabled in your browser. If they're not enabled, then you can enable them by going to your browser's options. Unfortunately, this is different from each browser, so I can't give you exact instructions. But Google can.

Storing Arrays One feature of cookies that is often missed in articles isrepparttar 105082 ability to story arrays. Cookies can be used to store multi-dimensional arrays, which can be extremely useful to store data.

Considerrepparttar 105083 following code;

<?php setcookie ("name[first]", "Dennis", time() + (60*60*24)); setcookie ("name[last]", "Pallett", time() + (60*60*24)); ?>

You can then display these two cookies usingrepparttar 105084 following code:

<?php echo "First Name: " . $_COOKIE['name']['first']; echo "<br />Last Name: " . $_COOKIE['name']['last']; ?>

The cookie 'name' is an array, and has multiple values. You can even go deeper and have multi-dimensional arrays, e.g. $_COOKIE['name']['test']['something']['value']. You could store whole arrays of data in cookies. But beware that you don't store too much data, there are certain size limits to cookies.

In Conclusion... Cookies are really versatile, and can be used for a lot of different purposes. Many websites use cookies, and cookies can really make your website more personalized. Using cookies in PHP isn't hard at all, and you should be able to use them without any difficulty.

Before actively using cookies in your website, you must check whetherrepparttar 105085 visitor has enabled them in their browser. If they don't have cookies enabled, you must either redirect to a non-cookies version of your website, or you can make sure your website also works without cookies.

You can download a sample script at, where cookies are used in a (somewhat) practical way. In this example, there is a logging module, called log.php and a display module, called history.php. Basically, you includerepparttar 105086 log.php in other PHP pages, and then you can view history.php to lookup allrepparttar 105087 pages you have viewed and how often. The example uses arrays, and stores them in cookies.

The examples in this article can be downloaded at

If you have a really unique practical way of using cookies, please let me know at dennis [AT] nocertainty [DOT] com. I'd really like to hear about interesting ways of using cookies.

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites at, and

Screen scraping your way into RSS

Written by Dennis Pallett

Continued from page 1

<?Now it's time to loop throughrepparttar items, and print their RSS XML. We first loop through each item, and get allrepparttar 105076 information we get, by using more regular expressions and preg_match(). After thatrepparttar 105077 RSS forrepparttar 105078 item is printed.

<?php // Loop through each content item foreach ($matches[0] as $match) { // First, get title preg_match ("/">([^`]*?)</a></h3>/", $match, $temp); $title = $temp['1']; $title = strip_tags($title); $title = trim($title);

// Second, get url preg_match ("/<a href="([^`]*?)">/", $match, $temp); $url = $temp['1']; $url = trim($url);

// Third, get text preg_match ("/<p>([^`]*?)<span class="byline">/", $match, $temp); $text = $temp['1']; $text = trim($text);

// Fourth, and finally, get author preg_match ("/<span class="byline">By ([^`]*?)</span>/", $match, $temp); $author = $temp['1']; $author = trim($author);

// Echo RSS XML echo "<item> "; echo " <title>" . strip_tags($title) . "</title> "; echo " <link>" . strip_tags($url) . "</link> "; echo " <description>" . strip_tags($text) . "</description> "; echo " <content:encoded><![CDATA[ "; echo $text . " "; echo " ]]></content:encoded> "; echo " <dc:creator>" . strip_tags($author) . "</dc:creator> "; echo " </item> "; } ?>

And finally,repparttar 105079 RSS file is closed off.
</channel> </rss>
That's all. If you put allrepparttar 105080 code together, like inrepparttar 105081 demo script, then you'll have a perfect RSS feed.

Conclusion In this tutorial I have shown you how to create a RSS feed from a website that does not have a RSS feed themselves yet. Thoughrepparttar 105082 regular expression is different for each website,repparttar 105083 principle is exactlyrepparttar 105084 same.

One thing I should mention is that you shouldn't immediately screen scrape a website's content. E-mail them first about a RSS feed. Who knows, they might set one up themselves, and that would be even better.

Download sample script

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites at, and

    <Back to Page 1 © 2005
Terms of Use