Archive for the ‘Work’ Category

NoFollow is defantely counting for something

Friday, May 8th, 2009

A while ago I began to try and get to the front of google for my first name. I planned to do it using commenting on blogs of which the majority were nofollow.

A few weeks ago I hit number 6, not bad out of 36Million other pages…

I then paused on the blog commenting and as expected I fell the back down the ranks.

Now last week i started commenting on a few more blog posts plus on .edu blog found using the search term intitle:blog inurl:.edu and I have rose back to number 8 on this graph but on checking it manually is is shown as number 6 for Shane!

NoFollows FTW!!!

Looking for Guest Bloggers

Tuesday, March 10th, 2009

I dont get to write so many blogs now that I have got a full time job again. So I am going to open it up to the world.

If you would like to guest blog about anything Internet / PHP / Search / Marketing please drop me a message from the contact form or give us a shout over at twitter.

Shane

Force File Download Using PHP

Tuesday, February 10th, 2009

A while ago I was asked to create a method to force files to download when a user requested. More importantly MP3 files, as when a user clicked on an MP3 file it would simply play in the browser. Yes why not right click / save as… etc etc well there were language barriers between us and some other people who were receiving the files.

This led me to create the a script to force mp3 download which I then tweaked further to force any file to download. Granted I had some help over at Experts Exchange but the script I have below will do all of the following.

  • force asf downloads
  • force avi downloads
  • force doc downloads
  • force zip downloads
  • force xls downloads
  • force gif downloads
  • force mp3 downloads
  • force mpeg downloads
  • force rtf downloads
  • force htm and html downloads
  • force asp downloads

The full script to do all of this is below, as a guide I’d copy and save it as download.php in your websites root folder.

Force File Download Script

<?php
if(isset($_REQUEST["id"])){ $id = $_REQUEST["id"]; }

# the files to download
switch ($id) {
    case '2f7b5a2624':
          $strFilePath =  './file-location/filename.doc';
          break;
     case '54707b2838':
          $strFilePath =  './file-location/filename.mp3';
          break;
     default:
        header('Location: http://www.YOUR-HOMEPAGE.com');
}
# end of the files to download

$strFileSize = filesize($strFilePath);
$strFileName = basename($strFilePath);

$pathinfo = pathinfo($strFilePath);
$strFileType = $pathinfo['extension'];

switch ($strFileType) {
     case 'asf':
          $ContentType = 'video/x-ms-asf';
          break;
     case 'avi':
          $ContentType = 'video/avi';
          break;
     case 'doc':
          $ContentType = 'application/msword';
          break;
     case 'zip':
          $ContentType = 'application/zip';
          break;
     case 'xls':
          $ContentType = 'application/vndms-excel';
          break;
     case 'gif':
          $ContentType = 'image/gif';
          break;
     case 'jpg':
          $ContentType = 'image/jpeg';
          break;
     case 'wav':
          $ContentType = 'audio/wav';
          break;
     case 'mp3':
          $ContentType = 'audio/mpeg3';
          break;
     case 'mpg':
     case 'mpeg':
          $ContentType = 'video/mpeg';
          break;
     case 'rtf':
          $ContentType = 'application/rtf';
          break;
     case 'htm':
     case 'html':
          $ContentType = 'text/html';
          break;
     case 'asp':
          $ContentType = 'text/asp';
          break;
     case 'mov':
          $ContentType = 'video/quicktime';
          break;
     case 'txt':
          $ContentType = 'text/plain';
          break;
     default:
          //Handle All Other Files
          $ContentType = 'application/octet-stream';
}

header("Content-Type: $ContentType");
header("Content-Length: $strFileSize");
header("Content-Disposition:attachment; filename=$strFileName");

$fd = fopen ($strFilePath, "r");
while (!feof ($fd)) {
    $buffer = fgets($fd, 32768);
    echo $buffer;
}
fclose ($fd);
?>

The only bit I am going to explain is right at the top in the switch statements. This is where we list our files that we want to force download of. If we take a look closer to the top the page is ready to accept an ID in the url. If you have saved your file as download.php we can access a download using this

http://www.domain.com/download.php?id=YOURIDHERE

Where YOURIDHERE would be replaced with the id in the case below code. To download the below file you would refer to it using

http://www.domain.com/download.php?id=2f7b5a2624

and as it is a true match it would then exit the switch statement with the $strFilePath variable populated and ready for the rest of the code to process the forcing of the download.

    case '2f7b5a2624':
          $strFilePath =  './file-location/filename.doc';
          break;

The letters and numbers you put in the case ‘ ‘ are up to you, whatever you put here will need to be the same as the id= in the url to make this particular file download.

You can have as many files as you want listed here. or if you liked you could link it all to a MYSQL database. One thing to remember is that you do not have the same id= string.

We also have the following up the top there

     default:
        header('Location: http://www.YOUR-HOMEPAGE.com');

You will need to change this to a page on your site, or maybe even your 404 page. The default will pick up any attempts to get into the page using invalid id’s and with automatically forward them to the page you put here.

This script can handle very large filetypes with ease, I have used it to force downloads of files that are hundreds of meg in size.

Use this code it will serve you wisely, any thought or suggestions on this code then drop us a comment.

Shane

Displaying your Twitter Posts on your Webpage with PHP

Friday, February 6th, 2009

Moving on from my last post about displaying data from RSS on your page using php this post will show you how to display your latest Twitter Posts on a XHTML page without using any third party software packages. I used the same code as the previous post however this time I had modified the way it displayed, to see an example of this in action see the ShaneDJ Homepage The code I have used is below.

<?php
    $rss_url = "http://twitter.com/statuses/user_timeline/15298261.rss";

    if (!$rss_data = @file_get_contents($rss_url)) {
        echo "<p>Ooooops</p><p>Looks like Shane's Twitter feed isn't working at the moment.</p>";
    } else {

    $rss_xml = SimpleXML_Load_String($rss_data);

    $channel_title = $rss_xml->channel->title;
    $channel_link = $rss_xml->channel->link;

    foreach ($rss_xml->channel->item as $item) {
        $item_title = $item->title;
        $item_link = $item->link;
        $item_description = $item->description;

        $tweet = explode(": ", $item_title);       
        $tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
        $tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"twit_at\">$1</span>", $tweetmessage);
        echo "<li><b>Shane tweeted</b> - " . $tweetmessage . " - ";
	echo "<a href=\"" . $item_link .  "\" rel=\"nofollow\">View in Twitter</a></li>\n";   
        }
    }

?>

First things first you need to change your $rss_url to the location of your RSS twitter feed. Then If you look at the following lines.

        $tweet = explode(": ", $item_title);

This line will split the $item_title this is needed so that we can split the title into two parts. Part 1 is the username and part 2 is the actual message.

        $tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
        $tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"twit_at\">$1</span>", $tweetmessage);

The next set of lines change the format of certain elements, the first one uses regex to find and replace the URL’s and make them actively clickable. Note the use of nofollow there too. The second line formats other users by looking for their @username names and changing the style of it.

        echo "<li><b>Shane tweeted</b> - " . $tweetmessage . " - ";
	echo "<a href=\"" . $item_link .  "\" rel=\"nofollow\">View in Twitter</a></li>\n";

The last 2 lines display all of the data in the way that we have declared it. At the end of the message we have a View in Twitter link. This will take you to the actual post. Good for people that want to reply to your message.

Once again to see this in use check out my homepage.

Shane

Displaying RSS contents on a page using PHP

Thursday, February 5th, 2009

A while ago I set out to work finding out how to display the contents of an RSS feed onto a XHTML page. Grated there where lots that served up the contents using JavaScript and Flash. The main usage of this code lives on a local page that can not be accessed online. Its like my own little RSS reading area that is hidden away.

So here is the code that displays this blogs posts on a html page

$rss_url = "http://www.shanedj.com/blog/feed/rss/";
    if (!$rss_data = @file_get_contents($rss_url)) {
        echo "<p>No RSS found at $rss_url</p><p>Hmmm thats not meant to happen. RSS Fail!</p>";
    } else {

    $rss_xml = SimpleXML_Load_String($rss_data);

    $channel_title = $rss_xml->channel->title;
    $channel_link = $rss_xml->channel->link;

    foreach ($rss_xml->channel->item as $item) {
        $item_title = $item->title;
        $item_link = $item->link;
        $item_description = $item->description;
        echo "<h3><a href=\"$item_link\">$item_title</a></h3><p>$item_description</p><p class=\"text_right\"><a href=\"$item_link\">Continue reading  $item_title</a></p>\n";
        }
    }

The main part that displays the data on the page is right at the end

        echo "<h3><a href=\"$item_link\">$item_title</a></h3><p>$item_description</p><p class=\"text_right\"><a href=\"$item_link\">Continue reading  $item_title</a></p>\n";

You can change this however you want to display it in whatever fashion you want.

Values that you can echo to get data are as follows

$channel_title - This is the usually the blog name
$channel_link - This is usually a the blog url
$item_title - This is the title of a post
$item_link - This is the URL of the post
$item_description - This is the summary contents of the post

You can play around with displaying it however you want. For more information about playing with RSS (XML) with php. Bounce over to the XML section at PHP.net

Shane