PHP User Authentication by IP Address

In this article, you will learn how to authenticate a user by IP address using PHP programming language.

If you want to restrict the website or page to certain IP addresses, it might be on the grounds that you have an inner system that has a freely perceptible webpage. You may wish to have certain pages visible only to a specific machine on the inner system, or you may want to restrict the page to some static IP address. You can easily do it by reading this article.

This process works by determining the IP address of the user. Suppose you want to allow the website access to IP addresses in the range of to In the given example, we have defined an array of the allowed IP addresses that can view the page. For this, we have taken three array elements. The array("123", "121", "0") limits the users who can view the page to those with an IP address in the range of to The current user's IP address is taken by using a global variable '$_SERVER' and stored in a variable '$REMOTE_ADDR'.

	$allowed_ip = array("123", "121", "0");
	$remote_ip = explode(".", $REMOTE_ADDR);
	$permitted = 1;
	for($i = 0; $i < sizeof($allowed_ip); $i++) {
		if($remote_ip[$i] != $allowed_ip[$i]) {
			$permitted = 0;
	if($permitted == 1) {
		echo 'Welcome, You are authorized to access this page.';
		echo 'Access Denied';

In the above example, replace the $allowed_ip array values with your allowed IP address. If you want to restrict the page to only one IP address, then pass all four values of IP address as four elements in the $allowed_ip array.

Related Articles

PHP reverse a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP get IP address of visitor
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
JavaScript display PDF in the browser using Ajax call
jQuery loop over JSON result after AJAX Success
PHP review and rating script
jQuery File upload progress bar with file size validation
Print section of page using javascript
Submit a form data without page refresh using PHP, Ajax and Javascript
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP

Read more articles

General Knowledge

Learn Popular Language