Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]nadpisanie hasła uzytkownika
Forum PHP.pl > Forum > Przedszkole
-imlegend-
Witam, znalazłem w sieci parę skryptów wysyłających do użytkownika mail z nowym hasłem.
Skrypty nawet działają (wysyłaja maile itp.), jednak za każdym razem jest to samo - nie da się zalogować na nowe hasło.
Pewnie coś źle robię z szyfrowaniem hasła - md5, może ktoś zobaczy błąd. Oto mój ostatni kod:
  1. <?php
  2.  
  3. define('IN_SCRIPT', true);
  4. // Start a session
  5.  
  6. //Connect to the MySQL Database
  7. include 'config2.php';
  8.  
  9. //this function will display error messages in alert boxes, used for login forms so if a field is invalid it will still keep the info
  10. //use error('foobar');
  11. function error($msg) {
  12. ?>
  13. <html>
  14. <head>
  15. <script language="JavaScript">
  16. <!--
  17. alert("<?=$msg?>");
  18. history.back();
  19. //-->
  20. </script>
  21. </head>
  22. <body>
  23. </body>
  24. </html>
  25. <?
  26. }
  27.  
  28. //This functions checks and makes sure the email address that is being added to database is valid in format.
  29. function check_email_address($email) {
  30. // First, we check that there's one @ symbol, and that the lengths are right
  31. if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
  32. // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
  33. return false;
  34. }
  35. // Split it into sections to make life easier
  36. $email_array = explode("@", $email);
  37. $local_array = explode(".", $email_array[0]);
  38. for ($i = 0; $i < sizeof($local_array); $i++) {
  39. if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
  40. return false;
  41. }
  42. }
  43. if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
  44. $domain_array = explode(".", $email_array[1]);
  45. if (sizeof($domain_array) < 2) {
  46. return false; // Not enough parts to domain
  47. }
  48. for ($i = 0; $i < sizeof($domain_array); $i++) {
  49. if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
  50. return false;
  51. }
  52. }
  53. }
  54. return true;
  55. }
  56.  
  57.  
  58. if (isset($_POST['submit'])) {
  59.  
  60. if ($_POST['forgotpassword']=='') {
  61. error('Please Fill in Email.');
  62. }
  63. $forgotpassword = htmlspecialchars(stripslashes($_POST['forgotpassword']));
  64. }
  65. else {
  66. $forgotpassword = htmlspecialchars($_POST['forgotpassword']);
  67. }
  68. //Make sure it's a valid email address, last thing we want is some sort of exploit!
  69. if (!check_email_address($_POST['forgotpassword'])) {
  70. error('Email Not Valid - Must be in format of name@domain.tld');
  71. }
  72. // Lets see if the email exists
  73. $sql = "SELECT COUNT(*) FROM users WHERE email = '$forgotpassword'";
  74. $result = mysql_query($sql)or die('Could not find member: ' . mysql_error());
  75. if (!mysql_result($result,0,0)>0) {
  76. error('Email Not Found!');
  77. }
  78.  
  79. //Generate a RANDOM MD5 Hash for a password
  80. $random_password=md5(uniqid(rand()));
  81.  
  82. //Take the first 8 digits and use them as the password we intend to email the user
  83. $emailpassword=substr($random_password, 0, 8);
  84.  
  85. //Encrypt $emailpassword in MD5 format for the database
  86. $newpassword = md5($emailpassword);
  87.  
  88. // Make a safe query
  89. $query = sprintf("UPDATE `users` SET `pass` = '%s'
  90. WHERE `email` = '$forgotpassword'",
  91. mysql_real_escape_string($newpassword));
  92.  
  93. mysql_query($query)or die('Could not update members: ' . mysql_error());
  94.  
  95. //Email out the infromation
  96. $subject = "Your New Password";
  97. $message = "Your new password is as follows:
  98. ----------------------------
  99. Password: $emailpassword
  100. ----------------------------
  101. Please make note this information has been encrypted into our database
  102.  
  103. This email was automatically generated.";
  104.  
  105. if(!mail($forgotpassword, $subject, $message, "FROM: $site_name <$site_email>")){
  106. die ("Sending Email Failed, Please Contact Site Admin! ($site_email)");
  107. }else{
  108. error('New Password Sent!.');
  109. }
  110.  
  111. }
  112.  
  113. else {
  114. ?>
  115. <form name="forgotpasswordform" action="" method="post">
  116. <table border="0" cellspacing="0" cellpadding="3" width="100%">
  117. <caption>
  118. <div>Forgot Password</div>
  119. </caption>
  120. <tr>
  121. <td>Email Address:</td>
  122. <td><input name="forgotpassword" type="text" value="" id="forgotpassword" /></td>
  123. </tr>
  124. <tr>
  125. <td colspan="2" class="footer"><input type="submit" name="submit" value="Submit" class="mainoption" /></td>
  126. </tr>
  127. </table>
  128. </form>
  129. <?
  130. }
  131. ?>
  132.  
Pyton_000
pokaż lepiej logowanie jeszcze
-imlegend-
logowanie:

  1. <?php
  2.  
  3.  
  4.  
  5. require 'config2.php'; // Do3?cz plik konfiguracyjny i po3?czenie z baz?
  6.  
  7. /**
  8.  * SKRYPT LOGOWANIA
  9.  */
  10. require_once 'user.class.php'; // Do3?czamy rdzen systemu u?ytkowników
  11.  
  12.  
  13. ?>
  14.  
  15. <!doctype html>
  16.  
  17. <html>
  18.  
  19. <head>
  20. <title>strona profilu</title>
  21. <meta charset="iso-8859-2">
  22. <link rel="stylesheet" type="text/css" href = "style6.css" media = "all">
  23. </head>
  24. <body>
  25. <div id = "zaw">
  26. <div id = "pole">
  27.  
  28. <?php
  29.  
  30.  
  31. $login = htmlspecialchars(mysql_real_escape_string($_POST['login']));
  32. $pass = mysql_real_escape_string($_POST['pass']);
  33.  
  34. if ($_POST['send'] == 1) {
  35. // Sprawd?, czy wszystkie pola zosta3y uzupe3nione
  36. if (!$login or empty($login)) {
  37. die ('<p class="error">Wypełnij pole z loginem!</p>');
  38. }
  39.  
  40. if (!$pass or empty($pass)) {
  41. die ('<p class="error">Wypełnij pole z hasłem!</p>');
  42. }
  43.  
  44. $pass = user::passSalter($pass); // Posól i zahashuj has3o
  45.  
  46. // Sprawd?, czy u?ytkownik o podanym loginie i ha?le isnieje w bazie danych
  47. $userExists = mysql_fetch_array(mysql_query("SELECT COUNT(*)FROM users WHERE login = '$login' AND pass = '$pass'"));
  48. echo "<div id = \"logz\">";
  49. echo "<div id = \"logs\">";
  50. if ($userExists[0] == 0) {
  51. // U?ytkownik nie istnieje w bazie
  52. echo '<p class="error">Użytkownik o podanym loginie i haśle nie istnieje.</p>';
  53. echo "</div>";
  54. echo "</div>";
  55. }
  56.  
  57. else {
  58. // U?ytkownik istnieje
  59. $user = user::getData($login, $pass, $id); // Pobierz dane u?ytknika do tablicy i zapisz j? do zmiennej $user
  60.  
  61. // Przypisz pobrane dane do sesji
  62. $_SESSION['login'] = $login;
  63. $_SESSION['pass'] = $pass;
  64. $_SESSION['id'] = $id;
  65. echo '<p class="success">Zostałeś zalogowany. Możesz przejść na <a href="index2.php">stronę główną</a></p>';
  66. }
  67. }
  68.  
  69. else {
  70. /**
  71.   * FORMULARZ LOGOWANIA
  72.   */
  73.  
  74.  
  75.  
  76.  
  77. ?>
  78.  
  79.  
  80. <div id = "sowa">
  81.  
  82. <div id = "srodek">
  83. <div id = "logo"></div>
  84. <form method="post" action="">
  85. <label for="login">Login:</label>
  86. <input type="text" name="login" maxlength="20" id="login">
  87. <br />
  88. <label for="pass">Haslo:</label>
  89. <input type="password" name="pass" maxlength="40" id="pass"><br>
  90.  
  91. <input type="hidden" name="send" value="1">
  92. <input type="submit" value="Zaloguj">
  93. </form>
  94. <a href = "robroy.php">Przypomnij hasło</a>
  95. </div></div>
  96. <?php
  97. }
  98.  
  99.  
  100. ?>
  101. </div>
  102. </div>
  103. </body>
  104. </html>
Pyton_000
Masz 2 sposoby generowania haseł:
Kod
$newpassword = md5($emailpassword);

i
Kod
$pass = user::passSalter($pass); // Posól i zahashuj has3o
-imlegend-
Uff, wielkie dzięki Pyton, rzeczywiście poprawiłem to generowanie i działa.
-imlegend-
Jak dodać że pomógł?
Pyton_000
Musisz być zarejestrowanym użytkownikiem.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.