1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Some help with PHP

Discussion in 'PHP & Perl' started by nisdee, Jan 28, 2015.

  1. nisdee

    nisdee Regular Member

    Joined:
    Nov 16, 2014
    Messages:
    354
    Likes Received:
    122
    Location:
    Rainbow Land
    Hey guys, I've been trying to write a refferal system php script but I can't get it to work

    So basically this is what I need:

    1. User comes to my website-gets his ref link in a form(window, whatever..)
    example. domain.com/refer.php?ID (id is a number)

    2. When someone clicks the link, the guy get's +1 on counter

    I think it should track cookies or something, I've been trying to do this for a week but I'm green to php so help..








     
  2. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,115
    Likes Received:
    1,725
    Location:
    Banana Republic
    Just use "GET" parameters of the url and use query to increment/update the row corresponding to the id in the DB.
     
  3. WizGizmo

    WizGizmo Super Moderator Staff Member Moderator Jr. VIP Premium Member

    Joined:
    Mar 28, 2008
    Messages:
    4,120
    Likes Received:
    55,854
    Thread moved to the PHP & Perl section.
     
  4. vayacondios

    vayacondios Newbie

    Joined:
    Mar 3, 2015
    Messages:
    30
    Likes Received:
    5
    Depends upon how deep you want to go with this, but here is one stab at it:

    #assume you are using mysql, here is connection object
    $mysqli = new mysqli("localhost", "USER", "PASSWORD", "DB_NAME");

    #if the ID exists, increment in the db
    if (isset($_GET['ID'])) {
    $sql = "UPDATE [TABLENAME] SET [COUNTCOLUMN] = [COUNTCOLUMN] + 1 WHERE [IDCOLUMN] = " . $_GET['ID'];
    $mysqli->query($sql);
    }
    #good practice to close object when done
    $mysqli->close();

    If you want to go further and ensure that referrals only get counted once, then that is where you can use cookies (let me know and I will add that code) and only increment on new referrals.
     
  5. ramiro2016

    ramiro2016 Newbie

    Joined:
    Mar 12, 2015
    Messages:
    1
    Likes Received:
    0
    Hi,
    if you are scripting a registering referral system !
    you need to check if the refereed user are registered before you ADD counts
    but if u just wanna to count how many visit to specified URL
    you can just do what they are already told you and verified if the request GET['id'] is a number !! preg_match, intval or whatever u need

    sorry 4 my bad english :D
     
  6. Darkhodge

    Darkhodge Newbie

    Joined:
    Feb 27, 2015
    Messages:
    15
    Likes Received:
    3
    Occupation:
    Web Developer
    Location:
    Surrey, UK
    Just a quickie, but be careful of "SQL Injections" if you're going to do that. You can use stuff like mysqli_real_escape_string() or make use of "Prepared Statements". Definitely worth looking into! :)
     
  7. Stablebox

    Stablebox Registered Member

    Joined:
    Dec 9, 2014
    Messages:
    70
    Likes Received:
    10
    Occupation:
    Web Hosting
    Location:
    Miami, Florida
    Home Page:
    Should be using filter_var() with FILTER_VALIDATE_INT, probably. Regexes can get overly complicated, not to mention the processing overhead, and intval is a bad choice because it can have unintended consequences - from the PHP docs: