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.
Comments
-
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 about 13 yearsThis 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 about 13 yearsAsk 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 about 13 yearsNo, just to notify that someone visited this page via email.
-
Anders Lindahl about 13 yearsYou 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 about 13 yearsA 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 about 13 yearsDepends on how much information is needed - a simple cron job
grep
ing 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 about 13 yearsAnders, is there a way to simplify this. Avoiding cron jobs and log analyzers. I really want to keep this simple but functional.
-
detonate about 13 yearsRichard, is there a way to add this in a condition that checks if the URL referrer meets a certain domain name?
-
detonate about 13 yearsJed, 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 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 about 13 yearsok thanks for the tip Richard. Would you have another alternative suggestion?
-
AndersTornkvist about 13 yearsDepending 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 withfile_get_contents()
. -
detonate about 13 yearsInteresting, 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 about 13 yearsThis is a good start, I'll have to look into stopping floods or other crawlers... any other suggestions would be nice!
-
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?