Using simple php redirect script but need to pull information from mysql instead

Reply

Join Date: Nov 2008
Posts: 5
Reputation: seacaptdon is an unknown quantity at this point 
Solved Threads: 0
seacaptdon seacaptdon is offline Offline
Newbie Poster

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.

  1. <?php
  2. // the list of banned IPs, including partial IP addresses
  3. $iplist = array("82.128.","82.128.45.248","82.128.45.247","172.194.4.185","82.128.107.164");
  4. $ip = getenv("REMOTE_ADDR"); // get the visitors IP address
  5. // echo "$ip";
  6. $found = false;
  7. foreach ($iplist as $value) { // scan the list
  8. if (strpos($ip, $value) === 0){
  9. $found = true;
  10. }
  11. }
  12.  
  13. if ($found == true) {
  14. echo "top.location = \access_denied.php\";\n"; // page to divert to
  15. }
  16.  
  17. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

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.

  1. $db_connect = mysql_connect(host, user, pass);
  2. $db_select = mysql_select_db('banned_ips');
  3. $current_ip = getenv("REMOTE_ADDR");
  4.  
  5. $select = 'SELECT banned.ip FROM banned WHERE ip like "'.$current_ip.'%"';
  6. $run = mysql_query($select, $db_connect);
  7. $result = mysql_num_rows($run);
  8.  
  9. if($result>0)
  10. {
  11. header('Location: access_denied.php');
  12. }
  13. else {$found=false;}
I think that this should be OK for what you need to accomplish.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: seacaptdon is an unknown quantity at this point 
Solved Threads: 0
seacaptdon seacaptdon is offline Offline
Newbie Poster

Re: Using simple php redirect script but need to pull information from mysql instead

 
0
  #3
Nov 18th, 2008
Thank you very much, I will try this change and let you know. I really appreciate your suggestion.
By the way, I love your city. I really enjoyed your country.
Last edited by seacaptdon; Nov 18th, 2008 at 8:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: seacaptdon is an unknown quantity at this point 
Solved Threads: 0
seacaptdon seacaptdon is offline Offline
Newbie Poster

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

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.

  1. $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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: seacaptdon is an unknown quantity at this point 
Solved Threads: 0
seacaptdon seacaptdon is offline Offline
Newbie Poster

Re: Using simple php redirect script but need to pull information from mysql instead

 
0
  #6
Nov 20th, 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: Using simple php redirect script but need to pull information from mysql instead

 
0
  #7
Nov 21st, 2008
Originally Posted by seacaptdon View Post
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.
This is really strange, because I have tested the code in my server and it works perfectly. Note that it works either with or without the second dot with no problems. Basically what I did was to use same method to match banned ips to current ip as, however using the built-in mysql functions, which is faster than retrieving the complete database into php and then applying search rules on these results.

But if you still insist on usign your code, it should look something like this:

  1. <?php
  2.  
  3. $result = mysql_query('select ip from banned.ip');
  4. $count = mysql_num_rows($result);
  5. $iplist=array();
  6. for($i=0;$i<$count;$i++)
  7. {
  8. $row=mysql_fetch_row($result);
  9. // the list of banned IPs, including partial IP addresses
  10. $iplist[$i] .= $row['0'];
  11. }
  12. $ip = getenv("REMOTE_ADDR"); // get the visitors IP address
  13. // echo "$ip";
  14. $found = false;
  15. foreach ($iplist as $value) { // scan the list
  16. if (strpos($ip, $value) === 0){
  17. $found = true;
  18. }
  19. }
  20.  
  21. if ($found == true) {
  22. echo "top.location = \access_denied.php\";\n"; // page to divert to
  23. }
  24.  
  25. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: seacaptdon is an unknown quantity at this point 
Solved Threads: 0
seacaptdon seacaptdon is offline Offline
Newbie Poster

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>...
  1. <SCRIPT TYPE="text/javascript" LANGUAGE="javascript" SRC="_includes/ip_redirect.php">
  2. </SCRIPT>

or would it be better to use a php code such as

  1. <?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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC