PHP、会话状态、重定向“主页”基于推荐人的链接

发布于 2024-08-15 02:55:35 字数 1769 浏览 7 评论 0 原文

通常,网站有一个菜单栏,其中包含指向网站不同页面的链接 - 主页、页面 A、页面 B 等。

假设您有一个地址为 http://www.example.com。 99% 当有人在浏览器中输入该 URL 时,他们会被定向到“主页”代表的页面。它通常具有可选的索引扩展名[.html|.php](例如,http:// /www.example.com/index.html)。但是,在某些用例中,您希望将他们重定向到不同的页面,具体取决于用户将访问您的网站的位置。例如,如果您在宣传您的网页的网站上有广告,您可能希望将其重定向到:http://www.example.com/camefromad.html 看起来与普通的“主页”页面几乎相同,但可能会添加一些文本来表明它们来自广告所在的页面。

我的问题是:

有没有办法,在用户从引用页面登陆网站后,使用 PHP 来确保单击“主页”链接始终会转到 http://www.example.com/camefromad.html IF 用户登陆那里首先?我试图避免的是用户访问 http://www.example .com/camefromad.html,然后单击菜单栏中的“主页”,最终会进入默认主页,并且永远无法返回http://www.example.com/camefromad.html 页面(我没有该着陆页在公共菜单中启用,因为我不希望普通用户能够去那里)。

这是我第一次使用 PHP,我正在尝试使用会话状态等将其拼凑起来,但我还没有完全完成这个难题。这是我在 index.php 中的内容(这是代表“主页”链接的文件:

<?
session_start(); 
$_SESSION['amSet']= 0;
?>

<?
if ($_SESSION['amSet'] == 0) {
  if (!empty($_SERVER['HTTP_REFERER'])) {
    $referrer = $_SERVER['HTTP_REFERER'];
    $_SESSION['amSet'] = 1;
  }
}
if (preg_match("/other.example.net/",$referrer)) {
   header('Location: http://example.com/camefromad.html');
}
?>

基本上,我的想法是在用户访问网站时设置一个变量,以便引用者变量但如果用户点击网站本身的链接,我不希望引用者重置,所以如果用户点击。在“主页”链接上,它总是会将用户引导到适当的页面,显然,我上面的代码不起作用。

Commonly, a website has a menu bar with links to different pages of the site— Home, Page A, Page B, etc..

Let’s say you have a site with address http://www.example.com. 99% when someone types that URL into their browser, they are directed to the page represented by “Home”. It usually has the optional extension of index[.html|.php] (e.g., http://www.example.com/index.html). However, there are use cases where, depending from where a user is going to hit your website, that you want to redirect them to a different page. For example, if you have an ad on a site promoting your page, you might want to redirect them to: http://www.example.com/camefromad.html which will look pretty much the same as your normal “Home” page, but maybe with a little added text specifying that they came from the page where the ad was on.

My question is:

Is there a way, after a user has landed on the site from a referred page, to use PHP to ensure that clicking the “Home” link will always go to http://www.example.com/camefromad.html IF the user landed there to begin with? What I am trying to avoid is for a user to have come to http://www.example.com/camefromad.html and then click “Home” in the menu bar and end up at the default home page never to be able to return to the http://www.example.com/camefromad.html page (I don’t have that landing page enabled in the public menu since I don’t want the general user to be able to go there).

This is my first time using PHP and I am trying to piece it together using session state, etc., but I just haven't quite completed the puzzle yet. Here is what I have in index.php (which is the file that represents the "Home" link:

<?
session_start(); 
$_SESSION['amSet']= 0;
?>

<?
if ($_SESSION['amSet'] == 0) {
  if (!empty($_SERVER['HTTP_REFERER'])) {
    $referrer = $_SERVER['HTTP_REFERER'];
    $_SESSION['amSet'] = 1;
  }
}
if (preg_match("/other.example.net/",$referrer)) {
   header('Location: http://example.com/camefromad.html');
}
?>

Basically, my thinking is to set a variable when the user comes to the site so that the referrer variable is set to that referring site. But if the user clicks around links in the site itself, I don't want the referrer reset. I want it to stay what it was when the user first came to the site, so if the user clicks on the "Home" link it will always refer the user to the appropriate page. Obviously, the code I have above is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-08-22 02:55:35

你快到了! -- 只需将 $referrer 保存在 $_SESSION 变量中。然后您可以检查是否在后续页面请求中设置了 $_SESSION['referrer'] 并采取相应措施。

You're almost there! -- just save the $referrer in the $_SESSION variable. Then you can check if $_SESSION['referrer'] is set on subsequent page requests and act accordingly.

寻找我们的幸福 2024-08-22 02:55:35

为什么不将引用者存储在会话变量中?
在您的登陆页面中,您可以这样:

<?php
session_start();
if(empty($_SESSION['comeFrom']) && !empty($_SERVER['HTTP_REFERER'])){
    $_SESSION['comeFrom'] = $_SERVER['HTTP_REFERER'];
}
?>

然后,在您的主页中:

<?php
session_start(); 
if(!empty($_SESSION['comeFrom']) &&
   preg_match("/referrerSite.com/",$_SESSION['comeFrom'])){
   header('Location: http://www.example.com/camefromad.html');
}
?>

Why don't you store the referer in a session variable ?
In your landing page you can have this :

<?php
session_start();
if(empty($_SESSION['comeFrom']) && !empty($_SERVER['HTTP_REFERER'])){
    $_SESSION['comeFrom'] = $_SERVER['HTTP_REFERER'];
}
?>

Then, in your home page :

<?php
session_start(); 
if(!empty($_SESSION['comeFrom']) &&
   preg_match("/referrerSite.com/",$_SESSION['comeFrom'])){
   header('Location: http://www.example.com/camefromad.html');
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文