PHP/MySQLi Creating a Forum - Part 7 - Subscriptions #2

PHP/MySQLi Creating a Forum - Part 6 - Subscriptions #1 Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this seventh part, we are carrying on with the subscription feature. Pre-creation: First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities. Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page. Obviously you will also need to go through the first, second, third, fourth, fifth and sixth parts of this tutorial series which can all be found on my profile tracking page. Please Note: The way that we are going to be doing our subscription service is the user will receive an email each time a new reply is added to the specific thread, this avoids us having to use a CronJob etc. to send one email every hour or so (if applicable). One last thing; we will be using the email from a given $_SESSION['email']; variable, simply because I don't have a user account table set up with this project just yet. If you know anything about mylsqi and php you should be able to amend this script to your liking (such as grabbing the emails from the users table within your database). subPage.php Editing: First we want to check for all the information and validate it. Once we are sure the email has at least one '.' and '@' sign(s) we check if the email has already subscribed to the given thread ID, if it hasn't, we add it to our database. We also output the information...
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial');
  3. if (isSet($_POST['subscribe']) && isSet($_GET['tid']) && isSet($_POST['email'])) {
  4. $email = $_POST['email'];
  5. $id = $_GET['tid'];
  6. $hasAt = false;
  7. $hasDot = false;
  8. for ($i=0;$i<strlen($email);$i++){
  9. if ($email[$i] == '@')
  10. $hasAt = true;
  11. if ($email[$i]== '.')
  12. $hasDot = true;
  13. }
  14. if ($hasAt && $hasDot) {
  15. $q = mysqli_query($con, "SELECT * FROM `subscriptions` WHERE `email`='$email' AND `threadID`='$id'");
  16. if (mysqli_num_rows($q) <= 0) {
  17. $qu = mysqli_query($con, "INSERT INTO `subscriptions` VALUES ('', '$id', '$email')");
  18. if ($qu) {
  19. echo 'Successfully Subscribed.';
  20. }else
  21. echo 'Failed to subscribe to the given thread ID with the given email.';
  22. }else
  23. echo 'The given email is already subscribed to that thread ID!';
  24. }
  25. }
  26. ?>
Adding Replies (email sending): Finally we want to send an email to everyone subscribed to the given thread ID once a new reply has been added. So... where we add the new reply to our replies table within our database, we want to send an email to everyone listed in the subscriptions database with our current thread ID...
  1. $qu = mysqli_query($con, "SELECT * FROM `subscriptions` WHERE `threadID`='$thread'");
  2. if (mysqli_num_rows($qu) > 0) {
  3. $msg = 'A new reply has been submitted to a thread you have subscribed to. '.$user.' had this to say:<br/>'.$cont;
  4. while ($row = mysqli_fetch_array($qu)) {
  5. mail($row['email'], 'New Reply', $msg);
  6. }
  7. }

Add new comment