Here is the deal I want to accept visitors only from Google Search referrer to my site So,if they type "domain.com" in the url bar, Google Search of "domain.com" must come up. Can someone show me a Php code for this or something else? Thanks
Asked
Active
Viewed 73 times
-1
-
1Can you please explain it more to a next level? What exactly you are looking for? – Satish Saini Jan 31 '17 at 10:15
-
My visitors must come only from Google Search,not direct link. So when they type my domain in their browser, they must be redirect to the search results of my website. I think i should add some code in index.php but dont know what. I hope i was clear – Joti Jan 31 '17 at 10:19
-
Possible duplicate of [How can i use $\_SERVER\['HTTP\_REFERER'\] to find that user came from google?](http://stackoverflow.com/questions/10613025/how-can-i-use-serverhttp-referer-to-find-that-user-came-from-google) – Tony Hensler Jan 31 '17 at 10:21
-
@TonyHensler well very close but not exactly. I dont want to know if they came from google or not. I want my website to be accessible only from Google and not from a direct link. – Joti Jan 31 '17 at 10:26
-
Example : news-worldx.com – Joti Jan 31 '17 at 10:27
-
Then you would need to add an if statement in the header in redirect the accordingly on the result of the $_SERVER['HTTP_REFERER'] – Tony Hensler Jan 31 '17 at 10:32
-
check the referer header and if the use is not from google, redirect them to google search page result [URL](https://www.google.co.in/search?q=news-worldx.com). This can trigger google spam detection maybe. better to POST request to https://www.google.com/search – Sourav Ghosh Jan 31 '17 at 10:37
-
@SouravGhosh hmmm yes i thought it too about the spam. Thanks – Joti Jan 31 '17 at 10:43
1 Answers
0
You can always check $_SERVER['HTTP_REFERER']
and redirect them to https://www.google.co.in/search?q=yourwebsite.com if hey are not from google search page. But this is not efficient because this maybe can trigger Google spam detection. I am not completely sure about the spam detection, but I can suggest an alternative which is better in my opinion.
//say your website URL is example.com. Someone landed in example.com
$url = $_SERVER['HTTP_REFERER'];
$host = parse_url ($url, PHP_URL_HOST);
if (strstr ($host, 'www.google.')) {
//someone can still spoof this. If you need more restriction, you need to check all google regional domains explicitly.
//https://en.wikipedia.org/wiki/List_of_Google_domains
header('Location: example.com/welcome');
} else{
header('Location: example.com/redirect');
}
Now in redirect
page, you need to do the trick. You fill up a form automatically and submit it automatically using javascript.
<form id="myForm" action="https://www.google.com/search" method="get">
<input type="hidden" name="q" lang="en" value="example.com">
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>
Now the users will be taken to the google search result page and your website will be the first result of course.

Sourav Ghosh
- 1,964
- 4
- 33
- 43
-
-
I mistakenly posted form method as `post, ` but it should be `get`. I tested my second code. It is working fine – Sourav Ghosh Jan 31 '17 at 11:53