How to design an affiliate system on CodeIgniter, to use on every page on the site?

2.9k views Asked by At

I'm designing an affiliate system for an e-commerce site on CodeIgniter, and am wondering what is the best approach for it.

I want the affiliates to be able to link to each and every page on the website.

So ideally, the affiliates would copy the desired link on my website, add some identifying suffix, and use it as the referral link.

I thought I'd instruct the affiliate to add a /ref/ref_id suffix to every link, and then use $this->uri->uri_to_assoc(n) in my header (i.e - on every page), and look for the pair 'ref' => 'ref_id' in the resulting array.

The problem is that some of the pages have an even number of segments in the uri, and others have an odd number of segments.

so, mywebsite.com/item/item_id/ref/ref_id would yield:

array
  'item' => 'item_id' 
  'ref' => 'ref_id' 

Which is great. But mywebsite.com/aboutus/ref/ref_id would yield:

array
 'aboutus' => 'ref'
 'ref_id' => false

Which is not very helpful.

Also, I cannot use this syntax on the homepage itself (mywebsite.com/ref/ref_id) because I get a 404 page not found, since there's no ref function in my home controller.

The other option I thought was to use query string (mywebsite.com/about?ref=ref_id), but that doesn't seem very CodeIgniterish, doesn't it?

So what would be the best way to implement this?

1

There are 1 answers

4
Robin Castlin On BEST ANSWER

I've worked with an affiliate site on CodeIgniter, and the easiest thing was to simply:

http://mywebsite.com/ref/ref_id?url=whateverorthesame.com/aboutus

Then in the controllers/ref.php save a cookie based on ref_id (might want to use routing for this), and simply redirect them to $this->input->get('url'). Now you can magically redirect them to others sites aswell! :)

Have them simply input the url in a text field, and through ajax:

<script type="text/javascript">
    // jQuery dependent
    $('input#url').bind('change', function() {
        $('div#copy_box').text('<?=base_url()?>ref/<?=$user->id?>?url='+encodeURIComponent($(this).val()));
    });
</script>

Simply have a text that encourages them to copy generated string.

No need to make it complicated.