-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
136 lines (109 loc) · 4.26 KB
/
register.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
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
128
129
130
131
132
133
134
135
136
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include 'includes/session.php';
if(isset($_POST['signup'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['email'] = $email;
if(!isset($_SESSION['captcha'])){
require('recaptcha/src/autoload.php');
$recaptcha = new \ReCaptcha\ReCaptcha('6LevO1IUAAAAAFCCiOHERRXjh3VrHa5oywciMKcw', new \ReCaptcha\RequestMethod\SocketPost());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()){
$_SESSION['error'] = 'Please answer recaptcha correctly';
header('location: signup.php');
exit();
}
else{
$_SESSION['captcha'] = time() + (10*60);
}
}
if($password != $repassword){
$_SESSION['error'] = 'Passwords did not match';
header('location: signup.php');
}
else{
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT COUNT(*) AS numrows FROM users WHERE email=:email");
$stmt->execute(['email'=>$email]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Email already taken';
header('location: signup.php');
}
else{
$now = date('Y-m-d');
$password = password_hash($password, PASSWORD_DEFAULT);
//generate code
$set='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code=substr(str_shuffle($set), 0, 12);
try{
$stmt = $conn->prepare("INSERT INTO users (email, password, firstname, lastname, activate_code, created_on) VALUES (:email, :password, :firstname, :lastname, :code, :now)");
$stmt->execute(['email'=>$email, 'password'=>$password, 'firstname'=>$firstname, 'lastname'=>$lastname, 'code'=>$code, 'now'=>$now]);
$userid = $conn->lastInsertId();
$message = "
<h2>Thank you for Registering.</h2>
<p>Your Account:</p>
<p>Email: ".$email."</p>
<p>Password: ".$_POST['password']."</p>
<p>Please click the link below to activate your account.</p>
<a href='http://localhost/ecommerce/activate.php?code=".$code."&user=".$userid."'>Activate Account</a>
";
//Load phpmailer
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mysourcepass';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('[email protected]');
//Recipients
$mail->addAddress($email);
$mail->addReplyTo('[email protected]');
//Content
$mail->isHTML(true);
$mail->Subject = 'ECommerce Site Sign Up';
$mail->Body = $message;
$mail->send();
unset($_SESSION['firstname']);
unset($_SESSION['lastname']);
unset($_SESSION['email']);
$_SESSION['success'] = 'Account created. Check your email to activate.';
header('location: signup.php');
}
catch (Exception $e) {
$_SESSION['error'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
header('location: signup.php');
}
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
header('location: register.php');
}
$pdo->close();
}
}
}
else{
$_SESSION['error'] = 'Fill up signup form first';
header('location: signup.php');
}
?>