Este sitio web utiliza cookies para realizar análisis y mediciones de tus visitas. [ Acepto ] [ Más información aquí ].

How to Create a Cookie

Creating a cookie is very easy. Here, I'll teach you how to create cookies both with PHP and Javascript.

How to create a cookie with PHP

This code will allow you to create a cookie using PHP code, and from this, you can create even more advanced cookies. You must introduce this code before your webpage produces any output.

$expiration= 60 * 60 * 24 * 30 + time();

if($_COOKIE["cookie"] == NULL){

setcookie("cookie", "chocolate cream", $expiration);

}

We just created a cookie, actually called “cookie”, and we've filled it with “chocolate cream”. You can change the filling according to the visitor. $expiration stores the expiring date of the cookie, the exact date when it will disappear from the user's browser. Some formulas to calculate time:

$day = 60 * 60 * 24 + time(); // after a day

$month = 60 * 60 * 24 * 30 + time(); // after a month

$year = 60 * 60 * 24 * 365 + time(); // after a year

How to create a cookie with Javascript

This code allows you to create a cookie by using Javascript. It's useful if you cannot use PHP. You only have to replace the cookie's name (yummycookie), its content (chocolate), its maximum age (currently, one month) and the domain.

<script text='javascript'>

document.cookie =

'yummycookie=' + encodeURIComponent('chocolate') +

'; max-age=' + 60*60*24*30 +

'; path=/; domain=yourdomain.com' ;

</script>

How to know if your cookie works?

Install ViewCookies plugin for Firefox, and then confirm the cookie appears.

What kind of information can be stored inside a cookie?

According to the European legislation, personal data can't be stored inside a cookie. However, I'll give you some pretty useful ideas:

Store the date of the first visit. By keeping the first time a user visited your website, you can customize content for older users. For example, a user that has visited your website one year ago could see special promotions:

setcookie("lastVisit", date("G:i - m/d/y"), $expiration);

Store the sum of total visits. If you know coding, you can use a counter to calculate the number of times a user returns to a particular webpage. This is an approximation to what airlines do, so they can show a higher price to people who have visited a same flight many times. This is also useful for doing aggressive offers or discounts, directed towards users who repeatedly visit a same webpage.

$expiration = 60 * 60 * 24 * 30 + time();

if($_COOKIE["visits"] == NULL){

setcookie("visits", 1, $expiration);

} else {

$suma = $_COOKIE["visitas"] + 1;

setcookie("visits", $sum, $expiration);

}

if ($_COOKIE["visits"] > 10) {

echo "You have a discount coupon of 25%!";

}

Customize content by age, sex and profession. It might sound absurd, but it's possible. If you're running campaigns in the Google Display Network, you can target a campaign only to females, and other to males. You can assign a particular URL for each campaign:

https://www.danielpinero.com/?gender=male

https://www.danielpinero.com/?gender=female

Next, this code will allow you to store the sex inside the cookie:

if($_COOKIE["gender"] == NULL && $_GET["gender"] != NULL){

setcookie("gender", $_GET["gender"], $expiration);

}

if($_COOKIE["gender"] == "female" || $_GET["gender"] == "female") {

echo "Now the site will change to pink!";

}

In Linkedin Ads, you can segment by age, sex, profession, education, seniority, residential zone, etc. In Facebook Ads, besides all that has been mentioned before, you can segment even by civil status and ethnicity. For each variable, you can create a new cookie to show custom content each time the user returns. Differently from the PHP session, the cookie doesn't expire immediately.

Store variables from ValueTrack. Using ValueTrack de Adwords, you can store very specific variables such as match type, network, device, creativity, keyword, product ID, and other details.

Keep referral. This is an alternative to Google Analytic's “nooverride”, which actually overrides old values with organic search traffic coming from Google. This way, you'll keep the real source of the user's first visit.

if($_COOKIE['php_referral'] == NULL) {

setcookie('php_referral', $_SERVER['HTTP_REFERER'], $expiration);

}

Store IP address. By adding code from my previous article, "How to see a Visitor's IP in Google Analytics", you can also store IP address.

Transfer cookies to third-party websites. For example, sending the content of cookies to an affiliate website. You can tag links by adding a new parameter, so they can retrieve it with $_GET, Google Analytics, or their own affiliate program. For example:

$url = "http://www.marketinet.com/?gender=".$_COOKIE["gender"];

echo $url;

This code will produce the following link, which can be retrieved by the third-party website, so they will know the visitor is a woman:

http://www.marketinet.com/?gender=female

Legality of cookies

Recently, the European Union has created a directive which forces webmasters to alert their visitors about the use of cookies in their websites. In my post "How to Create a Legal Advice for Cookies", you'll find how to comply with this directive.

Añadir comentario