Password Reset script

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jules Kreutzer

Jules Kreutzer

30/11/2012 21:09:13
Quote Anchor link
Weet iemand misschien een goed script of handige tutorial waarmee een email wordt verstuurd als je je wachtwoord wilt resetten.
In die email is dan een link waar je op moet klikken en zo kun je je wachtwoord veranderen.

Ik heb namelijk mijn wachtwoorden in mij database een md5 hash meegegeven waardoor ik niet simpel het wachtwoord kan versturen via email.
 
PHP hulp

PHP hulp

25/04/2024 09:49:24
 
- Ariën  -
Beheerder

- Ariën -

30/11/2012 21:11:33
Quote Anchor link
Waar loop je precies op vast? De basis is niets meer dan een query i.c.m een $_GET-variabele.
 
Jules Kreutzer

Jules Kreutzer

30/11/2012 21:12:05
Quote Anchor link
Ik weet niet goed hoe ik dit moet maken, dat is het probleem
 
- Ariën  -
Beheerder

- Ariën -

30/11/2012 21:23:20
Quote Anchor link
Je maakt een pagina aan waar de gebruikers hun mailadres en of username in kunnen vullen.
Vervolgens als deze correct zijn maak in je in de database in de tabel users in het veld pass_key een unieke code aan, en deze mail je naar de gebruiker toe.

De code staat ook in de link, welke je in de mail hebt geplaatst (www.site.nl/activatie.php?code=MNgJHGJKG&userId=42}, als de gebruikers naar die URL gaan controlleer je of die kloppen, en zo ja, dan vraag je direct wat hun nieuwe password moet worden.
 
Jan R

Jan R

01/12/2012 07:33:16
Quote Anchor link
Ik wil je wel mijnscript geven; het is gebasseerd op iets wat ik op internet gevonden heb.http://www.laughing-buddha.net/php/password
Je hebt 2 bestanden
1. newpaswoord.html
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>
            Nieuw paswoord aanvragen
        </title>
       <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
       <META NAME="AUTHOR" CONTENT="JanR">
       <META NAME="LANGUAGE" CONTENT="Dutch">
       <LINK id="style" HREF="invoer.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
            <tr>
                <form name="form1" method="post" action="sendnewpasword.php">
                    <td>
                        <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                            <tr>
                                <td colspan="3"><strong>Gebruikers Login </strong></td>
                            </tr>
                            <tr>
                                <td width="78">e-mail:</td>
                                <td width="300"><input name="myusername" type="text" id="myusername"></td>
                            </tr>                            
                            <tr>
                                <td colspan=2><input type="submit" name="Submit" value="Nieuw paswoord aanvragen"></td>
                            </tr>
                        </table>
                    </td>
                </form>                
            </tr>
        </table>
    </body>
</html>


2. sendnewpaswoord.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>
            Nieuw paswoord opgezonden!
        </title>
       <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
       <META NAME="AUTHOR" CONTENT="JanR">
       <META NAME="LANGUAGE" CONTENT="Dutch">
       <LINK id="style" HREF="invoer.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <?php
        function RandomPass($length = null) {
            $length = $length < 8 ? 8 : $length;
            for($a=0;$a<$length;$a++) {
                @
$pass .= chr(rand(33, 126));
            }

            return $pass;
        }
        function
generatePassword ($length = 8)  {
        // start with a blank password
        $password = "";

        // define possible characters - any character in this string can be
        // picked for use in the password, so if you want to put vowels back in
        // or add special characters such as exclamation marks, this is where
        // you should do it

        $possible = "012346789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#!%/*-+";

        // we refer to the length of $possible a few times, so let's grab it now
        $maxlength = strlen($possible);
      
        // check for length overflow and truncate if necessary
        if ($length > $maxlength) {
          $length = $maxlength;
        }

        
        // set up a counter for how many characters are in the password so far
        $i = 0;
        
        // add random characters to $password until $length is reached
        while ($i < $length) {

          // pick a random character from the possible ones
          $char = substr($possible, mt_rand(0, $maxlength-1), 1);
            
          // have we already used this character in $password?
          if (!strstr($password, $char)) {
            // no, so it's OK to add it onto the end of whatever we've already got...
            $password .= $char;
            // ... and increase the counter by one
            $i++;
          }
        }

        // done!
        return $password;
      }



        if ($_POST["myusername"]<>'') {
            include ("opendb.php");

        $newrandompassword = generatePassword(14);//"Test";
        $ToEmail = mysql_real_escape_string($_POST["myusername"]);
        
        $sql  = "update leden set password='";
        $sql .= hash('sha512',$newrandompassword);
        $sql .= "' where id in (SELECT id FROM emails WHERE email ='$ToEmail');";
        
        // echo $sql."<br>";
        
        if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error());}    
            $EmailSubject    = 'Aanvraag nieuw paswoord schaakclub Post-Gent';
            $mailheader        = "From: admin <'" . $_POST["myusername"]  . "'>" . PHP_EOL;
            $mailheader     .= "Reply-To: ".$_POST["myusername"]. PHP_EOL;
            
            $mailheader .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
            
            $MESSAGE_BODY = "Beste Lid,".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "U vroeg zonet een nieuw paswoord aan.".PHP_EOL;
            $MESSAGE_BODY .= "Uw nieuw paswoord is: ".$newrandompassword.PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Indien U met dit paswoord weer niet kan inloggen neem contact op met Jan.".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Als je geen mail ontvangt moet je eens kijken in je spamfolder of heb je mischien een foutief adres ingegeven.".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Met vriendelijke groeten".PHP_EOL;            $MESSAGE_BODY .= "".PHP_EOL;
            
            $MESSAGE_BODY = nl2br($MESSAGE_BODY);
            
            $mail_sent = @mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("<h2 class='NOK'>Er is een fout opgetreden!<br>Uw nieuw paswoord werd niet verstuurd.</h2>");
            echo "Beste,<br><br>Er werd een mail verstuurd naar het door U opgegeven adres.<br><br>Deze mail bevat Uw nieuwe paswoord. Dit paswoord is 14 karakters lang. Eens ingelogd kan je dit wijzigen door op Uw naam en dan op de knop bewerken te klikken.<br>Dan de 2 paswoord velden onderaan in vullen met een nieuw paswoord naar Uw keuze.<br><br><br><b>clubnaam</b><br><br><br><br>Als het niet lukt mag je me bellen. Je kent mijn nummer.";
        }

        else
        {Echo "<h1>Geen email adres opgegeven</h1>";}?>

    </body>
</html>


De styles moet je zelf maken :)
Ik doe geen controle op email adres. Ze krijgen dus wel een mail maar deze zal niet werken.
De juist link naar je databases zal je ook moeten wijzigen. Deze van mij is een beetje raar omdat ik meerdere emails toesta voor 1 gebruiker dus staan ze in een andere tabel
Gewijzigd op 01/12/2012 07:36:27 door Jan R
 
- Ariën  -
Beheerder

- Ariën -

01/12/2012 09:46:20
Quote Anchor link
Plus dat er een controle op het verzenden van de mail ontbreekt op verkeerde wijze wordt gedaan. Waarom gaat je script dood?
Gewijzigd op 01/12/2012 09:47:34 door - Ariën -
 
Jan R

Jan R

01/12/2012 14:45:39
Quote Anchor link
Waar bedoel je juist. Ik heb toch controle op geen mailadres en
{ die('Error: ' . mysql_error());}

Jan
 
Jules Kreutzer

Jules Kreutzer

02/12/2012 15:15:55
Quote Anchor link
Op een of andere manier krijg ik het script niet werkende...

Dit is de index.php code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form name="forgot_password" class="forgot_password" action="reset.php">
                        <h3>Forgot Password</h3>
                        <div>
                            <label>Email:</label>
                            <input name="email_pass" placeholder="Email" type="text" />
                            <input name="submitted" type="hidden" value="1" />
                            <span class="error">This is an error</span>
                        </div>
                        <div class="bottom">
                            <input type="submit" value="Send reminder"></input>
                            <a href="index.php" rel="login" class="linkform">Suddenly remebered? Log in here</a>
                            <a href="index.php" rel="register" class="linkform">You don't have an account? Register here</a>
                            <div class="clear"></div>
                        </div>
                    </form>


En dan de reset.php code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
require_once('../Connections/contact.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="description" content="test" />
        <meta name="keywords" content="buhh"/>
        <link rel="stylesheet" type="text/css" href="css/login.css" />
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
        <script src="js/cufon-yui.js" type="text/javascript"></script>
        <script src="js/ChunkFive_400.font.js" type="text/javascript"></script>
        <script type="text/javascript">
            Cufon.replace('h1',{ textShadow: '1px 1px #fff'});
            Cufon.replace('h2',{ textShadow: '1px 1px #fff'});
            Cufon.replace('h3',{ textShadow: '1px 1px #000'});
            Cufon.replace('.back');
        </script>
    </head>
    <body>
        <div class="wrapper">
            <div class="content">
                <div id="form_wrapper" class="form_wrapper">
                <form name="forgot_password" class="forgot_password active" action="">
                        <h3>Wachtwoord Reset</h3>
                        <div>
                            <?php
        function RandomPass($length = null) {
            $length = $length < 8 ? 8 : $length;
            for($a=0;$a<$length;$a++) {
                @
$pass .= chr(rand(33, 126));
            }

            return $pass;
        }
        function
generatePassword ($length = 8)  {
        // start with a blank password
        $password = "";

        // define possible characters - any character in this string can be
        // picked for use in the password, so if you want to put vowels back in
        // or add special characters such as exclamation marks, this is where
        // you should do it

        $possible = "012346789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#!%/*-+";

        // we refer to the length of $possible a few times, so let's grab it now
        $maxlength = strlen($possible);
      
        // check for length overflow and truncate if necessary
        if ($length > $maxlength) {
          $length = $maxlength;
        }

        
        // set up a counter for how many characters are in the password so far
        $i = 0;
        
        // add random characters to $password until $length is reached
        while ($i < $length) {

          // pick a random character from the possible ones
          $char = substr($possible, mt_rand(0, $maxlength-1), 1);
            
          // have we already used this character in $password?
          if (!strstr($password, $char)) {
            // no, so it's OK to add it onto the end of whatever we've already got...
            $password .= $char;
            // ... and increase the counter by one
            $i++;
          }
        }

        // done!
        return $password;
      }



        if ($_POST["email_pass"]<>'') { ;

        $newrandompassword = generatePassword(14);//"Test";
        $ToEmail = mysql_real_escape_string($_POST["email_pass"]);
        
        $sql  = "UPDATE users SET password='";
        $sql .= md5($newrandompassword);
        $sql .= "' WHERE id in (SELECT id FROM users WHERE email ='$ToEmail');";
        
        // echo $sql."<br>";
        
        if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error());}    
            $EmailSubject    = 'Aanvraag nieuw wachtwoord';
            $mailheader        = "From: admin <'" . $_POST["myusername"]  . "'>" . PHP_EOL;
            $mailheader     .= "Reply-To: ".$_POST["email_pass"]. PHP_EOL;
            
            $mailheader .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
            
            $MESSAGE_BODY = "Beste,".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Zojuist is uw email adres opgegeven om een nieuw wachtwoord te ontvangen voor het betreffende account. U kunt het nieuwe wachtwoord in deze mail vinden.".PHP_EOL;
            $MESSAGE_BODY .= "Uw nieuw paswoord is: ".$newrandompassword.PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Indien u niet kunt inloggen met bovenstaand wachtwoord, vragen wij u om contact op te nemen met onze helpdesk.".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "".PHP_EOL;
            $MESSAGE_BODY .= "Met vriendelijke groeten".PHP_EOL;            $MESSAGE_BODY .= "".PHP_EOL;
            
            $MESSAGE_BODY = nl2br($MESSAGE_BODY);
            
            $mail_sent = @mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("<h2 class='NOK'>Er is een fout opgetreden!<br>Uw nieuw paswoord werd niet verstuurd.</h2>");
            echo "Beste,<br><br>Er werd een mail verstuurd naar het door U opgegeven adres.<br><br>Deze mail bevat Uw nieuwe paswoord. Dit paswoord is 14 karakters lang. Eens ingelogd kan je dit wijzigen door op Uw naam en dan op de knop bewerken te klikken.<br>Dan de 2 paswoord velden onderaan in vullen met een nieuw paswoord naar Uw keuze.<br><br><br><b>clubnaam</b><br><br><br><br>Als het niet lukt mag je me bellen. Je kent mijn nummer.";
        }

        else
        {
            echo "<p align='center' style='color:red'>Geen email adres opgegeven</p>";}?>
</div>
                    </form>
                        <br />
                        <a style="color:#f90" href="index.php">Inloggen</a></div>
                <div class="clear"></div>
            </div>
        </div>
        

        <!-- The JavaScript -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                    //the form wrapper (includes all forms)
                
...


Wanneer ik op index.php mijn email adres invul, wordt ik naar reset.php gestuurd met de melding dat ik geen email adres heb opgegeven.
Gewijzigd op 02/12/2012 15:16:48 door Jules Kreutzer
 
- Ariën  -
Beheerder

- Ariën -

02/12/2012 16:30:16
Quote Anchor link
Dan is je query mislukt, en toon je dus de verkeerde melding. Als je wilt kijken of er een mailadres is ingegeven (POST/GET?), dan moet je met isset() kijken, nadat je query gelukt is.
 
Jan R

Jan R

03/12/2012 11:19:07
Quote Anchor link
Je werkt nog steeds met mijn query. Je moet deze aanpassen voor jou db.
Gezien we geen structuur hebben van jouw db kunnen wij het niet doen

Jan
 
Jules Kreutzer

Jules Kreutzer

03/12/2012 14:36:12
Quote Anchor link
de connections:contact.php ziet er zo uit:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

$hostname_contact = "localhost";
$database_contact = "jules_cms";
$username_contact = "jules";
$password_contact = "blabla";
$contact = mysql_connect($hostname_contact, $username_contact, $password_contact) or trigger_error(mysql_error(),E_USER_ERROR);
?>


- Aar -, je bedoelt dat regel 77 van reset.php moet worden veranderd naar
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php if (!isset($_POST['email_pass'])) { ?>
?
Gewijzigd op 03/12/2012 14:39:40 door Jules Kreutzer
 
- Ariën  -
Beheerder

- Ariën -

03/12/2012 14:38:07
Quote Anchor link
Nee, die die() is onnodig, en graag zou het handig zijn als je de relevante database-structuur kan tonen.
 
Jules Kreutzer

Jules Kreutzer

03/12/2012 14:56:51
Quote Anchor link
Ik heb de kolommen id; usernaam; password; email; Voornaam; Achternaa,; website; updates; level; key; in mijn database staan.

Ter informatie: level kan member of admin zijn, updates dient voor nieuwsbrief ja/nee

Ik zit nu op school en het is me even te lastig om een screenshot bij te voegen. Ik kan wanneer dat makkelijker is straks een screenshot van mijn database sturen.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.