Email verificatie
Ik ben bezig met een simpel registratie scriptje waar een email verificatie aan verbonden zit.
Hij gaat alleen steeds naar de melding: Geen geldig emailadres.
Terwijl het gewoon een geldig adres is.
Ziet iemand wat ik fout doe?
Ik werk op localhost met WAMP
Dit is mijn code.
Code (php)
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
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
<?php
include 'includes/header.php';
include 'connection/connection.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) && !empty($_POST['address']) && isset($_POST['address']) && !empty($_POST['postcode']) && isset($_POST['postcode']) && !empty($_POST['plaats']) && isset($_POST['plaats']) && !empty($_POST['land']) && isset($_POST['land']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
$address=mysqli_real_escape_string($connection,$_POST['address']);
$postcode=mysqli_real_escape_string($connection,$_POST['postcode']);
$plaats=mysqli_real_escape_string($connection,$_POST['plaats']);
$land=mysqli_real_escape_string($connection,$_POST['land']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation,address,postcode,plaats,land) VALUES('$email','$password','$activation','$address','$postcode','$plaats','$land')");
// sending email
include 'includes/Send_Mail.php';
$to=$email;
$subject="Email verificatie";
$body='Beste, <br/> <br/> Bedankt voor je aanmelding bij website! Klik op de volgende link om uw account te activeren. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>';
Send_Mail($to,$subject,$body);
$msg= "U bent geregistreerd, check alstublieft uw email voor de verificatiemail.";
}
else
{
$msg= 'Dit email adres is al in gebruik.';
}
}
else
{
$msg = 'Dit is geen geldig email adres.';
}
}
// HTML Part
?>
include 'includes/header.php';
include 'connection/connection.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) && !empty($_POST['address']) && isset($_POST['address']) && !empty($_POST['postcode']) && isset($_POST['postcode']) && !empty($_POST['plaats']) && isset($_POST['plaats']) && !empty($_POST['land']) && isset($_POST['land']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
$address=mysqli_real_escape_string($connection,$_POST['address']);
$postcode=mysqli_real_escape_string($connection,$_POST['postcode']);
$plaats=mysqli_real_escape_string($connection,$_POST['plaats']);
$land=mysqli_real_escape_string($connection,$_POST['land']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation,address,postcode,plaats,land) VALUES('$email','$password','$activation','$address','$postcode','$plaats','$land')");
// sending email
include 'includes/Send_Mail.php';
$to=$email;
$subject="Email verificatie";
$body='Beste, <br/> <br/> Bedankt voor je aanmelding bij website! Klik op de volgende link om uw account te activeren. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>';
Send_Mail($to,$subject,$body);
$msg= "U bent geregistreerd, check alstublieft uw email voor de verificatiemail.";
}
else
{
$msg= 'Dit email adres is al in gebruik.';
}
}
else
{
$msg = 'Dit is geen geldig email adres.';
}
}
// HTML Part
?>
Dit is de connection file
Code (php)
Activation.php
Code (php)
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
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
<?php
include '../connection/connection.php';
$msg='';
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=mysqli_real_escape_string($connection,$_GET['code']);
$c=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code'");
if(mysqli_num_rows($c) > 0)
{
$count=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code' and status='0'");
if(mysqli_num_rows($count) == 1)
{
mysqli_query($connection,"UPDATE users SET status='1' WHERE activation='$code'");
$msg="Uw account is geactiveerd!";
}
else
{
$msg ="Uw account is al actief, u hoeft niet meer te activeren.";
}
}
else
{
$msg ="Verkeerde activatiecode.";
}
}
?>
include '../connection/connection.php';
$msg='';
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=mysqli_real_escape_string($connection,$_GET['code']);
$c=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code'");
if(mysqli_num_rows($c) > 0)
{
$count=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code' and status='0'");
if(mysqli_num_rows($count) == 1)
{
mysqli_query($connection,"UPDATE users SET status='1' WHERE activation='$code'");
$msg="Uw account is geactiveerd!";
}
else
{
$msg ="Uw account is al actief, u hoeft niet meer te activeren.";
}
}
else
{
$msg ="Verkeerde activatiecode.";
}
}
?>
Het email script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
function Send_Mail($to,$subject,$body)
{
require '../phpmailer/class.phpmailer.php';
$from = "[email protected]";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.yourwebsite.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "SMTP_Username"; // SMTP username
$mail->Password = "SMTP_Password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
function Send_Mail($to,$subject,$body)
{
require '../phpmailer/class.phpmailer.php';
$from = "[email protected]";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.yourwebsite.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "SMTP_Username"; // SMTP username
$mail->Password = "SMTP_Password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
En als laatste het formulier:
Code (php)
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
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
<div class="grid_6">
<h2><span>Registreren</span></h2>
<form class="form" method="post">
<div class="inputgroup">
<label for="emailadres">Emailadres:</label>
<input name="email" id="emailadres" value="" placeholder="Email-adres" type="text">
</div>
<div class="inputgroup">
<label for="wachtwoord">Wachtwoord:</label>
<input name="password" id="wachtwoord" value="" placeholder="Wachtwoord" type="password">
</div>
<div class="inputgroup">
<label for="adres">Adres:</label>
<input name="address" id="adres" value="" placeholder="Adres" type="text">
</div>
<div class="inputgroup">
<label for="postcode">Postcode:</label>
<input name="postcode" id="postcode" value="" placeholder="Postcode" type="text">
</div>
<div class="inputgroup">
<label for="plaats">Plaats:</label>
<input name="plaats" id="plaats" value="" placeholder="Plaats" type="text">
</div>
<div class="inputgroup">
<label for="land">Land:</label>
<input name="land" id="land" value="" placeholder="Land" type="text">
</div>
<input type="submit" class="button" value="Registration" />
<span class='msg'><?php echo $msg; ?></span>
</div>
[/CODE]
<h2><span>Registreren</span></h2>
<form class="form" method="post">
<div class="inputgroup">
<label for="emailadres">Emailadres:</label>
<input name="email" id="emailadres" value="" placeholder="Email-adres" type="text">
</div>
<div class="inputgroup">
<label for="wachtwoord">Wachtwoord:</label>
<input name="password" id="wachtwoord" value="" placeholder="Wachtwoord" type="password">
</div>
<div class="inputgroup">
<label for="adres">Adres:</label>
<input name="address" id="adres" value="" placeholder="Adres" type="text">
</div>
<div class="inputgroup">
<label for="postcode">Postcode:</label>
<input name="postcode" id="postcode" value="" placeholder="Postcode" type="text">
</div>
<div class="inputgroup">
<label for="plaats">Plaats:</label>
<input name="plaats" id="plaats" value="" placeholder="Plaats" type="text">
</div>
<div class="inputgroup">
<label for="land">Land:</label>
<input name="land" id="land" value="" placeholder="Land" type="text">
</div>
<input type="submit" class="button" value="Registration" />
<span class='msg'><?php echo $msg; ?></span>
</div>
[/CODE]
Gewijzigd op 26/05/2015 17:15:27 door Snelle Jaap
Het probleem zit hem in de reguliere expressie:
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$
Ik weet niet wat je precies invoert, maar PHP kan het zelf ook goed controleren via een filter_var.
Deze functie kan ik zeker aanraden.
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
$email = "[email protected]"
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo "Mailadres is ongeldig";
} else {
echo "Mailadres is geldig.";
}
?>
$email = "[email protected]"
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo "Mailadres is ongeldig";
} else {
echo "Mailadres is geldig.";
}
?>
Gewijzigd op 26/05/2015 17:23:30 door - Ariën -
Wat is het probleem daar mee dan? Zo heb ik het altijd gedaan. Ik voer gewoon een normaal outlook adres in.
Toevoeging op 27/05/2015 13:32:12:
Laat maar het was een fout van mijn kant, de code werkt wel.
Hij pakt alleen het verzenden van de email niet. Weet iemand hoe je dit met wamp kunt doen vanaf een locale ontwikkelomgeving?
Fatal error: Class 'SMTP' not found in C:\wamp\www\website\phpmailer\class.phpmailer.php on line 1290
Call Stack
Is wat ik krijg als ik registreer.
En dit is de code die op die regel staat.
public function getSMTPInstance()
{
if (!is_object($this->smtp)) {
$this->smtp = new SMTP;
}
return $this->smtp;
}
Hij vind dus geen SMTP gegevens, wat zou ik hiervoor in moeten voeren in mijn Send_Mail.php ?
PHPMailer bestaat uit meerdere files. Uit mijn hoofd 3 (los van de voorbeelden)
class.phpmailer.php is er 1, maar er zijn dus nog 2. Niet altijd nodig, maar nu kennelijk wel.
Los van dat jouw controle voor een "normaal outlook adres" altijd werkt, kan je zo al zien dat je code al jaren niet goed is:
'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
je ziet hier dat je adres altijdk moet eindigen op een punt gevolgd door 2, 3 of 4 letters.
bijvoorbeeld .nl, .com of .info
Maar het al jaren bestaande .travel of .museum, komt niet door je check.
Laat staan dat recente adressen als [email protected] nog in je eis van 4 letters tld passen.
Toevoeging op 27/05/2015 14:54:44:
er klopt wel meer niet aan je controle:
[email protected] zou een geldig adres zijn?
En heinz@köln.de lukt ook weer niet. Een mail adres kun je (zelf als je de recentste tld's en de niet-us-alfabet-letters negeert) bijna niet in een regex controleren.
http://wiki.pfz.nl/invoer-validatie/#regular-expressions-is-het-een-geldig-email-adres
Nou, om te beginnen accepteert jouw regex een heleboel van de nieuwe TLD's niet (zoals .museum en .amsterdam; eigenlijk elke TLD van meer dan 4 letters). Daarnaast mag bij jouw regex een hostname of subdomein beginnen of eindigen met een - en dat is niet conform specificaties. Een adres als [email protected] komt gewoon door jouw validator heen.
Een reguliere expressie die valideert conform RFC-5322 zou er ongeveer als volgt uit moeten zien:
Code (php)
1
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
filter_var() lijkt me handiger. ;-)
Bedankt voor de hulp, ik heb de juiste class nu ook geinclude en hij geeft aan dat het lukt. Alleen krijg ik geen mail binnen, best logisch omdat ik nog niks met de SMTP gegevens heb gedaan.
Hoe kan ik via localhost (WAMP) toch die mailfunctie testen?
Of gewoon PHPmailer gebruiken.
Wat bedoel je met gewoon PHPmailer gebruiken? Dat gebruik ik nu.
Het flexibleste ben je denk ik met de laatste oplossing.
mysql_*, mysqli_*, etc is gemarkeerd als depreciated met een goede reden.
Wachtwoorden encrypten met md5 is erg onveilig en ik mis ook een salt key.
Stel dat je een SQL injectie op je website krijgt en je user table krijgt men in handen via API's kan men binnen een paar seconden al jouw leden hun wachtwoorden ontravelen door MD5 hashes om toveren naar de echte wachtwoorden met het gebruik van rainbow tables. Meer informatie hier: http://md5.gromweb.com/
Met PDO hoef je je ook geen zorgen meer te maken om dingen te "escapen".
$password=mysqli_real_escape_string($connection,$_POST['password']);
je password mogelijk anders dan dat het ingevoerd is. Bijvoorbeeld de pass "ge'heim" wordt "ge\'heim"
Als je daarna md5($password) doet, krijg je een heel andere hash...
@Johan K mysqli_* is toch niet depreciated?? Of heb ik dat verkeerd.
mysqli_* is niet deprecated. mysql_* daarintegen wel.
mysqli_real_escape_string zet er quotes bij dacht ik en daardoor klopt het niet meer
Quotes? Nee, mysql gebruikt de \ als escapekarakter
Ivo P op 27/05/2015 17:14:37:
Sowieso maak je met de regel
$password=mysqli_real_escape_string($connection,$_POST['password']);
je password mogelijk anders dan dat het ingevoerd is. Bijvoorbeeld de pass "ge'heim" wordt "ge\'heim"
Als je daarna md5($password) doet, krijg je een heel andere hash...
$password=mysqli_real_escape_string($connection,$_POST['password']);
je password mogelijk anders dan dat het ingevoerd is. Bijvoorbeeld de pass "ge'heim" wordt "ge\'heim"
Als je daarna md5($password) doet, krijg je een heel andere hash...
Nice catch. Escapen zou altijd de laatste handeling moeten zijn, op het moment dat je deze (user) DATA in de query-string (SQL) opneemt inderdaad...
Tevens:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
$address=mysqli_real_escape_string($connection,$_POST['address']);
$postcode=mysqli_real_escape_string($connection,$_POST['postcode']);
$plaats=mysqli_real_escape_string($connection,$_POST['plaats']);
$land=mysqli_real_escape_string($connection,$_POST['land']);
?>
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
$address=mysqli_real_escape_string($connection,$_POST['address']);
$postcode=mysqli_real_escape_string($connection,$_POST['postcode']);
$plaats=mysqli_real_escape_string($connection,$_POST['plaats']);
$land=mysqli_real_escape_string($connection,$_POST['land']);
?>
Aint nobody got time for that. Schrijf een wrapper oid, dit kan korter natuurlijk.
Gewijzigd op 27/05/2015 21:23:38 door Thomas van den Heuvel