FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how to - referral system ???





netpro
How to make:
1. referral system (php mysql); example: http://domain.com/?ref=123
2. 123 is taken from registered user
3. if visitor come without referrer, script will select (automatic random) from registered user

Please help me !
Thanks
hexkid
netpro wrote:
How to make:
1. referral system (php mysql); example: http://domain.com/?ref=123
2. 123 is taken from registered user
3. if visitor come without referrer, script will select (automatic random) from registered user


Code:
<?php
$referral = (isset($_GET['ref'])) ? ($_GET['ref']) : (random_registered_user());
?>
where random_registered_user() gets a random user from the database with something like
Code:
select user_id from usertable order by rand() limit 1
netpro
Thanks very much !
I was modified the script;

<?php
function random_registered_user() {
include "conn.php";
$random = mysql_query("SELECT * FROM `user` order by rand() limit 1",$koneksi);
while($brs = mysql_fetch_array($random))
{
echo "$brs[0]";
}
}
//
$referral = (isset($_GET['ref'])) ? ($_GET['ref']) : (random_registered_user());
echo "$referral";
?>


but, I have problem with track referrer to others page.
so, if user come with referrer, all page on my site will tract that visitor come with the referrer
please tell me again about tracking referrer,
no matter it's use cookies or session

And if I type on browser: ?ref=999
the referrer is 999, though the 999 is not registered id.
how to fix this problem ?.
only registered id (user) will be track, and if visitor typed the unregistered (unknown) id (user), script will select radomly from registered id.

Thanks
hexkid
netpro wrote:
Thanks very much !
You're very welcome.

netpro wrote:
if user come with referrer, all page on my site will tract that visitor come with the referrer
please tell me again about tracking referrer,
no matter it's use cookies or session
I'd rather do it with sessions. See below

netpro wrote:
And if I type on browser: ?ref=999
the referrer is 999, though the 999 is not registered id.
how to fix this problem ?.
Validate the id.

Code:
<?php
// Pay attention to ### comments
// This script was typed/edited directly in the browser
// and was not tested!

### start the session; you need this in every script!
session_start();

function random_registered_user() {
  ### the functions will assume a connection is established and use it
  // include "conn.php";
  ### why SELECT *?
  ### If you only want the user_id get only the user_id
  // $random = mysql_query("SELECT * FROM `user` order by rand() limit 1",$koneksi);
  $random = mysql_query("SELECT user_id FROM `user` order by rand() limit 1");
  ### The query above is guaranteed to return *one*and*only*one* row
  ### (unless the `user` table is empty, which you should check).
  if (mysql_num_rows($random) != 1) exit('No users defined!');
  ### Getting the value directly is more straightforward.
  $brs = mysql_result($random, 0);
  // echo $brs;
  ### delete the previous line and use return
  return $brs;
}

function user_id_isvalid($id) {
  $sql = "select count(*) from `user` where user_id=$id";
  $res = mysql_query($sql);
  if (!$res) {
    ### for simplicity sake we just exit with the error message
    exit('Error in query: ' . mysql_error());
  }
  return mysql_result($res, 0); ### returns 0 (false) or 1 (true)
}


require_once 'conn.php'; ### open database, connection will be used in functions
### if there's a problem opening conn.php the script will abort;
### it would continue with include

$referral = 0; ### initialize with an invalid user_id
if (isset($_GET['ref'])) {
  $referral = (int)$_GET['ref'];
  if ($referral < 1) $referral = 0; ### I assume there is no user_id = 0
  ### Now we have a numeric user_id ready to be tested
  if (!user_id_isvalid($referral)) {
    ### reset back to 0
    $referral = 0;
  }
}
if ($referral == 0) $referral = random_registered_user();
// echo "$referral";
### save $referral in a session variable
$_SESSION['referral'] = $referral;
?>



Happy Coding Smile
netpro
I have problem; if the userid is not number only, but letter include.
for example: mem123 or mem111
http://domain.com/?ref=mem123
After I check, the session can tracking userid with number only, not userid with number and letter (mem123 or mem111)
Confused ?????
hexkid
netpro wrote:
I have problem; if the userid is not number only, but letter include.
for example: mem123 or mem111
http://domain.com/?ref=mem123
After I check, the session can tracking userid with number only, not userid with number and letter (mem123 or mem111)
Confused ?????


That's what the script does. I (seemingly wrongly) assumed you'd want to track only numeric IDs. That's why I wrote
Code:
$referral = (int)$_GET['ref'];

If you want to track alphanumeric IDs you'll have to change the code.

There's a couple advantages of using numeric IDs only:
a) easier to check for validity before passing it on to SQL
b) no problems with magic_quotes or encodings.
netpro
Very Happy
Thanks again !
I have find the good answer
crazywolf
hello,

could you share the code for a simple ref system, without the random user applied.

user gets his ref link

refferral user registers on that link

the info goes to db.

thank you very much.
Related topics
Referals
PWN the internet!
Get more users / readers...
Needing People to Promote a Site for me
How do you get members?
New features being worked on
History of Frihost
Referral system rewards
Referral system
The new referral system
Woot! 989 emails deleted, and back on track!
Coins, domains, competitions and referrals in FAQ
Frihost number of posts ....
FORUM IS DEAD
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.