Wednesday, August 20, 2008

Authenticating users with IP Address - PHP

Authenticating users with IP address - PHP

PHP Code:
$accepted_ip = array ("127","0","0","1");
$remote = explode(".",$remote_addr);
//set $match to true
$match = 1;
//check if there is unmatched ip
for($i = 0; $i < sizeof($accept); $i++){
if ($remote[$i] != $accept[$i]){
//if not match, set $match to false
$match = 0;
//if $match true, then allow access, else denied access.
if($match) {
echo "Access Success!";
echo "Access Denied!";

1. $remote_addr - the remote IP address of client computer. IP address with means that that client computer is the same computer used as server computer which provide PHP services.
2. the ip address is located to an array to test whether it is same as the ip address stated there. eg. $accepted_ip