James Bachini

Coding PHP for Affiliates | A Getting Started Guide

phpCoding for me is one of the most important skills you can have as an affiliate marketer. I’ve met plenty of affiliates that are running huge volume, making millions and can’t write a single line of code. “I just outsource it all” but for me I couldn’t imagine coming up with a tweak to a landing page or a new concept and not being able to impliment it there and then. So here are some basic steps in getting started with PHP for the absolute novice who has never coded anything in their lives before.

What will you need:

  1. A text editor – I recommend http://notepad-plus-plus.org/
  2. A ftp client, I user fireftp for firefox but there are a million and one you can choose from.
  3. A Server, VPS or Web hosting for your new site/landing page, plus a domain name.
  4. WAMP is a good tool if you want to check your pages load from a local PC

OK so that is about all you need and all that software above is FREE.

First Steps – Hello World

So first step is to open up a new document in Notepad++ and copy and paste in the following text:

<?php
echo “Hello World”;
?>

Save that file somewhere and name it index.php

Using your FTP client connect to your web hosting account and upload the index.php file to your server. Be careful not to overwrite anything importnant, create a new directory if necessary.

Open up your favourite web browser (If you stil use Internet Explorer, go download Chrome/Firefox) and load the url for your web site i.e.
http://www.mydomain.com/newdirectory/index.php

If it’s worked you should see a blank page with Hello World written at the top…

Possible Errors:
404 – File not found “This just means your web browser is looking for a file that isn’t there did you get the path correct. File uploaded OK etc?”
403 or anything else. Google the error code and find out what you did wrong.

So what you are basically doing is writing some computer code on your home PC, transfering that to a webserver so the world can see and then viewing that code in your browser like a normal internet user would.

OK so that is a basic page. Now you have that down I’m going to provide some basic functions, loops etc. Then go through some examples.

The Basics

<?php
// Number functions
$number = 1;    // this sets the variable $number to 1
$number += 3;    // $number now equals 4
$rand = rand(0,1)   // sets the variable $rand to either 0 or 1
$total = $number * $rand;   //  star is used for multiplication

// String functions
$paintitblack = str_replace(“white”, “black”, “<body text=’white’>”);   // String replace
$jackson = substr(‘abcdef’, 0, 3)    // takes first 3 letters of a string i.e abc

// Basic loops
$c = 0;
while ($c <= 5) {
echo $c;
$c += 1;
}

if ($affiliate == ‘john’) { echo ‘Hey John’; }

// Other “fun” stuff
echo date(‘l jS F Y’);    // output todays date

if (isset($_GET[‘w’])) { echo ‘Landingpage2’; }     // if you load the url like index.php?w=whatever it will say landingpage2

$searchlink = ‘http://www.uksite.com’;
echo “<meta http-equiv=\”refresh\” content=\”0;url=”.urlencode($searchlink).”\”>”;  // basic metarefresh redirect
?>

OK so lets look at some examples of how we can use PHP in affiliate marketing.

Example 1 – Split testing landing pages

<?php
$landingpages = array(
‘http://www.mycpanetwork.com/test.php?affid=123’,
‘http://www.affnetwork2.com/test2.php’,
‘http://www.affnetwork3.com/anotherlp.php’
);
$searchlink = $landingpages[array_rand($landingpages)];
echo “<meta http-equiv=\”refresh\” content=\”0;url=”.$searchlink.”\”>”;
?>

The above is a really simple script you can use to test different offers or networks. It uses a meta refresh but you can just as easily use links like below in your html:

<a href=”<?php echo $searchlink; ?>”>Click Here</a>

Example 2 – Dynamic Content

<?php
$img = $_GET[‘pic’];
?>
<html>
<body>
Hello World, Like my pic?
<br />
<?php echo ‘<img src=”‘.$img.’” />’; ?>
</body>
</html>

You then load the url in the browser like http://www.mysite.com/myphpfile.php?pic=prettygirl.jpg

And it will load prettygirl.jpg on your web page if that image is in the same folder on the web server. For all the pros out there I know this isn’t secure coding but it’s meant to be a basic example.

Example 3 – Using a function to extract links from html code

<?php

function extract_links($text) {
preg_match_all(‘/<\s*a[^<>]*?href=[\’”]?([^\s<>\’”]*)[\’”]?[^<>]*>(.*?)<\/a>/si’,
$text,
$match_array,
PREG_SET_ORDER);
$return = array() ;
foreach ($match_array as $serp) {
$full_anchor = $serp[0];
$href = $serp[1];
$anchortext = $serp[2];
if ( (preg_match(“/http:/i”,$href)) &&
(!preg_match(“/cache/i”,$href)) &&
(!preg_match(“/google.com/i”,$href)) &&
(!preg_match(“/youtube.com/i”,$href)) &&
(!preg_match(“/wikipedia.org/i”,$href)) &&
($href[0]!= ‘/’) ) {
$anchor_array = array($href,$anchortext) ;
array_push($return,$anchor_array) ;
}
}

return $return ;
}

$html = file_get_contents(“http://www.domaintoberipped.com”);
$mylinks = extract_links($html);

print_r($mylinks);
?>

OK hope that is enough to get you started. There are some more great resources at:

As always if you get stuck https://www.google.co.uk/search?q=google+the+fucking+problem


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.


Posted

in

,

by