Advertisement
hexasoft

Credit Card Fraud Prevention Using PHP and MySQL for IPV6

May 16th, 2018
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user Credit Card Fraud Prevention                    */
  3. /*              suggestions for different city locations across the country.                                    */
  4. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  5. /*https://www.ip2location.com/tutorials/credit-card-fraud-prevention-using-php-and-mysql-database               */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP Country database for IPv6 at https://lite.ip2location.com/database/ip-country         */
  8. /* You can obtain free country information data at https://www.ip2location.com/free/country-information         */
  9.  
  10. <?php
  11.     // country in billing address, in this example, we assigned "US" for United States.
  12.     $billingCountrySHORT = "US";
  13.  
  14.     // Replace this MYSQL server variables with actual configuration
  15.     $mysql_server = "localhost";
  16.     $mysql_user_name = "root";
  17.     $mysql_user_pass = "";
  18.  
  19.      
  20.     // Retrieve visitor IP address from server variable REMOTE_ADDR
  21.     $ipaddress = $_SERVER['REMOTE_ADDR'];
  22.  
  23.     // Convert IP address to IP number for querying database
  24.     $ipno = Dot2LongIPv6($ipaddress);
  25.  
  26.     // Connect to the database server
  27.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  28.  
  29.     // Connect to the IP2Location database
  30.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  31.  
  32.     // SQL query string to match the recordset that the IP number fall between the valid range
  33.     $query = "SELECT * FROM ip_country WHERE $ipno <= ip_to LIMIT 1";        
  34.      
  35.     // Execute SQL query
  36.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  37.  
  38.     // Retrieve the recordset (only one)
  39.     $row = mysqli_fetch_assoc($result);
  40.  
  41.     // Keep the country information into two different variables
  42.     $country_code = $row['country_code'];
  43.     $country_name = $row['country_name'];
  44.  
  45.     // Free recordset and close database connection
  46.     mysqli_free_result($result);
  47.     mysqli_close($link);
  48.  
  49.      
  50.     if ($country_code == $billingCountrySHORT) {
  51.         // IP address same as country in billing address: Low Fraud Risk
  52.         echo "Low Frauk Risk ";
  53.       } else {                
  54.         // IP address different from country in billing address: High Fraud Risk
  55.         echo"High Fraud risk";
  56.       }
  57.  
  58.     exit;
  59.      
  60.      
  61.     // Function to convert IP address to IP number (IPv6)
  62.     function Dot2LongIPv6 ($IPaddr) {
  63.         $int = inet_pton($IPaddr);
  64.         $bits = 15;
  65.         $ipv6long = 0;
  66.         while($bits >= 0){
  67.             $bin = sprintf("%08b", (ord($int[$bits])));
  68.             if($ipv6long){
  69.                 $ipv6long = $bin . $ipv6long;
  70.             }
  71.             else{
  72.                 $ipv6long = $bin;
  73.             }
  74.             $bits--;
  75.         }
  76.         $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  77.         return $ipv6long;
  78.     }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement