Grant
Grant

Reputation: 9

Storing PHP Session across multiple pages

I have setup a PHP session to capture the CAMPAIGN and CLICKID variables that are passed to the site within a URL - i.e. domain.com/index.php?&clickid=test1&campaign=test1. I am achieving this using he code below :

<?php
session_start();
$_SESSION["campaign"] = $_GET['campaign'];
$_SESSION["clickid"] = $_GET['clickid'];
?>

I then pass this out to a third party in an external link, for example test.php?&clickid=&campaign=.

However what l cannot seem to do is share this SESSION across the domain. This script works if you land on page A and click the link, however what l want to be able to do is the user to click the link, visit page X and page Y, return to the page A and the variables still be stored.

Can anyone help ?

Upvotes: 0

Views: 7389

Answers (2)

1234567
1234567

Reputation: 956

Keep in mind that a $_SESSION is designed to allow you to set a variable once, and access it on another page. I'm guessing that you have the same content on both pages, X and Y.

If you page you are setting the variables on looks like:

<?php
session_start();
$_SESSION["campaign"] = $_GET['campaign'];
$_SESSION["clickid"] = $_GET['clickid'];
?>

With this in mind, all you need to access your variables that you set on your index.php, is the following:

<?php
session_start();
// Don't rewrite your variables by setting them again, they're already set!
echo $_SESSION['campaign'];
echo $_SESSION['clickid'];
?>

I think your mistake was incorrectly assuming that removing the values from the URL (test.php?&clickid=&campaign=), would not reset the $_SESSION variables. $_GET['clickid'] and $_GET['campaign'] are still set!

Upvotes: 0

Steve
Steve

Reputation: 20469

It appears you are overwritting your variables - if there are no get parameters.

You should only write to session if the parameters exist:

<?php
session_start();

if(isset($_GET['campaign'])){
    $_SESSION["campaign"] = $_GET['campaign'];
}
if(isset($_GET['clickid'])){
    $_SESSION["clickid"] = $_GET['clickid'];
}


?>

Upvotes: 1

Related Questions