I am trying to set a persistent cookie based on the original referer that a user came from. I have got the following code working by adding it to header.php:
session_start();
if ( !isset( $_SESSION[“originalReferer”] ) )
$_SESSION[“originalReferer”] = $_SERVER[“HTTP_REFERER”];
This works, and I can echo the value of the session cookie using this:
echo ‘You came from ‘ . htmlspecialchars($_SESSION[‘originalReferer’]) . ‘!’;
That shows up nicely, and no matter how many pages I click on after landing on my website I still retain the *original* referer URL. However, I need to get this value pushed into Gravity Forms, and for that I need it to be saved as a persistent cookie. The problem is, whenever I modify the above code it breaks, and instead of saving the original referer it saves a new referer each time I click through to a new page.
This is what I tried previously:
if ($\_SERVER\[‘HTTP\_REFERER’\] && !isset ($\_COOKIE\[‘originalReferer’\])){
setcookie(‘originalReferer’, $_SERVER[‘HTTP_REFERER’], time()+3600);
Can anyone help? Is there a better way to do this?
[ad_2]