How to send an email notification when a page is visited?

14,397

Solution 1

Try:

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('[email protected]', 'My Subject', $message);

// Redirect
header('Location: anotherpage.php');
?>

Solution 2

Yes, the mail function.

But I think you should reconsider this design - imagine what will happen if the page suddently gets hit by thousands upon thousands of users - or a buggy web crawler. A log file or database update is alot easier to handle.

Share:
14,397
detonate
Author by

detonate

Eager to learn how to code!

Updated on June 04, 2022

Comments

  • detonate
    detonate almost 2 years

    Is there a way to send an email notification if a certain page is visited?
    (Triggers an email saying the page was viewed)

    Ie. User comes to this page: thank-you.php
    And email is automatically sent to the admin of the website upon page load.
    Then the user gets redirected to another page right after this email trigger as been sent.

    EDIT --- I would need to check if a user came from a specific domain name or URL.
    This is to avoid exploits or other misc. submission hacks.

    Any suggestions?

  • detonate
    detonate about 13 years
    This is one of the things Anders. I would need to check if a user came from a specific domain name. Would this be secure enough?
  • Anders Lindahl
    Anders Lindahl about 13 years
    Ask the person who will have to handle the mails, and ask yourself what the person will do with the mail. Is it a trigger to a manual routine that should be automated aswell?
  • detonate
    detonate about 13 years
    No, just to notify that someone visited this page via email.
  • Anders Lindahl
    Anders Lindahl about 13 years
    You should plan for mail flooding at the design phase, and try to group the messages in a way that satisfies both the admins request for feedback with the potential millions-of-mails scenario. How about a combined report every (week|day|businessday|hour|15 minutes)?
  • detonate
    detonate about 13 years
    A daily report seems like a good idea. The email flood is definitely something to consider here. How would you put all of this together Anders? (Check for domain name referrer and send a notification once a day).
  • Anders Lindahl
    Anders Lindahl about 13 years
    Depends on how much information is needed - a simple cron job greping in the web server log might be enough, but you could also go for one of the many web log analyzers: en.wikipedia.org/wiki/List_of_web_analytics_software
  • detonate
    detonate about 13 years
    Anders, is there a way to simplify this. Avoiding cron jobs and log analyzers. I really want to keep this simple but functional.
  • detonate
    detonate about 13 years
    Richard, is there a way to add this in a condition that checks if the URL referrer meets a certain domain name?
  • detonate
    detonate about 13 years
    Jed, Made and edit to my question. Is there a way to add this in a condition that checks if the URL referrer meets a certain domain name?
  • AndersTornkvist
    AndersTornkvist about 13 years
    @detonate Yes, this is possible. You could use: if (isset($_SERVER['HTTP_REFERER']) && preg_match('/^http:\\/\\/www\\.yourdomain.com\\//',$_SERVER[‌​'HTTP_REFERER'])). Remember that this is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. Some antivirus software will delete the HTTP_REFERER. In short, it cannot really be trusted.
  • detonate
    detonate about 13 years
    ok thanks for the tip Richard. Would you have another alternative suggestion?
  • AndersTornkvist
    AndersTornkvist about 13 years
    Depending on the purpose, I would recommend mailing on a weekly basis. If cron isn't an alternative, you could use a script to append the visit to a text file and then send and delete the text file on the first visit the next week. Simply load the $message from the text file with file_get_contents().
  • detonate
    detonate about 13 years
    Interesting, I've never worked with cron... this might take me too long to implement and deploy. I wish we could send something that would alert via an email notification without writing up to a text file etc. Never thought this would've become such a task!
  • detonate
    detonate about 13 years
    This is a good start, I'll have to look into stopping floods or other crawlers... any other suggestions would be nice!
  • AndersTornkvist
    AndersTornkvist about 13 years
    @detonate You could also use HTTP_USER_AGENT to distinguish between crawlers and users. To improve my answer, I need some more information. If we suppose that there are 20 visitors every day, how many e-mails would you like during a week?