PHP - Working Example of a login screen


SUBMITTED BY: efbee

DATE: Oct. 3, 2016, 6:42 p.m.

FORMAT: PHP

SIZE: 4.9 kB

HITS: 603

  1. Complete, simple, working example of a login screen-system using php
  2. functions cookies and a mysql database for begginers.
  3. <?
  4. // This first if statement checks to see if we have a username/pass submited by the form, if it does then it attempts to validate it.
  5. if($username && $password) {
  6. mysql_connect() or die ("Whoops"); // Connect to the database, or if connection fails print error message.
  7. $password = md5($password); // encode submited password with MD5 encryption and store it back in the same variable. If not on a windows box, I
  8. suggest you use crypt()
  9. $sql = "select * from login where username='$username'"; // query statment that gets the username/password from 'login' where the username is the same as
  10. the one you
  11. submited
  12. $r = mysql_db_query("register",$sql); // Execute Query
  13. // if no rows for that database come up, redirect.
  14. if(!mysql_num_rows($r))
  15. header("Location: $SCRIPT_NAME"); // This is the redirection, notice it uses $SCRIPT_NAME which is a predefined variable with the name of the script in it.
  16. $user = mysql_fetch_array($r); // if we got passed the last if statment means we have a registered username, get the rest of the info and put it in an array
  17. named $user
  18. if($user["password"] == $password) { // If the password stored in the database is the same as the password the user entered (which is now encryped with MD5)
  19. $password = serialize($password); // if we get this far we know we have a registered username, and the password matches.
  20. // serialize() the already incrypted password just for fun and mabey some extra security for when we
  21. store it in a cookie
  22. setcookie("candle_login","$username $password"); // Set the cookie named 'candle_login' with the value of the username (in plain text) and the password
  23. (which has been
  24. encrypted and serialized.)
  25. // set variable $msg with an HTML statement that basically says redirect to the next page. The reason we didn't use header() is that using setcookie() and
  26. header() at the same
  27. time isn't 100% compatible with all browsers, this is more compatible.
  28. $msg = "<meta http-equiv=\"Refresh\" content=\"0;url=./nextpage.php\">";
  29. }else{
  30. header("Location: $SCRIPT_NAME"); //If the password didn't match, redirect to this page in which $username and $password are reset therefore the first if
  31. () never gets executed
  32. }
  33. }
  34. if($msg) echo $msg; //if $msg is set echo it, resulting in a redirect to the next page.
  35. ?>
  36. // This is the login screen
  37. <html>
  38. <title>Login</title>
  39. <body bgcolor="yellow" text="black">
  40. <form method="post" action="<?echo $SCRIPT_NAME;?>"> // submit form data to this page
  41. <center><font size=+5><b>Welcome!</b></font></center>
  42. <br>
  43. <br>
  44. <br>
  45. <table cellspacing=0 cellpadding=0 width=320 align="center">
  46. <tr><td>
  47. Username:
  48. </td><td>
  49. <input name="username" type="text" width=10>
  50. </td></tr>
  51. <tr><td>
  52. Password:
  53. </td><td>
  54. <input name="password" type="password" width=10>
  55. </td></tr>
  56. <tr><td colspan=2 align="center">
  57. <input name="login" type="submit">
  58. </td></tr>
  59. </table>
  60. </form>
  61. </html>
  62. /* That was the login page
  63. Next is some code you can put into a different file (named 'login_check.inc' or something)
  64. that you include() on each page you want protected on your site.
  65. It uses the cookie from the first script to verify user has already been there.
  66. */
  67. <?
  68. // if the cookie doesn't exsist means the user hasn't been verified by the login page so send them back to the login page.
  69. if(!$candle_login)
  70. header("Location: ./login.php");
  71. if($phpcoders) { // if the cookie does exsist
  72. mysql_connect() or die ("Whoops"); //connect to db
  73. $user = explode(" ","$phpcoders"); //explode cookie value (which is the '$username $password (note seperated by space)) and store values in $user. Check
  74. manual for more info
  75. on explode()
  76. $sql = "select * from login where username='$user[0]'"; //sql statment that uses the username from the cookie.
  77. $r = mysql_db_query("register",$sql); //execute sql
  78. if(!mysql_num_rows($r)) { // if there are no rows, means no matches for that username
  79. header("Location: ./login.php"); // so go back to the login page
  80. }
  81. $chkusr = mysql_fetch_array($r); //if we got passed the last part, then get the username/password set that match that username
  82. if(unserialize($user[1]) != $chkusr[1]) //if the password from cookie (notice we have to unserialize it) doesn't match the one from the database
  83. header("Location: ./login.php"); // go back to the login page
  84. } // if it did match then continue on to page and this ends up doing nothing :)
  85. ?>

comments powered by Disqus