-2

I have a user login/registration system on my simple site everything works great but I would like to have a function that sends a generic email to the user when the logout button is clicked. probably saying something like please send us an email regarding your findings on our site.

the site is an entertainment agency that books out Carnival equipment, registration and login is purely for users to view prices should they be interested in the actual product.

i have no idea where or how to implement anything like this.

my understanding is i must create a query to the databases requesting email from email column and if user email address exist proceed with the email sending (if they are at the point of logout then that means email address is available in database) then once it is sent log user out and redirect to a page. i just dont know how to do the query exactly.

any advice would be much appreciated

ps, I don't know what documentation you would need to see in order to see what i have done?

this is my logout page:

<?php
require_once 'db.php';
session_start();

if (isset($_SESSION['username']) && !empty($_SESSION['username'])):
    if (isset($_GET['action']) && !empty($_GET['action'])) {

        $action = $_GET['action'];

        if ($action == "logout") {
            $stmt  = $pdo->prepare("SELECT email FROM users WHERE email = ? ");
            $stmt->execute([$_SESSION['username']]);
            $email = $stmt->fetchColumn();

            $message = "Hellow you just used our website....";

            if (mail($email, "Feedback", $message)) {
                // the email is sent now log the user out.
                session_destroy();
                header("location:login_page.php");
                exit();
            }

        }

    }
endif;
?>
  • 1
    Well, I think you should store user's information such as (email, name , etc) on a session variable after your user login, cause i imagine when you make a query to checks if that user exists on your site you can before login save that info in that session variable and then use it wherever you want – kikemnzz Jul 18 '17 at 11:02
  • Are you working on core php or any MVC framework...? – GYaN Jul 18 '17 at 11:04

2 Answers2

0

You can follow the following steps to accomplish that Step 1: When the user logs in, save his Email ID in the session. I.e When the login ios successful you must be sending some "Success" response and redirecting to the user page.So, before doing that, if the login is successful create a session and store the Email Id in it. login.php

<?php 
session_start();
//Some database actions...

if($login == "success"){  // Just an example
    $_SESSION["email"] = $EMAIL // This is the user Email Address.
}

?>

Step 2:

When the user logs out, he must be redirected to the logout page where you destroy all the sessions.You'll have to call the trigger mail function in that function. In logout.php

<?php
session_start();

$EMAIL = $_SESSION["email"] // Get the previously saved email in login process.

triggerMail($EMAIL);


session_unset();
session_destroy();

header("location:home.php");
exit();


function triggerMail($email){

//Your email logic.. return either true or false.

}
?>
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21
  • @kikemnzz thank you for the advice. I had that theory in mind just did not know how to do it. – silent-noob Jul 18 '17 at 11:21
  • thank you i will implement your suggestion into my current login page and then do the logout step and hopefully i do it correctly. – silent-noob Jul 18 '17 at 11:22
0

When the user successful log's in if they are using their email address to login then store that email address on a session. Then on logout before destroying the session send the email to the email stored in the session.

Then have a logout link like this :

<a href="logoutpage.php?action=logout">Logout</a>

then you can verify the action on that logoutpage if user wanna logout.

<?php
session_start();
if (isset($_SESSION['currentUser']) && !empty($_SESSION['currentUser'])):

    if (isset($_GET['action']) && !empty($_GET['action'])) {

        $action = $_GET['action'];

        if ($action == "logout") {
            $message = "Hellow you just used our website.... what ever you want to write";

            if (mail($_SESSION['currentUser'], "Feedback", $message)) {
                // the email is sent now log the user out.
                session_destroy();
                header("location:loginPage.php");
                exit();
            }

        }

    }
endif;
?>

NB : $_SESSION['currentUser'] is the session you created when the user was logging and stored their email.

Well if you not using email address as username for login then then you can query the email address before user log's out. then send email then logout.

<?php
session_start();

if (isset($_SESSION['currentUser']) && !empty($_SESSION['currentUser'])):
    if (isset($_GET['action']) && !empty($_GET['action'])) {

        $action = $_GET['action'];

        if ($action == "logout") {
            $stmt  = $dbh->prepare("SELECT email FROM users WHERE userID = ? ");
            $stmt->execute([$_SESSION['currentUser']]);
            $email = $stmt->fetchColumn();

            $message = "Hellow you just used our website....";

            if (mail($email, "Feedback", $message)) {
                // the email is sent now log the user out.
                session_destroy();
                header("location:loginPage.php");
                exit();
            }

        }

    }
endif;
?>

I' m not quit sure which API you are using MYSQLI or PDO as you did not specify much on your question, the above uses PDO.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • I am using PDO, I was not sure what info to provide as i did not quite understand myself how to explain what i was trying to do – silent-noob Jul 18 '17 at 11:23
  • is there a reason why you are using $_GET? why im asking is everywhere i read people say get is unsafe and $_POST should be used instead. sorry for the noob question. – silent-noob Jul 18 '17 at 11:33
  • i get this error Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing in D:\xamp\htdocs\test\logout.php on line 17 when i run the code in your help script above. i changed details above to match my current details – silent-noob Jul 18 '17 at 12:08
  • follow here : https://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost – Masivuye Cokile Jul 18 '17 at 12:11