跨多个页面存储 PHP 会话 [英] Storing PHP Session across multiple pages
问题描述
我已经设置了一个 PHP 会话来捕获传递到 URL 中的站点的 CAMPAIGN 和 CLICKID 变量 - 即 domain.com/index.php?&clickid=test1&campaign=test1.我使用下面的代码实现了这一点:
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'];
?>
然后我通过外部链接将其传递给第三方,例如 test.php?&clickid=&campaign=.
I then pass this out to a third party in an external link, for example test.php?&clickid=&campaign=.
然而,我似乎无法在整个域中共享此 SESSION.如果您登陆页面 A 并单击该链接,则此脚本有效,但是我想要做的是用户单击链接,访问页面 X 和页面 Y,返回页面 A 并且仍然存储变量.
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.
有人可以帮忙吗?
推荐答案
看来您正在覆盖变量 - 如果没有获取参数.
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'];
}
?>
这篇关于跨多个页面存储 PHP 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!