Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]nadpisanie hasła uzytkownika
--imlegend--
post
Post #1





Goście







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.  
Go to the top of the page
+Quote Post

Posty w temacie
- -imlegend-   [MySQL][PHP]nadpisanie hasła uzytkownika   15.02.2014, 15:09:00
- - Pyton_000   pokaż lepiej logowanie jeszcze   15.02.2014, 16:06:57
- - -imlegend-   logowanie: [PHP] pobierz, plaintext <?phpsessi...   15.02.2014, 18:42:32
- - Pyton_000   Masz 2 sposoby generowania haseł: Kod$newpass...   15.02.2014, 19:10:03
- - -imlegend-   Uff, wielkie dzięki Pyton, rzeczywiście poprawiłem...   15.02.2014, 20:02:30
- - -imlegend-   Jak dodać że pomógł?   15.02.2014, 20:04:17
- - Pyton_000   Musisz być zarejestrowanym użytkownikiem.   15.02.2014, 20:06:30


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 16:57