I want to add a referral system to my membership site that saves cookies.

Where the steps go like this:

Registration

Member info. entered in database, and referral link given out like mysite.com/ref.php?id=xxx

Page has sql/php coding which then adds +1 in the users referral row.

I found this code


<?php

include("connect.php"); //make sure we connect to the database first

$user = $_GET['user'];

if(!$user){

//echo out site normally

} else {

$sql = mysql_query("SELECT * FROM members WHERE user='$user' LIMIT 1");

$row = mysql_fetch_array($sql);

$referal = $row['users_referals']; //Get whats in that field

$referaladd = $referal +1; //I am not sure on this as ive not used math operators in php before, but basically your adding one onto whats in the db

$add = mysql_query("UPDATE members SET users_referals = '$referaladd' WHERE user = '$user'"); //Update the db with the users referals

echo "You were refered by $user";

//echo out rest of the site

}

?>

Does this help?

    I want to add a referral system to my membership site that saves cookies.

    I don't think that you need cookies here.

    Additionally you can clean up a lot of your code by doing some of the following::

    1, escape your $GET variable (this is probably the most important)
    http://php.net/manual/en/function.mysql-real-escape-string.php

    $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die(mysql_error());
    $user = $_GET['id'];
    $query = sprintf("SELECT * FROM users WHERE user='&#37;s' LIMIT 1",
                mysql_real_escape_string($user));
    

    2, you can use the mysql_num_rows to see if the user is in the database, then quickly update based off the result
    http://php.net/manual/en/function.mysql-num-rows.php

    $result = mysql_query($query,$link);
    if(mysql_num_rows($result) > 0){
    	$query = sprintf("UPDATE users SET referral=+1 WHERE user='%s' LIMIT 1",
                mysql_real_escape_string($user));
    	mysql_query($query,$link);
    	echo "You were refered by $user";
    }
    mysql_close($link);
    

      @ Thanks

      Two questions:

      1. In this case what will be the referral link?
        mysite.com/ref.php?id=xxx

      2. What is the simplest code to show users their referrals on the site

        1, the link is the entire thing:
        mysite.com/ref.php?id=xxx
        WHERE ref.php is the page && id=xxx is the query string.
        I don't think I understand you correctly

        2, It would appear that the way you are creating your "referral system" is only by a counter, as in your only recording the number of people someone has referred. If you want to create a true referral system, you are looking to do something a bit more involved. IE saving the person referred into the db, ensuring that they are linked up to the proper referral, making sure that there are no duplicates, etc.

          For the true referral system, I already have open source ready scripts but I'm not sure how to properly integrate that with the site. It need advanced knowledge.

            Did the open script not come with any documentation? If so you might check that first. Additionally you need to think about what your trying to accomplish, as in what are the boundaries and limitations, obstacles, and other things that need to be currently modified to be able to use this script.

            I'm not sure that I can help as I don't know much about the project and what your trying to accomplish...

              It comes with documentations but to install it as a standalone and not to be integrated. I f some one could share a PHP module to be incorporated in any site that would be nice.

              What do you think of installing the script separately for interested members that would sign in the referral system to get out of the modifications trap?

                in a referral system, the visitor is landing on a page, like: ref.php, where you need to pass the 'parent' ID...

                index.php?r=X

                in index.php, you can wait this $GET["r"] variable.

                if(isset($_GET["r"]))
                {
                // check here if there is a user with $_GET["r"]  id...
                //save the $_GET["r"] if it is available user into session or cookie ( session would be better!)
                $_SESSION["ref"]=intval($_GET["r"]);
                // here you can redirect the user to a registration page
                // header("Location: registration.php"); die();
                }
                

                Now you can modify your registration script to be able to handle referrals..

                $username= mysql_real_escape_string( $_POST["username"] );
                $pass= mysql_real_escape_string( $_POST["password"]);
                ...
                ...
                $ref=isset($_SESSION["ref"]) ? $_SESSION["ref"] : NULL;
                

                and then save this $ref into your users table, in a referral field.

                  Write a Reply...