Php - Is it possible to block access to a webpage depending on the address used?

StackOverflow https://stackoverflow.com/questions/21592286

  •  07-10-2022
  •  | 
  •  

문제

Consider the following scenario.

There is a site on a local network which can be accessed one of 2 ways:

  1. 11.11.11.111/testsite (ip)
  2. test.site.com (vhost)

Is it possible with php to only allow access using test.site.com?

EDIT:

While there were many good suggestions as to how to solve this problem, I went with Publi Design & John V.'s mod_rewrite suggestion, but I implemented it in the httpd.conf file and redirected to a forbidden page.

What worked for me was:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^test.site.com$ [NC]
RewriteRule .? - [F]
도움이 되었습니까?

해결책

I would avoid the PHP request and go up one level. I haven't tried this, but maybe the .htaccess file could help you, using something similar to this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^11.11.11.111/testsite$ [NC]
RewriteRule ^(.*)$ test.site.com/$1 [L,R=301]

You might have to play around with it, but I would imagine something along those lines should get you to where you want to be.

다른 팁

As Michael P suggested in his comment, it's best to do this using a server configuration file, rather than with PHP. But it is possible with PHP, and rather easy. All you need is a simple if/else statement using PHP's global variable $_SERVER. Something like this should do.

<?php
// put this at the top of every page that will be accessed
if ($_SERVER['HTTP_HOST'] != 'test.test.com') { 'Please visit this site using the correct link: http://test.test.com'; exit; }
?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top