Posts Tagged ‘php’

RSS Feed: Building an RSS Data Feed in PHP with SimpleXML

Thursday, February 21st, 2008

RSS feeds are a must-have for modern websites. It’s easy enough to make an RSS feed of recent articles in PHP. But did you know an RSS feed can simply be information - not links to articles?

This article will look at how to build a basic feed of data - for our purposes, we’ll consider random quotes. It will also illustrate how to use SimpleXML to build the feed for us.
(more…)

How to Only Show the Digg Badge for Popular Posts

Monday, February 18th, 2008

Digg can be a great source of traffic and recognition for tech related blogs. Some people take it to the extreme though, and show the “Digg This” badge on every article they write.

As others have pointed out, this can be counter-productive. People don’t like to read articles that have just one Digg - they seem unpopular.

So here’s a solution: use the Conditional “Digg This” Wordpress Plugin to only show the Digg Badge for articles that have a certain number of Diggs.
(more…)

Digg API: Grabbing a Random Digg Story with PHP

Saturday, February 16th, 2008

I read about the Digg API the other day, and I thought it was about time I played around with it.

There is a ton of cool stuff you can do with this. Basically, you send a request to the Digg server and it sends back whatever kind of information you want - category names, recent diggs, recent stories, archived stories, etc. You can read through the API to see everything you can do.

For now, we’ll focus on one nifty little trick - grabbing a random story from a given topic.
(more…)

Which PHP Logical Operator Is Better - ‘and’ or ‘&&’?

Saturday, February 16th, 2008

I was browsing the Sitepoint forum today and somebody raised this question - is it better to use ‘and’ or ‘&&’ as a logical operator in PHP?

In most cases, they evaluate the same way. For example, these two statements are equivalent.

if ($x && $y) { ... }
if ($x and $y) { ... }

So what’s the difference?
(more…)

Cache Cleanup Script - Delete Old Files

Friday, February 15th, 2008

The Cache Cleanup Script is designed to do just what it says - clean up temporary files created in some sort of cache directory. This script is meant to accompany some other kind of script that temporarily stores information in a file.

How Cache Cleanup Works

Here’s the source code in text format.

At the top of the script, you need to set two values - $dirName and $timeLimit.

$dirName should be a local path to the directory you want to scan for old files. $timeLimit should be the maximum allowable inactive time, in seconds.

The logic of the script is very straightforward. First, we scandir() to get an array of filenames in our cache directory.

Then, we loop through each fileName. If the file was last accessed before our timeLimit, we delete the file. If not, we leave the file alone.

If the script attempts to delete a file, it will output a success or failure message. Otherwise, there is no output.

How to Implement Cache Cleanup

First, set the configuration variables to your desired settings. I intentionally refrained from using any kind of input into the script for security reasons.

Next, decide how you want to run the script.

If your host allows you to run cron jobs, the logical choice would be to make this a cron job run every so often. Depending on how long you want the temporary files to remain on your server, I’d run the cron job once a day or so.

If your host doesn’t allow cron jobs, you could manually access the script each time you want to check for idle files and delete them. This isn’t a bad choice if you want to check once a week or so, but it is kind of annoying to do it every day.

You could also incorporate this into your caching script. Every time the script creates or accesses a cached file, it could call this script (as a function) and delete unused files. This may not be a desirable option for performance reasons if you have a lot of cached files, but it wouldn’t be bad for smaller cache directories.

Anyhow, I just wrote the script. You figure out how you want to implement it.

$dirName = 'cache';	// Path to the cache directory
$timeLimit = 300;	// Max time since a file has been accessed
			//   before it should be deleted
 
if (is_dir($dirName)) {
  $files = scandir($dirName);
}
 
foreach ($files as $file) {
  if ($file == '.' || $file == '..') {
    continue;
  }
 
  if (fileatime($dirName . '/' . $file) < time() - $timeLimit) {
    if (unlink($dirName . '/' . $file)) {
      echo "$file deleted successfully.";
    } else {
      echo "$file not deleted.";
    }
    echo '<br />';
  }
}

How to Access XML Attributes in PHP Using SimpleXML

Friday, February 15th, 2008

A user on Programming Talk had a problem the other day. He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).

His parser wasn’t working - because all of the information was stored in attributes instead of child elements. What’s the difference between the two? And how can we fix it?

Use SimpleXML.
(more…)

How to Create an RSS Feed for Your Site in PHP

Thursday, February 14th, 2008

Most blogging platforms come with built-in support for RSS feeds. Why? It’s a great way for users to keep up to date on your new content. Instead of loading up a dozen favorite sites, they can look at one feed reader and then decide what to pursue further.

So what if you don’t use a blogging platform or CMS that creates a feed for you? You can create one yourself in PHP. It’s pretty simple.
(more…)

How to Alternate CSS Styles Using PHP

Wednesday, February 13th, 2008

There are a lot of situations in which you’d like to alternate CSS styles from one element to the next. Long tables, for example, look a lot nicer when the background color of the rows is alternated.

How do we do that? Some pretty simple PHP can help you accomplish this task and spice up your HTML/CSS.
(more…)

New Script: PHP Length Conversion Class

Sunday, February 10th, 2008

I was looking for a project to tackle today, and I decided to make a script that would convert lengths from one unit or system to another.

It doesn’t sound all that complicated - and it’s not. I had a working script in a few minutes, but I didn’t like it. It had too many functions to do conversions for individual units. I wanted this script to be pretty well abstracted so that I could easily add new unit types - and so that the source code for the class would be very short.
(more…)

How to Modify a Wordpress RSS Feed Widget with Rel=Nofollow

Saturday, February 9th, 2008

I just added an RSS feed widget to my site (see the lower right corner - the DZone links). I frequent dZone a decent amount, and I find a lot of the articles interesting.

However, after some thought I became concerned that ten sitewide external links might be a bit of a drain on my pagerank. If only I could add “rel=nofollow” to all those links. But there’s no option in the RSS Widget to do that.
(more…)

buy cheap viagra buy cheap viagra online buy discount viagra online buy viagra from india buy online viagra buy viagra india buy viagra online cheap viagra cheap viagra online discount viagra discount viagra online viagra india rx get viagra online non prescription viagra order viagra order viagra india order viagra online viagra for sale viagra no prescription viagra on line buy viagra online no prescription best price for viagra buy cheapest viagra buy generic viagra buy generic viagra from india online buy sildenafil citrate buy sildenafil citrate online buy viagra cheap buy viagra cheap prices buy viagra online without prescription cheapest prices on generic viagra cheapest viagra cheapest viagra prices cheap generic viagra cheap sildenafil citrate discount viagra canada email viagra get viagra no prescription get viagra without prescription low price viagra mail order viagra mastercard viagra order generic viagra order sildenafil citrate viagra 50/100mg viagra online without prescription viagra order online viagra sildenafil citrate viagra soft tabs viagra soft tabs for sale viagra soft tabs online viagra soft tabs without prescription buy viagra soft tabs cheap viagra soft tabs discount viagra soft tabs generic viagra soft tabs order viagra soft tabs buy cheap kamagra buy cheap kamagra online buy discount kamagra online buy online kamagra buy kamagra india buy kamagra online cheap kamagra cheap kamagra online discount kamagra discount kamagra online get kamagra online non prescription kamagra order kamagra order kamagra india order kamagra online kamagra for sale kamagra no prescription kamagra on line buy kamagra online no prescription best price for kamagra buy cheapest kamagra buy generic kamagra buy kamagra cheap buy kamagra cheap prices buy kamagra online without prescription cheapest prices on generic kamagra cheapest kamagra cheapest kamagra prices cheap generic kamagra discount kamagra canada email kamagra get kamagra no prescription get kamagra without prescription low price kamagra mail order kamagra mastercard kamagra order generic kamagra kamagra online without prescription kamagra order online