I have a PHP code. Now, I'm building a WordPress site, instead of a HTML/PHP website. And, I've posted a question on the WordPress forum, someone said:
You cannot set cookies with PHP in HTML head sections. Cookies must be set before any output is sent out to the browser. Adding your code to a callback hooked to “init” should be OK. Alternately, set the cookie with JavaScript.
But, I don't have a very good programming experience with JavaScript. This is my code:
<?php
//DETECT REFERRER
if(empty($_SESSION["referrer"]))
{
$referrer = $_SERVER["HTTP_REFERER"];
if(empty($referrer))
$referrer = $_SERVER["SCRIPT_URI"].((!empty($_SERVER["QUERY_STRING"]))? '?'.$_SERVER["QUERY_STRING"]:'');
$_SESSION["referrer"] = $referrer;
}
//DETECT AFFILIATE ID
if(empty($_SESSION["aid"]))
{
$aid = $_GET["aid"];
if(empty($aid))
$aid = $_COOKIE["aid"];
if(empty($aid))
$aid = 1;
setcookie("aid", $aid, strtotime("+10 years"), "/");
$_SESSION["aid"] = $aid;
}
\?>
Does someone have any JavaScript experience and can help me with converting this code from PHP to JavaScript? Thanks in advance.
If you have any question, please post a comment.
If someone knows how to insert that code before the html head on every page, that's even better. But, I don't know if it is actually possible.
Regards, Luuk Faasse