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

5 responses. 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]

  2. kOLEK Ads
    Nov 20, 2012 at 03:20:31
    #2

    Nice tips. Good job

    [Reply]

  3. rahul
    May 29, 2015 at 10:50:53
    #3

    qwerty

    [Reply]

  4. Pest
    Dec 21, 2016 at 21:15:35
    #4

    How do I set the reff value to be in the box on the registration page?

    [Reply]

  5. mukesh jakhar
    Apr 25, 2017 at 10:42:49
    #5

    This is awesome help me, but can you suggest about how to set reff value..

    [Reply]

Post a Comment

OR