I build 2 website with a single codebase that have the same database and deployed in two different web sites.
I would like to restrict another domain 2 from accessing domain 1 and add function that if the domain is domain 2 replace it to domain 1, but i dont know what function to add
<?php
if($_SERVER['HTTP_REFERER'] !== 'abc.com'){
//add funtion to restrict domain 2
}
elseif($_SERVER['HTTP_REFERER'] == 'cde.com');{
// add function here to change domain 2 to domain 1
}
?>
CodePudding user response:
In PHP, you can restrict access to a specific domain by using the $_SERVER['HTTP_HOST']
variable to check the current domain and compare it to the allowed domain.
Here's an example of how you can restrict access to a specific domain, say "example.com":
if ($_SERVER['HTTP_HOST'] !== 'example.com') {
die('Unauthorized access');
}
This code checks the current domain against the string 'example.com' and if it does not match, the script sends a 403 Forbidden HTTP status code to the browser and exits the script.
You can also use preg_match() function to check for multiple domain names.
if (!preg_match("/^(www\.)?(example1|example2|example3)\.com$/i", $_SERVER['HTTP_HOST'])) {
die('Unauthorized access');
}