Easily Redirect To A Random URL Using PHP

June 29, 2009 · 35 Views · Filed Under Scripts · Comment 

Using the code below, you can send a visitor to one URL and then have them redirected to a random URL. All you have to do is open notepad, copy the code below, change the addresses and then save it as “whatever.php”. You can also add more addresses by adding more lines and changing the number.

<?php
$url[0] = "http://www.google.com";
$url[1] = "http://www.yahoo.com";
$url[2] = "http://www.47movies.com";
 
srand ((double)microtime()*1000000);
$randomnum = rand(0, count($url)-1);
 
header ("Location: $url[$randomnum]");
?>

Right now, I’m using this to redirect visitors to a random sponsor on a website that I recently closed down. I redirect all 404s to the index of the site using htaccess and then the index.php file contains the random redirect.

Hiding Affiliate Links With PHP

January 16, 2009 · 98 Views · Filed Under Scripts · Comment 

While I was working on a new layout for my main site, I thought about implementing a system of “hiding” affiliate links and making them appear cleaner.

So, instead of this:

http://secure.hostgator.com/cgi-bin/affiliates/clickthru.cgi?id=bigfish

I wanted something like this:

http://www.0092ff.com/go.php?id=hostgator

I found a ton of scripts that would allow me to do this, both free and paid versions, but I couldn’t find exactly what I wanted. I tested out a couple free scripts, but I didn’t like the way the links were created.

After a while I stumbled upon this simple PHP code that would allow me to do exactly what I wanted to do, without all the bells and whistles.

Create a text file and copy/paste the following code:

<?php
  $id = $_GET['id'];
  $links = array(
    "google" => "http://google.com",
    "yahoo" => "http://yahoo.com",
    "wikipedia" => "http://wikipedia.org"
    );
 
  header("Location:".$links[$id]);
  exit;
?>

Then, just change the names and link codes to fit your needs. Save the file as “go.php” and then set up your links like this:

http://www.yourdomain.com/go.php?id=google

http://www.yourdomain.com/go.php?id=yahoo

http://www.yourdomain.com/go.php?id=wikipedia

If you want to add more links, just copy/paste another line in. You have to pay attention to where your commas are placed though.

It doesn’t get any easier than that!

Random Text Link Script

December 18, 2008 · 111 Views · Filed Under Scripts · Comment 

This is a very simple script that will insert a random text link in a desired location. All you have to do is copy the text below and paste it in notepad and name it “random.php”.

<?
$random_url = array("http://www.google.com/1",
                    "http://www.google.com/2",
                    "http://www.google.com/3",
                    "http://www.google.com/4",
                    "http://www.google.com/5");
 
$url_title = array("Google Link 1",
                   "Google Link 2",
                   "Google Link 3",
                   "Google Link 4",
                   "Google Link 5");
srand(time());
$sizeof = count($random_url);
$random = (rand()%$sizeof);
print("<a href="$random_url[$random]">$url_title[$random]</a>");
?>

Then, insert the following code in your php file where you want the link to appear.

<?php include ("random.php"); ?>

Enjoy!

How to fix LinkEX and .htaccess conflicts

September 30, 2008 · 106 Views · Filed Under Scripts · Comment 

Last month I discussed using 301 redirects to avoid duplicate content and now I’d like to expand on that topic with something I just learned this morning. On a few of my sites, I’m using a script named LinkEX to trade text links with other sites. It seems that there is a conflict when using the 301 redirect method mentioned above with LinkEX. When I would try to login, the username/password fields would just reset. I knew the password was correct because it had been saved within Firefox. When I tried to use the password reset too, that field would just reset too.

After talking to the script creator for awhile, we figured out that this issue was caused by my .htaccess file and he provided me with a much better method of using the 301 redirects.

This was my old method:

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.htm
RewriteRule ^(.*)index.htm$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.shtml
RewriteRule ^(.*)index.shtml$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.asp
RewriteRule ^(.*)index.asp$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.aspx
RewriteRule ^(.*)index.aspx$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.cfm
RewriteRule ^(.*)index.cfm$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.pl
RewriteRule ^(.*)index.pl$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/default\.asp
RewriteRule ^(.*)default.asp$ http://www.bfxmedia.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/default\.htm
RewriteRule ^(.*)default.htm$ http://www.bfxmedia.com/$1 [R=301]

Once we figured out the issue, he provided me with this:

RewriteCond %{THE_REQUEST} !/links/ [NC]
RewriteCond %{THE_REQUEST} ^.*/(index|default)\.(s?html?|php|aspx?|cfm|pl)
RewriteRule ^(.*)(index|default)\.(s?html?|php|aspx?|cfm|pl)$ http://www.bfxmedia.com/$1 [R=301,L]

He added the RewriteCond with the “links” directory and he condensed the line with all the file extensions down to one line. Now, I’ve got to change all my .htaccess files to this new streamlined version.

If you’re looking for a good (free) link trade script, you need to get LinkEX.
LinkEX