Posted By

limonn on 10/04/06


Tagged


Versions (?)

Who likes this?

33 people have marked this snippet as a favorite

trof
blakeb
dmarten
demods
banjomamo
vali29
willcodeforfood
leandemon
pixelhandler
grn
oriolfb
salibaray
Nix
cjwilburn
nb109
Hollow
marnold2000
letosword
kellyrmartin
tapdrup
heatherk
fyreflyX
colingardom
bobbym245
adth
tspitzr
lfcortes
NeekGerd
wakeupandrage
svemoguci
bug5
ChristianOttinger
ringo380


login


 / Published in: PHP
 

URL: http://www.evolt.org/php_login_script_with_remember_me_feature

login with forgot

  1. <?
  2. include("database.php");
  3.  
  4. /**
  5.  * Returns true if the username has been taken
  6.  * by another user, false otherwise.
  7.  */
  8. function usernameTaken($username){
  9. global $conn;
  10. $username = addslashes($username);
  11. }
  12. $q = "select username from users where username = '$username'";
  13. $result = mysql_query($q,$conn);
  14. return (mysql_numrows($result) > 0);
  15. }
  16.  
  17. /**
  18.  * Inserts the given (username, password) pair
  19.  * into the database. Returns true on success,
  20.  * false otherwise.
  21.  */
  22. function addNewUser($username, $password){
  23. global $conn;
  24. $q = "INSERT INTO users VALUES ('$username', '$password')";
  25. return mysql_query($q,$conn);
  26. }
  27.  
  28. /**
  29.  * Displays the appropriate message to the user
  30.  * after the registration attempt. It displays a
  31.  * success or failure status depending on a
  32.  * session variable set during registration.
  33.  */
  34. function displayStatus(){
  35. $uname = $_SESSION['reguname'];
  36. if($_SESSION['regresult']){
  37. ?>
  38.  
  39. <h1>Registered!</h1>
  40. <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
  41.  
  42. <?
  43. }
  44. else{
  45. ?>
  46.  
  47. <h1>Registration Failed</h1>
  48. <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
  49. Please try again at a later time.</p>
  50.  
  51. <?
  52. }
  53. unset($_SESSION['reguname']);
  54. unset($_SESSION['registered']);
  55. unset($_SESSION['regresult']);
  56. }
  57.  
  58. if(isset($_SESSION['registered'])){
  59. /**
  60.  * This is the page that will be displayed after the
  61.  * registration has been attempted.
  62.  */
  63. ?>
  64.  
  65. <html>
  66. <title>Registration Page</title>
  67. <body>
  68.  
  69. <? displayStatus(); ?>
  70.  
  71. </body>
  72. </html>
  73.  
  74. <?
  75. return;
  76. }
  77.  
  78. /**
  79.  * Determines whether or not to show to sign-up form
  80.  * based on whether the form has been submitted, if it
  81.  * has, check the database for consistency and create
  82.  * the new account.
  83.  */
  84. if(isset($_POST['subjoin'])){
  85. /* Make sure all fields were entered */
  86. if(!$_POST['user'] || !$_POST['pass']){
  87. die('You didn\'t fill in a required field.');
  88. }
  89.  
  90. /* Spruce up username, check length */
  91. $_POST['user'] = trim($_POST['user']);
  92. if(strlen($_POST['user']) > 30){
  93. die("Sorry, the username is longer than 30 characters, please shorten it.");
  94. }
  95.  
  96. /* Check if username is already in use */
  97. if(usernameTaken($_POST['user'])){
  98. $use = $_POST['user'];
  99. die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
  100. }
  101.  
  102. /* Add the new account to the database */
  103. $md5pass = md5($_POST['pass']);
  104. $_SESSION['reguname'] = $_POST['user'];
  105. $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
  106. $_SESSION['registered'] = true;
  107. echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  108. return;
  109. }
  110. else{
  111. /**
  112.  * This is the page with the sign-up form, the names
  113.  * of the input fields are important and should not
  114.  * be changed.
  115.  */
  116. ?>
  117.  
  118. <html>
  119. <title>Registration Page</title>
  120. <body>
  121. <h1>Register</h1>
  122. <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
  123. <table align="left" border="0" cellspacing="0" cellpadding="3">
  124. <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
  125. <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
  126. <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
  127. </table>
  128. </form>
  129. </body>
  130. </html>
  131.  
  132.  
  133. <?
  134. }
  135. ?>

Report this snippet  

You need to login to post a comment.