Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add restriction to a domain

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
}
?>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading