简体   繁体   English

重定向到主页(如果在Google中单击)

[英]redirect to homepage if clicked in google

I've read a several ways to redirect users to homepage, but, what if I need to redirect them just if they got to that page through a search engine like Google or Bing. 我已经阅读了几种将用户重定向到首页的方法,但是,如果我需要通过用户如Google或Bing的搜索引擎将其重定向到该页面,该怎么办?

I need this because I'm building a cigar webpage and in some countries is demanded to be a certain age to view the content. 我需要这样做是因为我正在建立雪茄网页,并且在某些国家/地区要求年龄一定,才能查看内容。

You could set a cookie or session through PHP/JavaScript on the first website visit, and check on all upcoming visits if the cookie already exists. 您可以在首次访问网站时通过PHP / JavaScript设置cookie或会话,并检查所有后续访问(如果cookie已经存在)。 If not, redirect them to the homepage. 如果不是,请将其重定向到主页。

In PHP you can use the function: 在PHP中,您可以使用以下功能:

$_SERVER['HTTP_REFERER'];

I believe this will also retrieve the full URL you was referred from. 我相信这还将检索您引用的完整URL。

See: http://php.net/manual/en/reserved.variables.server.php 请参阅: http//php.net/manual/en/reserved.variables.server.php


Ignore the above answer as I read the question wrong. 当我看错问题时,请忽略以上答案。 Best thing to do would be store within a session. 最好的办法是在一个会话中进行存储。 If a session key exists then do not redirect, if it doesn't then do a redirect. 如果存在会话密钥,则不要重定向,如果不存在,则不要重定向。

<?php
session_start();
$_SESSION['existingUser'] = true;

print_r($_SESSION);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM