Some help with PHP

nisdee

Regular Member
Joined
Nov 16, 2014
Messages
381
Reaction score
151
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..








 
Just use "GET" parameters of the url and use query to increment/update the row corresponding to the id in the DB.
 
Thread moved to the PHP & Perl section.
 
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.
 
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
 
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! :)
 
Hi,
verified if the request GET['id'] is a number !! preg_match, intval or whatever u need

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:

Strings will most likely return 0 although this depends on the leftmost characters of the string
 
Back
Top