SoFunction
Updated on 2025-04-08

PHP implementation code to restrict domain name access (local verification)

What if you don’t want to be directly used by others if you write a source code in PHP? The first thing that comes to mind is encryption, but now, except for Zend 5 encryption, it is still difficult to crack, other encryption methods are vulnerable. Even if you don't crack it, people with bad intentions can use your source code to build a website exactly like you. Wouldn't this make your labor fruits be taken away by others for yourself? The following describes a method that restricts domain names to protect your source code from being copied and run directly.

How to restrict domain names through programs to protect source code? For example, what you want to use in the future is that if the PHP program is lost, even if other people use your PHP source code, if the domain name is judged incorrect, it will not run. At the same time, the source code will be encrypted, so that it will be foolproof.

The program source code of PHP program restricts domain names is as follows:

Let's share the implementation code below

1. Restricted domain name access method one

<?php 
if(!in_array($_SERVER['HTTP_HOST'],array('',''))){
	exit('Please contact us for website building:');
} 
?> 

2. Restricted domain name access method two

function allow_domain(){
	$is_allow=false;
	$servername=trim($_SERVER['SERVER_NAME']);
	$Array=array("localhost","127.0.0.1","","");
	foreach($Array as $value){
		$value=trim($value);
		$domain=explode($value,$servername);
		if(count($domain)>1){
			$is_allow=true;
			break;
		}
	}
	if(!$is_allow){
		die("<center> is only available for local use! Please contact us if you need domain name authorization");
	}
}
allow_domain();

Then use zend to encrypt, other encryptions are easily cracked.

Two ways to implement domain name authorization in PHP

01. Methods to verify domain name authorization online:

Client code:

&lt;?php
//Get the domain name prefix without port number$servername = trim($_SERVER['SERVER_NAME']);
//Get the server authorization file verification$verifyurl = file_get_contents('///zb_users/upload/?domain='.$servername);
if(!empty($verifyurl)){
	echo "Authorized!"; //Authorization is successful}else{
	die("Unauthorized!"); //Authorization failed}
?&gt;

Server code:

&lt;?php
//Get the domain name$domain = $_GET['domain'];
//Authorized domain name list$Array = array('127.0.0.1','localhost');
//Calibration resultsecho in_array($domain, $Array) ? 'yes' : '';
?&gt;

Domain name authorization code can be encapsulated into functions or encrypted. For commonly used PHP encryption forms, there are methods to crack, such as ZendGuard, ionCube, etc. If there are many authorized domain names, you can add domain name fields to the project, write the domain name to the database and then read and verify. We have published this method as an independent plug-in. For details, see: ZBlogPHP domain name authorization plug-in-AllowURL. Through the plug-in, you can add domain name and other information to the database for verification.

02. Methods to independently verify domain name authorization:

&lt;?php
function allow_domain(){
	$is_allow=false;
	//Get the domain name prefix without port number	$servername=trim($_SERVER['SERVER_NAME']);
	//Authorized domain name list	$Array=array("localhost","127.0.0.1");
	//Transfer the array	foreach($Array as $value){
		$value=trim($value);
		$domain=explode($value,$servername);
		if(count($domain)&gt;1){
			$is_allow=true;
			break;
		}
	}
	if(!$is_allow){
		die("Domain name is not authorized!"); //Authorization failed	}else{
		echo "Domain name is authorized!"; //Authorization is successful	}
}
allow_domain();
?&gt;

The purpose of domain name authorization is to protect intellectual property rights, encourage developers to publish more excellent works, and promote the cultural development and technological progress of the entire online society, which is of great significance.

This is the article about PHP implementation code (local verification) that restricts domain name access. For more related content related to PHP restricts domain name access, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!