I have the following code that takes the data from the URL itself, specifically the referrer code site.com/register/?ref=1234
.
<?php
session_start();
$url_referred = $_GET['ref'];
setcookie("referred", $url_referred);
//Reference time 86400 = 1 day && 60*60*24*90 = 90 days && 30 * 24 * 60 * 60 = 30 days
setcookie('referred', $url_referred, time() + (86400 * 30), '/');
$referred = isset($_COOKIE["referred"]) ? $_COOKIE["referred"] : '';
//setcookie("referred", "", time()-(86400 * 30));
?>
Now I can read the reference code either through echo $_COOKIE["referred"];
I have it defined in the following way to read it:
if($referred != ""){
echo $referred;
} else {
$referred = "0000";//Defaul for my site
}
The cookie
does not seem to work, when reopening the site twice, but this time if in the reference data the reference site.com/register/
code is no longer shown.
And I think I know why, firstly because the cookie
established one is easily replaced by another one in the previous case by an empty one or null data, because the one that happens to show is the default reference data, and if I do site.com/register/?ref=123
the following it shows me the following data 123
and If I do the following again but with a different reference site.com/register/?ref=123456
, it shows me the consequent again, 123456
eliminating the previous one.
So, how can I define certain parameters and conditions to a cookie
, if a user first arrives at my site without any referral link, the data to register in the cookie
will be the default value 0000
, and if he goes and returns with a referral link that this not be valid, because the user already knew my site for his own purposes and not for a reference link.
And if a user arrives by the following referral link site.com/register/?ref=123
that registers in the cookie
value 123
and yes they leave and come back with another referral link site.com/register/?ref=123456
that continues to show the value 123
unless the user has returned with that referral link after 30 days , there if the following value is registered and displayed 123456
.
How can I do it and have the following conditions in my cookie
?
soda
I think the flow would basically be:
referred
you skip the rest of the checks.$referred
is the value of the cookie.ref
in the query string,$referred
it is the value of$_GET['ref']
.$referred
is0000
$referred
took in the previous loop. This implies that if you visit weekly, your cookie is renewed for 30 days each time.The code is more or less this:
Beware, if the user cleans the cookies of his browser, it is as if he had never visited your site.
edited bonus track
(fixes syntax errors and displays informational messages)
To handle this case you could use the frontend and if it detects the cookie, store the value in the
localStorage
browser. The localStorage forces the cookie to stubbornly persist . In that case the flow would be:0000
.referred
in localStorageHowever , you will not be able to directly read the expiration date of the browser cookie. What you can do then is give the cookie the value
$referred
concatenated with the expiration date, preferably in the form of a timestamp. In this case, if the referrer is,123
you could assign the value123_<timestamp de expiración>
.Full working example
(I changed the separator
|
to an underscore_
because the pipe is rewritten to%7C
and that complicates everything)In this case the complete script would be:
And I think that completes the cycle. If you notice, I am declaring a variable
correct_value
that is the value of the cookie (only what comes before the underscore). If localStorage steps on the cookie, it also updatescorrect_value
.I pulled up that example at https://examples.ffflabs.com/setcookie.php
Some possible streams
Initial Flow you go to https://examples.ffflabs.com/setcookie.php?ref=1234
You enter for the first time. There is no value in
$_COOKIES
. It detects the ref 1234 and sets that value. The browser says:If you open Chrome Devtools, Application tab Storage section, you will see the value of the cookie ( you may have to press the refresh button if the cookie does not appear)
And the value in localStorage:
Second Flow : you go to https://examples.ffflabs.com/setcookie.php?ref=4321 (you are changing the ref)
The backend already has the cookie
1234
. Ignore the ref and the frontend says:If you examine Chrome Devtools the value of the cookie and localStorage have not changed. You persisted the cookie even though another ref was specified in the URL.
Third Flow: You delete the cookie but keep the value in localStorage. You navigate to https://examples.ffflabs.com/setcookie.php?ref=4321
The front end says:
You reload the page and you will see that thanks to localStorage you restored the referrer 1234.
Fourth Flow: You modify the value of localStorage to
2468_1540047515
Your cookie keeps saying
1234_1540047515
. You navigate to https://examples.ffflabs.com/setcookie.php?ref=4321The frontend answers you:
In the background, what's in localstorage (if it's current) rules over the cookie . If you reload, now the referrer is
2468
Fifth Flow: set localstorage to a different value and an expired timestamp
1357_1500047942
The frontend responds:
LocalStorage has been overwritten with the cookie value because it is expired and needs to be renewed:
Last Flow: you delete the cookie. You delete localStorage. You reload the page https://examples.ffflabs.com/setcookie.php?ref=1234
You get the same response as in the initial flow (although the timestamps have changed). If you check Chrome DevTools you will see that the cookie and localStorage stored what the backend just passed to them.
Closing Conclusions
When the entire stream ends, the variable
correct_value
always has the correct value of the cookie.So, if your original intention was to associate a user to a referrer in a semi-permanent way, then at the end of the flow you can send the data
correct_value
to another endpoint (egasociar_referrer.php
) through Ajax that persists the user and his referrer, for example in a database. .This persistence should allow insert if there is no value for that user, or update if it already exists . In MySQL that can be done using something like
If the userid is a primary key, it will insert the record if it doesn't exist, and replace it if it already exists.
The same in PostgreSQL has a similar syntax: