Jump to content

simple referral script


herghost

Recommended Posts

Good evening guys and gals,

 

for some reason I cant get my head around something quite simple, basically I am trying to work out a way of simply recording referrals to my site when the potential user registers.

 

This worded example is basically what I want:

 

registered user a with a username of smithy invites his friend western to become a member of a site he has joined:

 

I guess here I would basically use something like mysite.com?refid=smithy as the link he gives he's friend

 

Now this is where my brain refuses to work, what if western decides to have a look around the site before he decides to register and clicks on a few internal pages? Obviously the referral id will no longer be in the address bar.

 

So my 1st question is, how can I check to see if a referral is used when accessing the site? I could then store the username in a session or cookie.

 

If I had something like this:

<?php

$refid = $_GET('refid')

setcookie("user", $refid, time()+3600);
?> 

 

But how would I check that a refid exists in the url so it doesnt throw up a parse error?

 

I have more questions but 1 thing at a time!

 

Many Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/217777-simple-referral-script/
Share on other sites

When the prospective user goes to [ mysite.com/register.php?refid=smithy ], check to see if refid is set (in a session, or in $_GET data), then if it is valid (Check if "smithy" is actually a user).

 

If it is, save it in a session. If the user strays away from the registration page, the refid is saved in the session, and can be pulled into the registration script later.

 

session_start();

// See if a refid is being given in the URL
if (strlen($_GET['refid']) > 0) {
  $refid = $_GET['refid'];
}

// If there is no refid in the URL, check the session
elseif (strlen($_SESSION['refid']) > 0) {
  $refid = $_SESSION['refid'];
}

// If a refid is set, check to see if the referring user exists
// Take note that the registration process shouldn't stop if the user does not exist;
// just a notice should be given.
$sql = 'SELECT referring user, etc';
if (!$result = mysql_query($sql)) {
  echo 'Note: Could not check if the referring user exists.';
}
elseif (mysql_num_rows($result) === 0) {
  echo 'Note: Referring user does not exist.';
}

// The user exists
else {
  // Write the current refid to the session.
  // Now, if the user strays away, the session will be checked for the most recent refid
  $_SESSION['refid'] = $refid;
  // Echo an <input> tag for the refid, for the registration form
  echo '<input type="hidden" name="refid" value="' . htmlspecialchars($refid) . '" />';
}

// Continue the registration process

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.