1. <?php
  2. // Connects to your Database
  3. mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
  4. mysql_select_db("Database_Name") or die(mysql_error());
  5.  
  6. //Checks if there is a login cookie
  7. if(isset($_COOKIE['ID_my_site']))
  8.  
  9. //if there is, it logs you in and directes you to the members page
  10. {
  11. $username = $_COOKIE['ID_my_site'];
  12. $pass = $_COOKIE['Key_my_site'];
  13. $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
  14. while($info = mysql_fetch_array( $check ))
  15. {
  16. if ($pass != $info['password'])
  17. {
  18. }
  19. else
  20. {
  21. header("Location: members.php");
  22.  
  23. }
  24. }
  25. }
  26.  
  27. //if the login form is submitted
  28. if (isset($_POST['submit'])) { // if form has been submitted
  29.  
  30. // makes sure they filled it in
  31. if(!$_POST['username'] | !$_POST['pass']) {
  32. die('You did not fill in a required field.');
  33. }
  34. // checks it against the database
  35.  
  36. if (!get_magic_quotes_gpc()) {
  37. $_POST['email'] = addslashes($_POST['email']);
  38. }
  39. $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
  40.  
  41. //Gives error if user dosen't exist
  42. $check2 = mysql_num_rows($check);
  43. if ($check2 == 0) {
  44. die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
  45. }
  46. while($info = mysql_fetch_array( $check ))
  47. {
  48. $_POST['pass'] = stripslashes($_POST['pass']);
  49. $info['password'] = stripslashes($info['password']);
  50. $_POST['pass'] = md5($_POST['pass']);
  51.  
  52. //gives error if the password is wrong
  53. if ($_POST['pass'] != $info['password']) {
  54. die('Incorrect password, please try again.');
  55. }
  56.  
  57. else
  58. {
  59.  
  60. // if login is ok then we add a cookie
  61. $_POST['username'] = stripslashes($_POST['username']);
  62. $hour = time() + 3600;
  63. setcookie(ID_my_site, $_POST['username'], $hour);
  64. setcookie(Key_my_site, $_POST['pass'], $hour);
  65.  
  66. //then redirect them to the members area
  67. header("Location: members.php");
  68. }
  69. }
  70. }
  71. else
  72. {
  73.  
  74. // if they are not logged in
  75. ?>
  76. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
  77. <table border="0">
  78. <tr><td colspan=2><h1>Login</h1></td></tr>
  79. <tr><td>Username:</td><td>
  80. <input type="text" name="username" maxlength="40">
  81. </td></tr>
  82. <tr><td>Password:</td><td>
  83. <input type="password" name="pass" maxlength="50">
  84. </td></tr>
  85. <tr><td colspan="2" align="right">
  86. <input type="submit" name="submit" value="Login">
  87. </td></tr>
  88. </table>
  89. </form>
  90. <?php
  91. }
  92.  
  93. ?>
  94.