Shopping Cart Forum

Go Back   eShop Forums - eCommerce Help Forum for Shopping Cart Owners. > Tech Corner > PHP/HTML/CSS/JavaScript and Servers
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
PHP/HTML/CSS/JavaScript and Servers Pick up tips and exchange info on the technical side of having a web presence.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2007, 03:46 PM
PeterC PeterC is offline
Member
 
Join Date: Aug 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can I block an IP with php?

Hi

I've never posted on a forum before so here goes.

I have a mini (3 pages) custom built store but have a problem with someone abusing it, he/she keeps using different contact details but their IP is always the same. I've lost contact with the web designer so I can't ask him to do it for me.

The site is written in php, I'm quite good with html but I haven't a clue about php.

Can this IP be blocked with php and if so, how do I do it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-08-2007, 12:22 AM
Lawrence Lawrence is offline
Forum Admin
 
Join Date: Aug 2007
Location: Kenley, Surrey, UK
Posts: 1,226
Thanks: 2
Thanked 82 Times in 82 Posts
Default Re: Can I block an IP with php?

Hi Peter

This is the simplest way to block a single IP with PHP.

PHP Code:
<?php
$ip 
$_SERVER['REMOTE_ADDR'];
if (
$ip== "127.0.0.1") {
   exit();
}
?>
Change "127.0.0.1" to the offending IP. If you place this snippet at the very top of the page the unwanted user will be served a blank page.

You can also replace the "exit()" function with a page redirect to send your unwanted visitor to another location of your choice. To change the code to handle multiple IPs or even whole IP blocks e.g. 127.0.0.* requires a slight modification of the code, if you need this done let me know and I'll post the solution.

Note that if the top of your source code already begins with "<?php" then you can leave out the "<?php" & "?>" parts of the above code as these two lines designate the start and end of a PHP segment. As long as you place the rest of the code after the first "<?php" it will work.
__________________
Lawrence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-08-2007, 04:36 PM
PeterC PeterC is offline
Member
 
Join Date: Aug 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Can I block an IP with php?

Thank you very much, I tested it with my own ip and it worked a treat. I only need the one IP blocked but if I need more I may come back to you.

One more question, how can I make it display a message instead of a blank page?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-08-2007, 06:26 PM
Lawrence Lawrence is offline
Forum Admin
 
Join Date: Aug 2007
Location: Kenley, Surrey, UK
Posts: 1,226
Thanks: 2
Thanked 82 Times in 82 Posts
Default Re: Can I block an IP with php?

Hi Pete

You can add a message to the exit() function like this exit("Go Away");

If you want something more elaborate then you can do something like this.

PHP Code:
<?php
$ip 
$_SERVER['REMOTE_ADDR'];
if (
$ip== "127.0.0.1") {
  
?>
 
  <H1>Put some standard HTML here</H1>
 
  <?php
 
exit();
}
?>
Basically what this is doing is switching off PHP "mode" with the ?> so it reverts back to HTML, then when the the HTML is finished the <?php switches PHP back on again.

You can place an entire HTML page is that space.
__________________
Lawrence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-08-2007, 04:55 PM
PeterC PeterC is offline
Member
 
Join Date: Aug 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Can I block an IP with php?

Thanks a lot Lawrence, that's exactly what I need. Don't be surprised if I return with more questions soon though.

Cheers
Pete
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
php


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use .htaccess to block IPs and domains from your website. Lawrence PHP/HTML/CSS/JavaScript and Servers 1 04-07-2008 10:40 PM
How to use mod_rewrite to block bad bots with .htaccess Lawrence PHP/HTML/CSS/JavaScript and Servers 0 30-10-2007 04:25 AM

Google

Edible Graphics, Affordable E-Commerce, Web Shops & Custom Form Scripts
Click here for domain name registration and web hosting
tech news, product reviews, the latest home and business technology, the latest in digital imaging


Content Relevant URLs by vBSEO 3.0.0