User authentication with notification of the system IP

Language

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

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Comments

Add new comment