| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Account Closed | PHP IP Ban/Redirect. Can you help with my script? Im in need of a little help with my php script if anyone has a bit of time. Simply put what i'm attempting to do is have the php script look at every person's IP as they come to my page. If their IP is in the banned_ip list i'm wanting to redirect that person to the first URL ( http://www.yahoo.com ). If their IP is not in the list I want to redirect these people to the second URL in the script ( http://www.google.com ). It's basically a php IP ban/redirect script. I have tried it on my host the way it is written and added my own IP to the banned list and it kept sending me to the 2nd URL when it should have sent me to the 1st. I'm lost. I'm a complete PHP newb guys so please keep the laughing and rude comments to a minimum, lol. Anyways i'd appreciate any and all help I can get. Below is my code. Code: <?php
$banned_ip = array();
$banned_ip[] = '204.96.148.3';
$banned_ip[] = '205.162.217.141';
$banned_ip[] = '209.62.82.14';
$banned_ip[] = '24.7.101.103';
$banned_ip[] = '65.31.130.2';
$banned_ip[] = '68.168.78.226';
$banned_ip[] = '68.205.75.110';
$banned_ip[] = '69.36.158.35';
$banned_ip[] = '70.173.198.69';
$banned_ip[] = '70.97.122.18';
$banned_ip[] = '71.189.157.53';
$banned_ip[] = '71.230.54.244';
$banned_ip[] = '74.52.245.146';
$banned_ip[] = '76.90.3.205';
$banned_ip[] = '24.20.251.76';
$banned_ip[] = '68.83.254.243';
$banned_ip[] = '69.2.27.10';
foreach($banned_ip as $banned) {
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == $banned){
header("location: http://www.yahoo.com");
}
else
{
header("location: http://www.google.com");
exit();
}
}
?>
|
| |
| | #2 (permalink) |
| Senior Member | PHP Code: That should work
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #4 (permalink) | |
| i love automation | Quote:
PHP Code:
__________________ Xrvel | Various online tools | Free Proxy List | Hidden Proxy Partai Golkar | Noomle works : Las Vegas Airfare | |
| |
| | #6 (permalink) |
| Senior Member | In lieu of the for look, I would use an in_array() check. Jason
__________________ Web Development |
| |
| | #8 (permalink) |
| Senior Member | yes it would. However as it is a built in function written in c it would perform faster than initially doing a for loop yourself. Although it will double the work if you then go ahead and carry out a for loop.
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #10 (permalink) |
| NamePros Regular | You could always make a table with 8 cols (4 octets x 2) then you could do all of the work in a single sql statement which would process ranges too. Add another column for a custom redirect would be a fun touch. Imagine what some banned ppl would think if they got redirected to the DOJ site, or 2girls1cup, or got rickrolled *evil grin* |
| |
| | #11 (permalink) |
| Senior Member | I vote for the rickroll
__________________ Web Development |
| |
| | #12 (permalink) |
| NamePros Regular | OK, this script should work for any range in any quartile of the IP. Even [0-255].[0-255].[0-255].[0-255] will work. With every $ip_range value you add, it will only get a very small fraction of a second slower. Running the script with 100 ip ranges took 0.00264000892639 seconds. Have Fun! PHP Code: |
| |
| | #14 (permalink) |
| New Member | Derek "Palyriot"... You seem like a real GURU at PHP... I'm also doing something similar I was going through your code and it works great.... I modified it a bit to make it suitable for what i need it to do... but what I need to do is to block out all IP ranges and exclude a few. Is it possible to exclude some IPs or IP ranges ? |
| |
| | #15 (permalink) |
| New Member | Hey guys... what I did to block out all IP's except these range and this ip ... I'm a bit of a noob as well at this but, if would be nice if someone could just have a quick look to see if it is right... I just need ip range of 150.203.197.*** to work, and 136.153.2.2 ... And reject all other ips. thanks in advanced... <?php $valid_ips = array('150.203.197..*','136.153.2.2'); if (!in_array($_SERVER['REMOTE_ADDR'],$valid_ips)) { echo "Access denied. Please go return to menu screen you must login in with your barcode and password before you can access this page."; //header('Location: /index.php'); //change according to your site if needed exit(); } ?> |
| |
| | #16 (permalink) |
| NamePros Regular | This should do it ![]() PHP Code: |
| |
| | #19 (permalink) |
| New Member | Awesome.... dude... Alright ... I've added the ip_ranges array Code: $ip_ranges[] = '[0-255].[0-255].[0-255].[0-255]'; In valid_ip array I added Code: $valid_ip[] = '136.153.2.2'; I've tried to play around with the figures in the ip_ranges array to skip this range '150.203.197.*' but I couldn't get it to work.... I must be doing something wrong I think.... I've playing around with you code at all morning to try to rejig it a bit but, I still be unsucessful. hmm.... |
| |
| | #20 (permalink) |
| New Member | I'm surprised that no one mentioned .htaccess mod_rewrite and mod_access, this apache modules works faster and better, was made for this kind of tasks and enabled everywhere by default. If you need dynamic IP lists - you can simply remove and add IPs to .htaccess file (manually or via php).
__________________ ;) |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |