• We just launched and are currently in beta. Join us as we build and grow the community.

User authentication with notification of the system IP

danielg758

Meta Description Crafter
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
41
Likes
12
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. session_start

    (

    )

    ;
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <script type="text/javascript" src="js/jQuery.js"></script>
  9. <script type="text/javascript" src="js/notificationBox.js"></script>
  10. <style type="text/css">
  11. #mask {
  12. position:absolute;
  13. left:0;
  14. top:0;
  15. z-index:9000;
  16. background-color:#eee;
  17. display:none;
  18. }
  19. #boxes .window {
  20. position:absolute;
  21. left:0;
  22. top:0;
  23. width:440px;
  24. height:100px;
  25. display:none;
  26. z-index:9999;
  27. padding:20px;
  28. }
  29. #boxes #dialog {
  30. font-family:verdana;
  31. width:375px;
  32. height:103px;
  33. padding:10px;
  34. background-color:#ffffff;
  35. border-top-left-radius: 5pt;
  36. border-top-right-radius: 5pt;
  37. border-botttom-left-radius: 5pt;
  38. border-bottom-right-radius: 5pt;
  39. border-bottom-left-radius: 5pt;
  40. border: 2px solid lightgrey;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <?php
  46. function

    get_ip_address(

    )

    {
  47. $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'

    )

    ;
  48. foreach

    (

    $ip_keys

    as

    $key

    )

    {
  49. if

    (

    array_key_exists

    (

    $key

    ,

    $_SERVER

    )

    ===

    true

    )

    {
  50. foreach

    (

    explode

    (

    ','

    ,

    $_SERVER

    [

    $key

    ]

    )

    as

    $ip

    )

    {
  51. // trim for safety measures
  52. $ip

    =

    trim

    (

    $ip

    )

    ;
  53. // attempt to validate IP
  54. if

    (

    validate_ip(

    $ip

    )

    )

    {
  55. return

    $ip

    ;
  56. }
  57. }
  58. }
  59. }

  60. return

    isset

    (

    $_SERVER

    [

    'REMOTE_ADDR'

    ]

    )

    ? $_SERVER

    [

    'REMOTE_ADDR'

    ]

    :

    false

    ;
  61. }

  62. /*** Ensures an ip address is both a valid IP and does not fall within
  63. * a private network range.*/
  64. function

    validate_ip(

    $ip

    )

    {
  65. if

    (

    filter_var

    (

    $ip

    ,

    FILTER_VALIDATE_IP,

    FILTER_FLAG_IPV4 |

    FILTER_FLAG_NO_PRIV_RANGE |

    FILTER_FLAG_NO_RES_RANGE)

    ===

    false

    )

    {
  66. return

    false

    ;
  67. }
  68. return

    true

    ;
  69. }
  70. $setIP

    =

    get_ip_address(

    )

    ;
  71. ?>
  72. <?php
  73. //declaring variables
  74. $_dbHost

    =

    "localhost"

    ;
  75. $_dbUser

    =

    "root"

    ;
  76. $_dbPass

    =

    ""

    ;
  77. $_dbName

    =

    "tutorials"

    ;
  78. $_connFailed

    =

    "Database connection failed."

    ;
  79. $_dbConnFailed

    =

    "Database selection failed."

    ;
  80. ?>
  81. <?php
  82. //validate host connection
  83. if

    (

    !

    mysql_connect

    (

    $_dbHost

    ,

    $_dbUser

    ,

    $_dbPass

    )

    )

    {
  84. echo

    $_connFailed

    ;
  85. }
  86. //validate database
  87. if

    (

    !

    mysql_select_db

    (

    $_dbName

    )

    )

    {
  88. echo

    $_dbConnFailed

    ;
  89. }
  90. ?>
  91. <?php

  92. if

    (

    isset

    (

    $_POST

    [

    "login"

    ]

    )

    )

    {
  93. $uName

    =

    trim

    (

    $_POST

    [

    "uName"

    ]

    )

    ;
  94. $uPass

    =

    trim

    (

    $_POST

    [

    "uPass"

    ]

    )

    ;
  95. $_uIP

    =

    trim

    (

    $_POST

    [

    "uIP"

    ]

    )

    ;

  96. $loginQry

    =

    "SELECT * FROM users WHERE uName='$uName

    ' AND uPass='$uPass

    ' AND uIP='$_uIP

    '"

    ;
  97. $result

    =

    mysql_query

    (

    $loginQry

    )

    or die

    (

    "Database query failed: $loginQry

    "

    .

    mysql_error

    (

    )

    )

    ;
  98. $userRaw

    =

    mysql_fetch_array

    (

    $result

    )

    ;

  99. if

    (

    $userRaw

    )

    {
  100. $_SESSION

    [

    'id'

    ]

    =

    $userRaw

    [

    'id'

    ]

    ;
  101. echo

    "<script>windows: location='index.php?id=$uName

    '</script>"

    ;
  102. }

    else

    {
  103. $msgOut

    =

    "Sorry you can't login. Please check your input username and password."

    ;
  104. }
  105. }
  106. ?>
  107. <div>
  108. <?php

    echo

    $msgOut

    ?>
  109. </div>
  110. <div>

  111. <fieldset>
  112. <legend>User Authentication</legend>
  113. <form action="login.php" method="post">
  114. Username:
  115. <br />
  116. <input type="text" name="uName" placeholder="Username!">
  117. <br />
  118. <br />
  119. Password:
  120. <br />
  121. <input type="password" name="uPass" placeholder="Password!">
  122. <br />
  123. <br />
  124. IP Address:
  125. <br />
  126. <input type="text" name="uIP" value="<?php

    echo

    $setIP

    ;

    ?>

    ">
  127. <br /><br />
  128. <input type="submit" name="login" value="Login"><input type="reset" value="Clear">
  129. </form>
  130. <br />
  131. <a href="login.php">Not yet registered?</a>
  132. </fieldset>
  133. </div>

  134. <?php
  135. $_uIP

    =

    $setIP

    ;
  136. $query

    =

    mysql_query

    (

    "Select * From users Where uIP = '$_uIP

    '"

    )

    or die

    (

    "Database query failed."

    .

    mysql_error

    (

    )

    )

    ;
  137. if

    (

    mysql_num_rows

    (

    $query

    )

    )

    {
  138. }

    else

    {
  139. ?>
  140. <div id="boxes" onClick="window.location='register.php'">
  141. <div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window">
  142. <b style="color: #336699;">System Notification</b>
  143. <hr />
  144. <div>
  145. <font color="red"><b>Your machine is not recognized. Please ask for assistance to your system administrator.</b></font>
  146. </div>
  147. </div>
  148. <div style="width: 1478px; height: 202px; display: none; opacity: 0.8;" id="mask">
  149. </div>
  150. </div>
  151. <?php
  152. }
  153. ?>
  154. <?php
  155. //$query = mysql_query("Select * From users Where ");
  156. ?>
  157. </body>
  158. </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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,692

323,701

Top