| | |
Using simple php redirect script but need to pull information from mysql instead
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Using simple php redirect script but need to pull information from mysql instead
0
#1 Nov 18th, 2008
I am using the following PHP redirect script for a non-profit website, but because the blocked IP list keeps getting longer and longer, I want to pull the IP information from a simple mysql database that lists only IP addresses that have been blocked. I have tried several different combinations but cannot get them to work. I have an _include.connection.php that works well. The database is maintained using an admin page so adding the IP addresses to the database is no problem. Thanks for any suggestions and help.
PHP Syntax (Toggle Plain Text)
<?php // the list of banned IPs, including partial IP addresses $iplist = array("82.128.","82.128.45.248","82.128.45.247","172.194.4.185","82.128.107.164"); $ip = getenv("REMOTE_ADDR"); // get the visitors IP address // echo "$ip"; $found = false; foreach ($iplist as $value) { // scan the list if (strpos($ip, $value) === 0){ $found = true; } } if ($found == true) { echo "top.location = \access_denied.php\";\n"; // page to divert to } ?>
Re: Using simple php redirect script but need to pull information from mysql instead
0
#2 Nov 18th, 2008
Well, actually if you use a database there is much faster way to do your job.
Your code should look something like this. Let's presume your db table is called 'banned' and your column with banned ip's is called ip.
I think that this should be OK for what you need to accomplish.
Your code should look something like this. Let's presume your db table is called 'banned' and your column with banned ip's is called ip.
PHP Syntax (Toggle Plain Text)
$db_connect = mysql_connect(host, user, pass); $db_select = mysql_select_db('banned_ips'); $current_ip = getenv("REMOTE_ADDR"); $select = 'SELECT banned.ip FROM banned WHERE ip like "'.$current_ip.'%"'; $run = mysql_query($select, $db_connect); $result = mysql_num_rows($run); if($result>0) { header('Location: access_denied.php'); } else {$found=false;}
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: Using simple php redirect script but need to pull information from mysql instead
0
#3 Nov 18th, 2008
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: Using simple php redirect script but need to pull information from mysql instead
0
#4 Nov 19th, 2008
Rhyan,
I used your php script and it worked fine with the mysql database when I included the full IP address such as 123.456.789.011 but when I tried to use a partial IP address such as 123.456. it did not block the IP. I tried it with both 123.456. (with dot) and 123.456 (without dot) because somewhere I remember that it required the last dot.
Any suggestions? Because I need to be able to ban a range of IPs also since some objectionable visitors are on dynamic ISP and their IP changes with every log on.
I used your php script and it worked fine with the mysql database when I included the full IP address such as 123.456.789.011 but when I tried to use a partial IP address such as 123.456. it did not block the IP. I tried it with both 123.456. (with dot) and 123.456 (without dot) because somewhere I remember that it required the last dot.
Any suggestions? Because I need to be able to ban a range of IPs also since some objectionable visitors are on dynamic ISP and their IP changes with every log on.
Re: Using simple php redirect script but need to pull information from mysql instead
0
#5 Nov 20th, 2008
Hmm...
I analyzed your original script. So i think if we change the SELECT string like this, it will work for you.
Try it and tell us the result.
I analyzed your original script. So i think if we change the SELECT string like this, it will work for you.
PHP Syntax (Toggle Plain Text)
$select = 'SELECT banned.ip FROM banned WHERE instr("'.$current_ip.'",ip)';
Try it and tell us the result.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: Using simple php redirect script but need to pull information from mysql instead
0
#6 Nov 20th, 2008
Re: Using simple php redirect script but need to pull information from mysql instead
0
#7 Nov 21st, 2008
•
•
•
•
I tried the script change and it will block a full IP but not a partial IP such as 172.190 or 172.190.
The original script blocked the partial IP but could not access the database and unfortunately I do not know how to fix it.
But if you still insist on usign your code, it should look something like this:
PHP Syntax (Toggle Plain Text)
<?php $result = mysql_query('select ip from banned.ip'); $count = mysql_num_rows($result); $iplist=array(); for($i=0;$i<$count;$i++) { $row=mysql_fetch_row($result); // the list of banned IPs, including partial IP addresses $iplist[$i] .= $row['0']; } $ip = getenv("REMOTE_ADDR"); // get the visitors IP address // echo "$ip"; $found = false; foreach ($iplist as $value) { // scan the list if (strpos($ip, $value) === 0){ $found = true; } } if ($found == true) { echo "top.location = \access_denied.php\";\n"; // page to divert to } ?>
Hope it works.
By the way, is this php script part of a javascript, because top.location is not php's function for sending headers?
Last edited by Rhyan; Nov 21st, 2008 at 3:22 am.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: Using simple php redirect script but need to pull information from mysql instead
0
#8 Nov 21st, 2008
Thanks again! Actually I prefer the simpler code and I do not know why it would not work when I tested it but I will try the simple code again. Perhaps it had to do with the way I included your script in my web pages using the JavaScript code below which was inserted just above the </head>...
or would it be better to use a php code such as
Again, thanks for your help and as I said, I would much prefer the most simple way to do it. Also it occurred to me that maybe I should include your code in the head of my web page instead and use it that way, but is that the most awkward way to do it?
Thanks, Donald
PHP Syntax (Toggle Plain Text)
<SCRIPT TYPE="text/javascript" LANGUAGE="javascript" SRC="_includes/ip_redirect.php"> </SCRIPT>
or would it be better to use a php code such as
PHP Syntax (Toggle Plain Text)
<?php include("_includes/ip_redirect.php"); ?>
Again, thanks for your help and as I said, I would much prefer the most simple way to do it. Also it occurred to me that maybe I should include your code in the head of my web page instead and use it that way, but is that the most awkward way to do it?
Thanks, Donald
![]() |
Other Threads in the PHP Forum
- Previous Thread: Email addresses showing as text. How to turn them into hyperlinks?
- Next Thread: php -> classes problem
| Thread Tools | Search this Thread |
ajax apache api array basics beginner binary broken cakephp checkbox class cms code codingproblem combobox cron curl database date display domain dynamic echo email error file files folder form format forms function functions google href htaccess html image include insert interactive ip java javascript joomla js limit link load login mail malfunctioning menu mlm mobile multiple mysql nodes oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion reference remote return script search server sessions sms source space sql syntax system table tutorial unset up-to-date update upload url validation validator variable video web webapplications websitecontactform youtube





