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
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.