-
First, you will need to download the SMTP PEAR module files required. Refer to this page for instructions on setting it up on your NginX AWS Lightsail web server.
-
Creating a database and table for page likes using MySQL Workbench:
- Create a Database:
Manually:
- Open MySQL Workbench and connect to your MySQL server.
- In the navigator panel, right-click the "schemas" tab.
- Select "Create Schema."
- Provide a schema name (e.g., "csx2") and click "Apply" to create the new database.
Using Commands:
CREATE DATABASE csx2;
- Create a Table:
Manually:
- Under the appropriate database in the left navigation pane, right-click "Tables" and select "Create Table..."
- Enter the table name (e.g., "email").
- Add columns: "id", "email" and "verified" with their respective data types and any other necessary details.
- Click "Apply" to create the table.
Using Commands:
CREATE TABLE `csx2`.`likes` (
`pageName` VARCHAR(50) NULL,
`count` BIGINT NOT NULL
);
// Show value command in MySQL Workspace
USE csx2; SELECT count FROM likes WHERE pageName = 'page1';
Extending Email Subscription Code from section 2, we can enhance our email subscription functionality. Here’s how it works:
- User Submission: When a user submits their email, we store it in our database for email subscriptions.
- Verification Process: Each entry in the database is assigned a verification value (a boolean, either 0 or 1). Initially, this value is set to 0.
- User Verification: When the user clicks the verification link in their email inbox, they are directed to the verify.php page.
- Comparison: Our PHP code on the verify.php page compares the MD5 hash of the user’s email with the stored hash.
- Update: Update: If the hashes match, we change the verification value for that email to 1.
This approach ensures secure and verified email subscriptions.
action_page.php
<?php
require_once "Mail.php";
$servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
$username = "{USERNAME-FOR-DATABASE}";
$password = "{PASSWORD-FOR-DATABASE}";
$dbname = "{DATABASE-NAME}";
$vemail = $_POST['email'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "INSERT INTO subs (email)
VALUES ('{$vemail}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
// Send verification email
$verificationCode = md5($vemail);
$verificationLink = "https://computersciencex.com/verify.php?code=$verificationCode";
$subject = "Verify Your Email Address";
$message = "Click the link below to verify your email address:\n$verificationLink";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$from = "admin@computersciencex.com";
$to = $vemail;
$host = "ssl://smtp.zoho.eu";
$port = "465";
$username = 'admin@computersciencex.com';
$password = '{PASSWORD}';
$body = $message;
$headers = array ('From' => $from, 'To' => $to,'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}
header("Location: https://computersciencex.com/indexSuccess.html", true, 301);
$conn->close();
?>
-
Now create a php file called verify.php and add the following code(ensuring that you change the variable values to your own):
verify.php
<?php
$servername = "{ENDPOINT-TO-MYSQL-DATABASE}";
$username = "{USERNAME-FOR-DATABASE}";
$password = "{PASSWORD-FOR-DATABASE}";
$dbname = "{DATABASE-NAME}";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
if (isset($_GET['code'])) {
$code = $_GET['code'];
// Update user status to verified
$sql = "UPDATE subs SET verified = 1 WHERE MD5(email) = '$code'";
if ($conn->query($sql) === TRUE) {
header("Location: https://computersciencex.com/indexVerify.html", true, 301);
} else {
echo "Error verifying email: " . $conn->error;
}
}
$conn->close();
?>