How to setup own referral or affiliate system in PHP in easy steps

Posted By: KP On July 18, 2009 Under Internet, Other, Programming, Web Application, Web Design, Web Master Categories

Affiliate is system is what in which you track your current member referred some new member to join your site or purchase your product. You give some reward for it. Making or better say coding your affiliate system is easy. Benefit of own script is you can customize it as per your need.

We will use cookies and MySql database to track affiliates. Why cookies not directly storing in some variable ? If we store in cookie the referrer code will stay in user browser till he/she clears cookie, but if we use PHP variable and if page changes or registration fails the affiliate won’t get counted.

//here we are starting new session then we are checking whether the new comer is referred by someone else before this or not, if not then store the referrer name in session’s variable named referral, we are getting referral code as get. For example pagename.php?reff=kpsofts

<?php
session_start();
if($_SESSION['referral'] == ”){
$_SESSION['referral']=$_GET['reff'];}
?>

//now we have referrer’s name in session’s referral variable we can use it anywhere till the session has been started on page load and unless the session has not been destroyed.

//keep referrer code and database user name same. On successful registration query the referral variable=username, if match is found increase number of referral by 1.

//on start of page we will start session and take the session referral variable value in php variable.

<?php
session_start();
$reff=$_SESSION['referral'];
?>

//after successful registration you will need to add code something like that.

<?php

$query =”select username from user where username=’”.$reff.”‘ “;
$list=mysql_query($query)
or die(‘Error: ‘ .mysql_error());
if(mysql_num_rows($list)==0)
{
return 0;
}
else
{
$query =”update user set totalref=totalref + 1 where username=’”.$reff.”‘ “;
$list=mysql_query($query)
or die(‘Error: ‘ .mysql_error());
return 1;
}

?>

//even you can add a field that the user is referred by whom to make a cross check.

If you have any question regarding this ask in comments, for other questions use Help Center.

Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

One response. Wanna say something?

  1. gasa
    Jul 31, 2009 at 19:40:15
    #1

    I Like this post bro… nice post n thanks for share

    [Reply]

Post a Comment

OR