forked from jaiswalaman/Online-Notes-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateemail.php
51 lines (40 loc) · 1.91 KB
/
updateemail.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
<?php
//start session and connect
session_start();
include ('connection.php');
//get user_id and new email sent through Ajax
$user_id = $_SESSION['user_id'];
$newemail = $_POST['email'];
//check if new email exists
$sql = "SELECT * FROM users WHERE email='$newemail'";
$result = mysqli_query($link, $sql);
$count = $count = mysqli_num_rows($result);
if($count>0){
echo "<div class='alert alert-danger'>There is already as user registered with that email! Please choose another one!</div>"; exit;
}
//get the current email
$sql = "SELECT * FROM users WHERE user_id='$user_id'";
$result = mysqli_query($link, $sql);
$count = mysqli_num_rows($result);
if($count == 1){
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
$email = $row['email'];
}else{
echo "<div class='alert alert-danger'>There was an error retrieving the email from the database</div>";exit;
}
//create a unique activation code
$activationKey = bin2hex(openssl_random_pseudo_bytes(16));
//insert new activation code in the users table
$sql = "UPDATE users SET activation2='$activationKey' WHERE user_id = '$user_id'";
$result = mysqli_query($link, $sql);
if(!$result){
echo "<div class='alert alert-danger'>There was an error inserting the user details in the database.</div>";exit;
}else{
//send email with link to activatenewemail.php with current email, new email and activation code
$message = "Please click on this link prove that you own this email:\n\n";
$message .= "http://mynotes.thecompletewebhosting.com/activatenewemail.php?email=" . urlencode($email) . "&newemail=" . urlencode($newemail) . "&key=$activationKey";
if(mail($newemail, 'Email Update for you Online Notes App', $message, 'From:'.'[email protected]')){
echo "<div class='alert alert-success'>An email has been sent to $newemail. Please click on the link to prove you own that email address.</div>";
}
}
?>