RSS script with cache function

The native function SimpleXML in PHP5 is a great functionality to parse a XML feeds as HTML show content on your website. Maybe you like to show your last blog posts on your company’s website? If your website has some bigger traffic, you should think about to cache the RSS/XML file to lower the server load on the host where your RSS feed is hosted. This small PHP snippet will check if there is already a fresh cached version and if not a new feed is requested with PHP file functions. In that case a cache version for the new XML file is stored on the website’s host. With this function the XML is stored unchanged, why? The XML has already a data structure which we use for later purposes. Using simpleXML, the XML is converted into an object and this object is used together with a loop to show some content in your HTML. In our example the RSS feed from our WordPress weblog is parsed to show the latest posts on our website.

Continue reading RSS script with cache function

Extract the TLD from a domain name

There is no standard PHP function to extract the TLD from a domain name, parsing an URL with parse_url() will return the host name and other URL information. If you need only the TLD, for example to query the WHOIS data of a given domain name, the following script should be useful. To run this script you need a database with all TLD’s. The snippet is using a database table which is required for the scripts functionality, download the MySQL table dump here. Continue reading Extract the TLD from a domain name

Shorten text values without to break words

There are several situations were a shorter text version on your website is necessary: Post summaries for result pages or just for the page’s META description (of course it’s better to write a unique description). The regular PHP function “substr()” will shorten your text to a given length without a check if the text will end in a middle of a word.

Continue reading Shorten text values without to break words

Count clicks in your MySQL database

With this simple script it’s possible to count and track (country code and IP address) clicks from a visitors which followed a link to one of your link partners or advertisers. This script can be used together with your existing link database or, if you have made some modifications, with an array of links. I used the IP2nation database to get the visitors country code to store this code with the other information. We use the server variable HTTP_REFERER to store the URL, where the link was clicked, with the other data. You can use this script to cloak affiliate links too.

Use these SQL statements within your MySQL client:

CREATE TABLE `links` (
  `id` int(11) NOT NULL auto_increment,
  `titel` varchar(75) NOT NULL default '',
  `naam` varchar(35) NOT NULL default '',
  `url` varchar(150) NOT NULL default '',
  `description` text NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

CREATE TABLE `clicks` (
  `id` int(10) NOT NULL auto_increment,
  `link_id` int(10) NOT NULL default '0',
  `visitor_ip` varchar(15) NOT NULL default '',
  `click_at` datetime NOT NULL default '0000-00-00 00:00:00',
  `country` char(2) NOT NULL default '',
  `on_page` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

Install in the same way the data from ip2nation tables

The PHP code snippet

Create a script past this code between the PHP tags.

// place here your database connection code

if (isset($_GET['id']) && intval($_GET['id']) > 0) {
    $delay = 12*3600; // change here the number of hours how often a unique click must be counted
    $sql_check = sprintf("SELECT COUNT(*) AS test FROM clicks WHERE link_id = %d AND visitor_ip = '%s' AND UNIX_TIMESTAMP(click_at) + %d > UNIX_TIMESTAMP(NOW())", $_GET['id'], $_SERVER['REMOTE_ADDR'], $delay);
    if (mysql_result(mysql_query($sql_check), 0, "test") == 0) {
        $country_sql = "SELECT country FROM ip2nation WHERE ip < INET_ATON('".$_SERVER['REMOTE_ADDR']."') ORDER BY ip DESC LIMIT 0,1";
        $country = mysql_result(mysql_query($country_sql), 0, "country");
        $sql_insert = sprintf("INSERT INTO clicks (link_id, visitor_ip, click_at, country, on_page) VALUES (%d, '%s', NOW(), '%s', '%s')", $_GET['id'], $_SERVER['REMOTE_ADDR'], $country, $_SERVER['HTTP_REFERER']);
        mysql_query($sql_insert);
    }
    $sql_url = sprintf("SELECT url FROM link_table WHERE id = %d", $_GET['id']);
    $url = mysql_result(mysql_query($sql_url), 0, "url");
    header("Location: ".$url);
    exit;
} else {
    header("Location: http://www.yourwebsite.com/");
    exit;
}

How-to use the script in your web page?

The script above is your link target, you need to create links like:

<a href="http:domain.com/myclickscript.php?id=34">Click this link please</a>