Love Telling ducduct nhắn với moi nguoi: i love 4rumminhtuancn nhắn với 4rum: Chào cả nhà, đang test tính năng nè

+ Reply to Thread
Results 1 to 1 of 1

Thread:
How to Redirect users based on their IP Using Php and MySQL

  1. #1
    minhtuancn's Avatar
    minhtuancn is offline Ông chủ VietKeyNet
    Join Date
    Aug 2006
    Posts
    718
    Rep Power
    10

    How to Redirect users based on their IP Using Php and MySQL

    I’ll illustrate how to use PHP (server side scripting language) and MYSQL (IP address to country lookup database) to redirect your web site visitors to a different page based on their IP addresses.
    The MySQL Part
    1>Create a MySQL database. I like phpMyAdmin, but use whatever method you are comfortable with. Name the database ip2country.

    Create and connect to 'ip2country' database
    mysql> CREATE DATABASE ip2country
    mysql> CONNECT ip2country
    2>Create 'ip2country' table
    Code:
    CREATE TABLE `ip2country` (
      `begin_ip` varchar(15) NOT NULL default '',
      `end_ip` varchar(15) NOT NULL default '',
      `begin_number` double NOT NULL default '0',
      `end_number` double NOT NULL default '0',
      `countryCode` char(2) NOT NULL default '',
      `countryName` varchar(64) NOT NULL default '',
      PRIMARY KEY  (`begin_number`,`end_number`)
    )
    
    3>Import the 'ip2country.csv' database into table 'ip2country'
    It is easier by using the LOAD DATA feature available in MYSQL.
    mysql> LOAD DATA INFILE "/ip2country.csv" INTO TABLE IPCountry FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r';
    IP-Country mapping Database Available Formats:
    1>Comma-separated values (CSV) ( Sample data)
    2>Plain text For Target Country by IP Address
    3>MySQL dump format (SQL)
    4>MS Access database (.mdb)
    5>Ip2country database For phpAdsNew (New Format)
    If you have the full version of IP Country mapping database,you can easy Importing IP2Country MySQL dump file into MySQL.
    The PHP Part
    Let's assume the client is looking to evaluate 3 countries (UK, USA and Ireland) . if the visitor in the the USA it will go to us.html and if in the UK it will go to uk.html, otherwise Redirect to uk.html
    1>Detect the real IP address of visitor
    PHP Code:
     <?if (getenv(HTTP_X_FORWARDED_FOR)) { 
         
    $ipaddress getenv(HTTP_X_FORWARDED_FOR); 
        } else { 
        
    $ipaddress getenv(REMOTE_ADDR); }
        
    ?> 
    2>Redirect Visitors By Country (PHP   Script)             
    <?php 
    // Get the real IP address of the  visitor
    if (getenv(HTTP_X_FORWARDED_FOR)) 
      { 
    $ip getenv(HTTP_X_FORWARDED_FOR); } 
      else { 
    $ip getenv(REMOTE_ADDR); }

     
    // Convert IP address to IP number for querying database
    $ipn anp_ip2long($ip);

    // Establish a database connection (adjust address, username, and password)
    $mysql_server "localhost"
    $mysql_user_name "UserName"
    $mysql_user_pass "Password"

    // Connect to the database server 
    $link mysql_connect($mysql_server$mysql_user_name$mysql_user_pass) or die("Could not connect to MySQL database"); 

    // Connect to the ip2country database 
    mysql_select_db("ip2country") or die("Could not select database"); 
                 
    // Create a query string
      
    $country_query "SELECT countrycode FROM ip2country WHERE end_number >=$ipn AND begin_number<=$ipn";
      
      
    // Execute the query
      
    $country_exec mysql_query($country_query);
     
    // Fetch the record set into an array
      
    $ccode_array mysql_fetch_array($country_exec);
     
    // Close the database connection
      
    mysql_close($link);

    // Get the country code from the array and save it as a variable
      
    $countrycode $ccode_array['countrycode'];
     
    // If the visitors are from US, redirect them to US Web Page 
    if ($countrycode == "US"
    {
    Header("Location: http://www.yourstore.com/us.html"); 
    } else { 
    if (
    $countrycode == "UK"
    {
    Header("Location: http://www.yourstore.com/uk.html"); 

    // Otherwise, redirect them to IE Web Page 
      
    else {   Header("Location: http://www.yourstore.com/ie.html");}
    }
    exit;

     
    // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number 
                     
    function anp_ip2long ($IPaddr)
        {
        if (
    $IPaddr == "") {
        return 
    0;
        } else {
        
    $ips split ("\.""$IPaddr");
        return (
    $ips[3] + $ips[2] * 256 $ips[1] * 65536 $ips[0] * 16777216);
        }
        }          
    ?>
    http://phpweby.com/software/ip2country

    Sửa file php.ini
    memory_limit = 512M ; Maximum amount of memory a script may consume (8MB)
    ;
    ; Safe Mode
    ;
    safe_mode = off << off

    View more random threads same category:

    Last edited by minhtuancn; 27-06-2011 at 12:23 PM.
    - Yahoo minhtuancn

+ Reply to Thread

Similar Threads

  1. BigDump: Staggered MySQL Dump Importer
    By minhtuancn in forum Tools
    Replies: 0
    Last Post: 11-12-2006, 02:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86