Advertisement
hexasoft

Display San Francisco Bay Area for IPV4

May 16th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display San Francisco Bay Area .                */
  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/display-san-francisco-bay-area                                         */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location LITE database for IPv4 at https://lite.ip2location.com                       */
  8. /* You can obtain free Bay Area CSV file form Github at https://github.com/chrislim2888/bay-area                */
  9.  
  10. <?php
  11.     if(isset($_POST['ip_address'])){
  12.     $conn = new mysqli("localhost", "root", "", "sf_bay");
  13.     if (mysqli_connect_errno()) {
  14.         echo "<p>Connection failed:".mysqli_connect_error()."</p>\n";
  15.     }
  16.  
  17.     $ip = $_POST['ip_address'];
  18.  
  19.     $sql = "SELECT db.country_name, db.region_name, db.city_name, b.bay_area
  20.    FROM ip2location_db3 db, bay_area b
  21.    WHERE db.region_name = b.region_name
  22.    AND db.country_code = b.country_code
  23.    AND inet_aton('$ip') <= db.ip_to
  24.    limit 1";
  25.  
  26.     $result = mysqli_query($conn, $sql) or die (mysqli_error($conn));
  27.  
  28.     mysqli_close($conn);
  29.     }
  30.     ?>
  31.     <!DOCTYPE html>
  32.     <html>
  33.     <head>
  34.         <title>San Francisco Bay Areas</title>
  35.         <style>
  36.             th, td{
  37.                 padding-right: 15px;
  38.                 text-align: left;
  39.             }
  40.         </style>
  41.     </head>
  42.  
  43.     <body>
  44.         <h2>San Francisco Bay Areas</h2>
  45.         <p>Enter an IP address to extract bay area information</p>
  46.         <form name="query" method="post">
  47.             <label for="address">IP here: </label>
  48.             <input type="text" name="ip_address" id="ip_address" size="40" value="<?= isset($_POST['ip_address']) ? htmlspecialchars($_POST['ip_address']) : '' ?>">
  49.             <button type="submit">Run</button>
  50.         </form>
  51.         <br/>
  52.         <table>
  53.         <tr>
  54.             <th>Country</th>
  55.             <th>Region</th>
  56.             <th>City</th>
  57.             <th>Bay Area</th>
  58.         </tr>
  59.         <?php
  60.         if (isset($ip)){
  61.             while ($row = mysqli_fetch_assoc($result)){
  62.                 $country = $row['country_name'];
  63.                 $region = $row['region_name'];
  64.                 $city = $row['city_name'];
  65.                 $bay = $row['bay_area'];
  66.                 echo "<tr>";
  67.                 echo "<td>".$country."</td>";
  68.                 echo "<td>".$region."</td>";
  69.                 echo "<td>".$city."</td>";
  70.                 echo "<td>".$bay."</td>";
  71.                 echo "</tr>";
  72.             }
  73.             echo "</table>";
  74.         }
  75.     ?>
  76.  
  77.     </body>
  78.     </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement