Thursday, May 12, 2016

PHP Tutorial 4

Getting visitors IP Address
<?php
// Getting visitors IP Address
$ip_address = $_SERVER['REMOTE_ADDR'];
$ipblocked = array('127.0.0.1', '100.100.100.100');
foreach($ipblocked as $ip){
if($ip == $ip_address){
die('Your IP address '. $ip_address. ' has been blocked' );
}
}
?>
 Better way to get visitors IP
<?php
// Better way to get visitors IP
$http_ip = $_SERVER['HTTP_CLIENT_IP'];
$httpxforwardedfor=$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote_addr = $_SERVER['REMOTE_ADDR'];

if(!empty($http_ip)){
$ip = $http_ip;
} else if(!empty($httpxforwardedfor)){
$ip = $http_ip;
} else {
$ip = $remote_addr;
}
echo $ip;
?>
Detect Browser
<?php
// DETECT BRowser
$browser = get_browser(null, true);
// echo $browser['browser'];
$browser = strtolower($browser['browser']);

if($browser !='chrome'){
echo 'You\'re not using Google Chrome. Please do!';
}
?>



No comments:

Post a Comment