Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2007, 04:00 PM
lambdacerro lambdacerro is offline
 
Join Date: Feb 2006
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Country Restriction Script

Hello,

I've coded a PHP script that restricts the access in my forum to some countrys, it works with an IP to country database and a cookie so it checks every half an hour.

I want to know where is the best place to insert it, I think it could be the same place where vbulltin checks the IP bans, so where does vbulletin check the IP Bans?

Thank you.
Reply With Quote
  #2  
Old 12-16-2007, 05:07 PM
iogames's Avatar
iogames iogames is offline
 
Join Date: Jan 2007
Location: Las Vegas, NV.
Posts: 1,433
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just to remind you, that like every cluster of society, there's just a few bad apples... I don't understand why do you want to do that
Reply With Quote
  #3  
Old 12-16-2007, 05:43 PM
lambdacerro lambdacerro is offline
 
Join Date: Feb 2006
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by iogames View Post
Just to remind you, that like every cluster of society, there's just a few bad apples... I don't understand why do you want to do that
My community is dedicated to only a region in the world, and this would help me to redirect that people who is not part of my region and spend less bandwidth in my hosting.
Reply With Quote
  #4  
Old 12-16-2007, 07:05 PM
jdelasko's Avatar
jdelasko jdelasko is offline
 
Join Date: Jan 2007
Posts: 159
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by iogames View Post
Just to remind you, that like every cluster of society, there's just a few bad apples... I don't understand why do you want to do that

I plan on doing the exact same thing with my site as soon as my hosting company can add GeoIP to the server.

The vast majority of hacking and spamming attempts on my site originate from IP addresses in China, Russia, Korea, Russia, Germany, and a few others. Due to the nature of my site, there is simply no reason to allow IP addresses from those countries access to my site since the likelyhood of a single legitimate user from those countries is near zero.

While in many cases blocking entire countries is illogical, in my particular case it makes perfect sense.
Reply With Quote
  #5  
Old 12-16-2007, 07:37 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The lengths some people are prepared to go to reduce spam ceases to amaze me

Regarding the OP, just stick some PHP code to call the script in a Plugin hooked to the Global Start hook and you should be fine.
Reply With Quote
  #6  
Old 12-16-2007, 11:44 PM
valdet's Avatar
valdet valdet is offline
 
Join Date: Feb 2007
Posts: 505
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Could you share your solution to block specific countries to access the website. I am also highly interested in doing this for one of my sites.
Reply With Quote
  #7  
Old 12-17-2007, 03:30 AM
lambdacerro lambdacerro is offline
 
Join Date: Feb 2006
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ok, releasing the code and database:

What you need:
A website (duh)
A IP To Country Database (included here)

So the script is like this:
PHP Code:
<?php
// Check if the cookie exists
if( empty($_COOKIE['IsValid']) ) {

    
$valid 'true';

    
// Establishing a database connection
    
$dbh=mysql_connect("localhost:3306","USER","PASS");
    
mysql_select_db("IP_ToCountry_Database");

    
// Getting User's IP
    
$ip $_SERVER["REMOTE_ADDR"];

    
// Executing main query
    
$country_exec mysql_query("SELECT * FROM iptocountry WHERE ipto >= inet_aton('$ip') AND ipfrom <= inet_aton('$ip')");

    
// Fetching the record
    
$ccode_fetch=mysql_fetch_assoc($country_exec);

    
// Getting the country code from the array
    
$pais=$ccode_fetch['country2'];

    
// Declaring the Allowed Countries by their CountryCode2
    
$allowed = array('US''AR''UY''BR''FR');

    
// Verifying if the user belongs to any of the allowed countries
    
if (!in_array($pais$allowed)) {
    
$valid 'false';
    }
    
// Setting the cookie for a 10 minutes life
    
setcookie("IsValid"$validtime()+600);
    
mysql_close($dbh);
    if ( 
$_COOKIE['IsValid'] == 'false' ) {
    echo 
"Not allowed country";
    die();
    }
}else{
    if ( 
$_COOKIE['IsValid'] == 'false' ) {
    echo 
"Not allowed country";
    die();
    }
}
?>
There you have to set the MySQL Connection and the valid countries array.
You can get the countrycode2 of all the countries from here:
http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

You can get the IP To Country Table for free at http://ip-to-country.webhosting.info/
But it's in csv, I passed it to sql and attached.

I added this script at init.php, but I don't think init.php is the best place for adding that code.

That's All.

NOW I want to know how to transform this script in a vBulletin plugin, I think the ip to country table can be added to vBulletin database, and in the script use the db=> thing.

Thank you
Attached Files
File Type: zip ip-to-country 12-2007.sql.zip (819.4 KB, 5 views)
Reply With Quote
  #8  
Old 12-17-2007, 04:42 AM
jdelasko's Avatar
jdelasko jdelasko is offline
 
Join Date: Jan 2007
Posts: 159
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Opserty View Post
The lengths some people are prepared to go to reduce spam ceases to amaze me

It's not about reducing spam. It's about eliminating unwanted visitors to your site that are there for nothing other than hacking attempts and other malicious things. Most of the hacking attempts on my site originate from .cn .ru .de and a few other domains. If my site is about american baseball, what possible reason can I have for allowing these countries to access my site??? I have yet to see a single hacking attempt originate from a US IP address. Everyone that hasn't been living in a cave knows that blocking IP addresses and email addresses are 2 tactics that are completely useless. Blocking entire countries makes good sense in many cases. If I am not offering some sort of global service, why on earth shouldn't I block entire countries?

--------------- Added [DATE]1197875135[/DATE] at [TIME]1197875135[/TIME] ---------------

Quote:
Originally Posted by valdet View Post
Could you share your solution to block specific countries to access the website. I am also highly interested in doing this for one of my sites.

As an alternative, you can also check out this site: http://www.maxmind.com/app/geolitecountry

I recently had my hosting company move my site to a new server in preparation for running the GeoIP Apache API. If you are on a dedicated server, things are pretty straightforward. On a shared server, hosting companies get pretty cautious when you ask them to run a new API on that shared server. That's why my hosting comany moved me to a less populated server. Once the Apache API is running on my server, all I will have to do is upload a database file and add a few lines to a .htaccess file and I'm all set... no coding, no vbulleting hacks.... nothing. I won't even have to set up an sql database.

Even if you do block entire countries, you can still allow access from IP addresses that you specify even if they originate from a blocked country.
Reply With Quote
  #9  
Old 12-18-2007, 10:53 PM
lambdacerro lambdacerro is offline
 
Join Date: Feb 2006
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by jdelasko View Post
As an alternative, you can also check out this site: http://www.maxmind.com/app/geolitecountry

I recently had my hosting company move my site to a new server in preparation for running the GeoIP Apache API. If you are on a dedicated server, things are pretty straightforward. On a shared server, hosting companies get pretty cautious when you ask them to run a new API on that shared server. That's why my hosting comany moved me to a less populated server. Once the Apache API is running on my server, all I will have to do is upload a database file and add a few lines to a .htaccess file and I'm all set... no coding, no vbulleting hacks.... nothing. I won't even have to set up an sql database.

Even if you do block entire countries, you can still allow access from IP addresses that you specify even if they originate from a blocked country.
That's nice, but if you use shared hosting, the best is to use my code.

Btw, do you have any guide to make work that api?
Reply With Quote
  #10  
Old 12-19-2007, 05:23 PM
jdelasko's Avatar
jdelasko jdelasko is offline
 
Join Date: Jan 2007
Posts: 159
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by lambdacerro View Post
That's nice, but if you use shared hosting, the best is to use my code.

Btw, do you have any guide to make work that api?

You can read all about it at maxmind.com

I'm sure your code works just fine but I'm not interested in a plugin for vbulletin. GeoIP will block access to anything on my server.... period. Once the GeoIP API is running on my server, all I have to do is add a few lines to a .htaccess file to put it to work. The free version of GeoIP gets the country code to IP address translations from a file that resides on your server. The paid version of GeoIP uses an SQL database to look up the information and it's faster I presume. For now, the free version will work for me.
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 07:58 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04146 seconds
  • Memory Usage 2,298KB
  • Queries Executed 12 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (1)postbit_attachment
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • postbit_attachment
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete