Trying to set persisent cookie based on original referer

[ad_1]

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]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer