PHP and Cookies; a good mix!

Written by Dennis Pallett


Introduction Cookies have long been used in PHP scripts, and are a very useful function. But what exactly are cookies? Maybe you have used then, but you still don't know exactly what they are. Or you are completely new to cookies? It doesn't matter, because in this tutorial I will show you exactly what cookies are, and what they are used for.

Cookies in a nutshell Cookies are small pieces of information that is stored onrepparttar computer of your visitors. Each browser handles it differently, but most simply storerepparttar 105079 information in a small text file. Internet Explorer has a special folder, which can be found in your C:Windows or C:WindowsSystem32 folder. You can delete all your cookies, by going torepparttar 105080 Options and 'Clearing Cookies' or deleting them by hand. I don't recommend this though.

Almost every website uses cookies. If you go to Amazon.com, you will get several cookies. The same goes for CNN.com. Even Google uses cookies! They are extremely useful for (temporarily) storing information. For example, if you have a login system for your visitors, you could save their userid and password (very heavily encrypted!) so they are automatically logged inrepparttar 105081 next time they visit your website.

Or you could remember their last visit, and highlight everything that is new. And that's justrepparttar 105082 beginning.

Using Cookies Using cookies in PHP is extremely easy. In fact, there is nothing to it, because of PHP's inbuilt setcookie() function. Have a look atrepparttar 105083 documentation, and then tryrepparttar 105084 following example:

<?php

// Set a cookie // Cookie name: name // Cookie value: Dennis Pallett // Cookie expire: in 24 hours

setcookie ('name', 'Dennis Pallett', time() + (60*60*24)); ?>

If you runrepparttar 105085 code above, then a cookie will be set. That's all. The cookie name and value are pretty obvious. The cookie expire is whenrepparttar 105086 cookie expires, or goes away. Simply userepparttar 105087 time() function and addrepparttar 105088 number of seconds you want to haverepparttar 105089 cookie available to it. Inrepparttar 105090 example I added 60*60*24=86400 seconds, or 24 hours.

If you have looked atrepparttar 105091 documentation, you probably noticed there are additional arguments. Asrepparttar 105092 documentation says,repparttar 105093 path is to limit a cookie to a specific path on your web server. This is often used when you run multiple instances ofrepparttar 105094 same script in separate directories. You can safely omit this argument when it doesn't matter ifrepparttar 105095 cookie is available site-wide.

There is alsorepparttar 105096 domain argument. This can be used to limitrepparttar 105097 cookie to a specific sub-domain, e.g. test.example.com. You can also safely ignore this argument, or set it to .example.com (noterepparttar 105098 beginning period, this is essential!).

Finally, there is alsorepparttar 105099 secure argument. This argument is only used for cookies that are sent over a secure HTTPS connection (SSL). Just ignore this argument, unless you're working with a secure connection.

One thing that should be mentioned is that cookies must be set, before you display any HTML/text. It's probably best if you turn on output buffering by putting ob_start() atrepparttar 105100 top of your page.

Now that you have set a cookie, you probably want to retrieverepparttar 105101 value as well. After all, that isrepparttar 105102 whole point of using cookies. Thankfully, as PHP is ever so easy, you can retrieverepparttar 105103 same way as you retrieve a GET value. Seerepparttar 105104 following example to retrieverepparttar 105105 value ofrepparttar 105106 previous example:

<?php echo 'Your name is ' . $_COOKIE['name']; ?>

This should print "Your name is Dennis Pallett". There's nothing more to it. It's just that easy!

Finally, one thing you probably want to do as well is remove cookies. This is as easy as setting them. Simply changerepparttar 105107 value ofrepparttar 105108 cookie to FALSE, and changerepparttar 105109 expire date to -3000 seconds. Seerepparttar 105110 following example:

Screen scraping your way into RSS

Written by Dennis Pallett


Introduction RSS is onerepparttar hottest technologies atrepparttar 105076 moment, and even big web publishers (such asrepparttar 105077 New York Times) are getting into RSS as well. However, there are still a lot of websites that do not have RSS feeds.

If you still want to be able to check those websites in your favourite aggregator, you need to create your own RSS feed for those websites. This can be done automatically with PHP, using a method called screen scrapping. Screen scrapping is usually frowned upon, as it's mostly used to steal content from other websites.

I personally believe that in this case, to automatically generate a RSS feed, screen scrapping is not a bad thing. Now, on torepparttar 105078 code!

Gettingrepparttar 105079 content For this article, we'll use PHPit as an example, despiterepparttar 105080 fact that PHPit already has RSS feeds.

We'll want to generate a RSS feed fromrepparttar 105081 content listed onrepparttar 105082 frontpage. The first step in screen scraping is gettingrepparttar 105083 complete page. In PHP this can be done very easily, by using implode(file("", "[the url here]")); IF your web host allows it. If you can't use file() you'll have to use a different method of gettingrepparttar 105084 page, e.g. usingrepparttar 105085 CURL library.

Now that we haverepparttar 105086 content available, we can parse it forrepparttar 105087 content using some regular expressions. The key to screen scraping is looking for patterns that matchrepparttar 105088 content, e.g. are allrepparttar 105089 content items wrapped in <div>'s or something else? If you can successfully discover a pattern, then you can use preg_match_all() to get allrepparttar 105090 content items.

For PHPit,repparttar 105091 pattern that matchrepparttar 105092 content is <div class="contentitem">[Content Here]<div>. You can verify this yourself by going torepparttar 105093 main page of PHPit, and viewingrepparttar 105094 source.

Now that we have a match we can get allrepparttar 105095 content items. The next step is to retrieverepparttar 105096 individual information, i.e. url, title, author, text. This can be done by using some more regular expression and str_replace() onrepparttar 105097 each content items.

By now we haverepparttar 105098 following code;

<?php

// Get page $url = "http://www.phpit.net/"; $data = implode("", file($url));

// Get content items preg_match_all ("/<div class="contentitem">([^`]*?)</div>/", $data, $matches);

Like I said,repparttar 105099 next step is to retrieverepparttar 105100 individual information, but first let's make a beginning on our feed, by settingrepparttar 105101 appropriate header (text/xml) and printingrepparttar 105102 channel information, etc.
// Begin feed header ("Content-Type: text/xml; charset=ISO-8859-1"); echo "<?xml version="1.0" encoding="ISO-8859-1" ?> "; ?> <rss version="2.0"  xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:content="http://purl.org/rss/1.0/modules/content/"  xmlns:admin="http://webns.net/mvcb/"  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <channel> <title>PHPit Latest Content</title> <description>The latest content from PHPit (http://www.phpit.net), screen scraped!</description> <link>http://www.phpit.net</link> <language>en-us</language> 

Cont'd on page 2 ==>
 
ImproveHomeLife.com © 2005
Terms of Use