User authentication with notification of the system IP PHP Script


User authentication with notification of the system IP PHP Script

his code will allows you to login with and notify you if your machine to be recognized to the system, with the help of the PHP codes that calls your IP Address.

Please see below and copy/paste to your web server then run to your browser.

 

 
 
  1. <?php
  2.       session_start();
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html</a> xmlns="<a href="http://www.w3.org/1999/xhtml">
  7. " rel="nofollow">http://www.w3.org/1999/xhtml">
  8. </a>      <head>
  9.             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10.             <script type="text/javascript" src="js/jQuery.js"></script>
  11.             <script type="text/javascript" src="js/notificationBox.js"></script>
  12.       <style type="text/css">
  13.             #mask {
  14.             position:absolute;
  15.             left:0;
  16.             top:0;
  17.             z-index:9000;
  18.             background-color:#eee;
  19.             display:none;
  20.             }
  21.             #boxes .window {
  22.             position:absolute;
  23.             left:0;
  24.             top:0;
  25.             width:440px;
  26.             height:100px;
  27.             display:none;
  28.             z-index:9999;
  29.             padding:20px;
  30.             }
  31.             #boxes #dialog {
  32.                   font-family:verdana;
  33.                   width:375px;
  34.                   height:103px;
  35.                   padding:10px;
  36.                   background-color:#ffffff;
  37.                   border-top-left-radius: 5pt;
  38.                   border-top-right-radius: 5pt;
  39.                   border-botttom-left-radius: 5pt;
  40.                   border-bottom-right-radius: 5pt;
  41.                   border-bottom-left-radius: 5pt;
  42.                   border: 2px solid lightgrey;
  43.             }
  44.       </style>
  45.       </head>
  46.       <body>
  47.             <?php
  48.                         function get_ip_address() {
  49.                               $ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
  50.                               foreach ($ip_keys as $key) {
  51.                                     if (array_key_exists($key, $_SERVER) === true) {
  52.                                           foreach (explode(',', $_SERVER[$key]) as $ip) {
  53.                                           // trim for safety measures
  54.                                           $ip = trim($ip);
  55.                                           // attempt to validate IP
  56.                                           if (validate_ip($ip)) {
  57.                                                 return $ip;
  58.                                           }
  59.                                           }
  60.                                     }
  61.                               }
  62.                               return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
  63.                         }
  64.                         /*** Ensures an ip address is both a valid IP and does not fall within
  65.                         * a private network range.*/
  66.                         function validate_ip($ip) {
  67.                               if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
  68.                               return false;
  69.                               }
  70.                               return true;
  71.                         }
  72.                         $setIP = get_ip_address();
  73.                   ?>
  74.             <?php
  75.             //declaring variables
  76.                   $_dbHost = "localhost";
  77.                   $_dbUser = "root";
  78.                   $_dbPass = "";
  79.                   $_dbName = "tutorials";
  80.                   $_connFailed = "Database connection failed.";
  81.                   $_dbConnFailed = "Database selection failed.";
  82.             ?>
  83.             <?php
  84.                   //validate host connection
  85.                   if(!mysql_connect($_dbHost, $_dbUser, $_dbPass)) {
  86.                         echo $_connFailed;
  87.                   }
  88.                   //validate database
  89.                   if(!mysql_select_db($_dbName)) {
  90.                         echo $_dbConnFailed;
  91.                   }
  92.             ?>
  93.             <?php
  94.                   if(isset($_POST["login"])) {
  95.                         $uName = trim($_POST["uName"]);
  96.                         $uPass = trim($_POST["uPass"]);
  97.                         $_uIP = trim($_POST["uIP"]);
  98.                         $loginQry = "SELECT * FROM users WHERE uName='$uName' AND uPass='$uPass' AND uIP='$_uIP'";
  99.                         $result = mysql_query($loginQry) or die ("Database query failed: $loginQry" . mysql_error());
  100.                         $userRaw = mysql_fetch_array($result);
  101.                         if($userRaw) {
  102.                               $_SESSION['id'] = $userRaw['id'];
  103.                                     echo "<script>windows: location='index.php?id=$uName'</script>";
  104.                         } else {
  105.                               $msgOut = "Sorry you can't login. Please check your input username and password.";
  106.                         }
  107.                   }
  108.       ?>
  109.             <div>
  110.                   <?php echo $msgOut?>
  111.             </div>
  112.             <div>
  113.             <fieldset>
  114.             <legend>User Authentication</legend>
  115.             <form action="login.php" method="post">
  116.                   Username:
  117.                   <br />
  118.                   <input type="text" name="uName" placeholder="Username!">
  119.                   <br />
  120.                   <br />
  121.                   Password:
  122.                   <br />
  123.                   <input type="password" name="uPass" placeholder="Password!">
  124.                   <br />
  125.                   <br />
  126.                   IP Address:
  127.                   <br />
  128.                   <input type="text" name="uIP" value="<?php echo $setIP; ?>">
  129.                   <br /><br />
  130.                   <input type="submit" name="login" value="Login"><input type="reset" value="Clear">
  131.             </form>
  132.             <br />
  133.             <a href="login.php">Not yet registered?</a>      
  134.             </fieldset>
  135.             </div>
  136.             <?php
  137.                   $_uIP = $setIP;
  138.                   $query = mysql_query("Select * From users Where uIP = '$_uIP'") or die ("Database query failed." . mysql_error());
  139.                         if(mysql_num_rows($query)) {
  140.                   } else {
  141.                   ?>
  142.                         <div id="boxes" onClick="window.location='register.php'">
  143.                               <div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window">
  144.                                     <b style="color: #336699;">System Notification</b>
  145.                                     <hr />
  146.                                     <div>
  147.                                           <font color="red"><b>Your machine is not recognized. Please ask for assistance to your system administrator.</b></font>
  148.                                     </div>
  149.                               </div>
  150.                               <div style="width: 1478px; height: 202px; display: none; opacity: 0.8;" id="mask">
  151.                               </div>
  152.                         </div>
  153.                   <?php
  154.                   }
  155.                   ?>
  156.                   <?php
  157.                         //$query = mysql_query("Select * From users Where ");
  158.                   ?>
  159.       </body>
  160.       </html>

 

 

Hope this code will help you guys. Please leave your comments below or any suggestions. Any problem with your web development just inquire me with this email: [email protected] 🙂 happy codings…

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"