how to block someone from visiting my page using ip adress ?
this is the case
if someone come to my site then we record the ip address and put it into database.
when the visitor come again with the same IP they cannot see my page (block them) and redirect it to somewhere page.
Block IP with Database
Moderator: General Moderators
Re: Block IP with Database
If you've already got a database of blocked IPs, just query the database for a match on their $_SERVER['REMOTE_ADDRESS'] and exit if you find a match.
Re: Block IP with Database
can you give me a code ?Celauran wrote:If you've already got a database of blocked IPs, just query the database for a match on their $_SERVER['REMOTE_ADDRESS'] and exit if you find a match.
i dont have the database but i want to make it first so every visitor come to my site cannot see my site for twice time with the same IP
-
mikeashfield
- Forum Contributor
- Posts: 159
- Joined: Sat Oct 22, 2011 10:50 am
Re: Block IP with Database
You'll be better using a cookie.
When the user first accesses the site, you store the cookie, when they come back, you check for the presence of that cookie, if it's there, you exit. Using a database of IP addresses would be impractical. IP addresses are typically dynamically assigned to end users, so it's entirely possible that you could inadvertently block access to your site to users that haven't used your site yet.
Mike.
When the user first accesses the site, you store the cookie, when they come back, you check for the presence of that cookie, if it's there, you exit. Using a database of IP addresses would be impractical. IP addresses are typically dynamically assigned to end users, so it's entirely possible that you could inadvertently block access to your site to users that haven't used your site yet.
Mike.
Re: Block IP with Database
The problem with using cookies is that they can easily be deleted by the user, rendering the whole exercise pointless. While you're right that most users will have dynamic IP addresses, most broadband providers -- as far as I know, at any rate -- provide one IP to one client for months at a time.
Re: Block IP with Database
First time script will show your data or other action, just put in { } when other (2.,3.,4., times) go to page will be automaticly redirected.
Code: Select all
$ip = $_SERVER['REMOTE_ADDR'];
if(!file_exists("$ip.txt"))
{
echo "YOUR first DATA SHOW";
}
else
{
echo '<script language="javascript">location.replace("other.php");</script>';
}
$upis_otvaranje=fopen("$ip.txt","w+");
fwrite($upis_otvaranje,"");
fclose($upis_otvaranje);Re: Block IP with Database
already posted this code once, but it seems relevant, so:
This will get a user's IP even if they're behind a proxy. Works so far, not sure about multiple proxies.
and query the database:
This will get a user's IP even if they're behind a proxy. Works so far, not sure about multiple proxies.
Code: Select all
$real_ip = $_SERVER['REMOTE_ADDR'];
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
$real_ip = $ip;
}
}
}
}
Code: Select all
$result = mysql_query("SELECT * FROM `ips` WHERE `ip`='".$real_ip."';");
if(!$result){
echo(mysql_error());
exit();
}
if(mysql_num_rows($result) == 0){
echo("OK");
}else{
echo("Your IP has been blocked");
}