Google Answers Logo
View Question
 
Q: PHP Script Needed! ( Answered,   2 Comments )
Question  
Subject: PHP Script Needed!
Category: Computers > Programming
Asked by: jellybeans-ga
List Price: $25.00
Posted: 31 Jan 2004 17:13 PST
Expires: 01 Mar 2004 17:13 PST
Question ID: 302278
Hi! This question is open to all:

I would like a simple PHP script that emails me notification of a
preset HTTP_USER_AGENT visitor hitting a page on my website.

For example, let's say I want to be notified via email that the
following user agent visited a page on my website:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sbcydsl 3.12)

The script needs to check the value of each visitors HTTP USER AGENT
value, and compare it with the above value (presumably stored
somewhere in the script).

If it matches, then it needs to send me an email. Exactly what the
email says I don't really care.  If it does not match, then it needs
to do nothing.

Hoping someone can cook this up, good tip for working script.

P.S.   I would like to call the script using a <img
src="http://www.website.com/script.php" height=1 width=1> method.  I
do not want a script reliant on Server Side Includes (SSI).

Thanks!
Answer  
Subject: Re: PHP Script Needed!
Answered By: majortom-ga on 02 Feb 2004 09:54 PST
 
The following PHP script does what you want. All you have to do is
change the email address on the line that invokes the mail() function,
provide whatever subject line and body text you wish, and provide the
location of any valid PNG, JPEG or GIF image since, as you requested,
this script outputs an image and is suitable for fetching via an IMG
tag. Also be sure to change the line that invokes the header()
function if you choose an image that is not in PNG format; you can
simply output image/gif or image/jpeg rather than image/png, nothing
to it. Enjoy the code, I'm sure it will work well for you.

<?php 
  // This is a file system path to the image that should be sent after
  // the user agent check. You may use any valid image that you want to
  // incorporate in your page, or a 1x1 transparent GIF image, however
  // IF THE IMAGE IS NOT A PNG FILE then you MUST change the header() call
  // further down in the code to say image/gif or image/jpeg as
  // appropriate to the type you are sending.
  $imageFile = "/path/to/my/image.png";
  if ($HTTP_USER_AGENT == 
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sbcydsl 3.12)") 
  {
    mail('yourname@yoursite.com', 'Subject goes here', 'Body text goes here'); 
  }
  header("Content-type: image/png");
  $handle = fopen($imageFile, "rb");
  echo fread($handle, filesize($imageFile));
  fclose($handle);
?>
Comments  
Subject: simple solution
From: cristoper-ga on 02 Feb 2004 08:49 PST
 
Include the following in an HTML/php file, and it should send you the
notification you desire. Change the value of the variable $notifyEmail
to the email address you wish to receive the notifications at.
---

<?php

$notifyEmail = "yourname@yourdomain.com";  // Change to your email address
$stringToMatch = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
sbcydsl 3.12)"; // Change to the User Agent string you wish to test
against


$thisPage = $_SERVER['SCRIPT_NAME'];
$thisSite = $_SERVER['SERVER_NAME'];
$theUserAgent = $_SERVER['HTTP_USER_AGENT'];

if ($theUserAgent == $stringToMatch) {
    $message = "A user agent matching the string \"$stringToMatch\"
has accessed the page located at: $thisSite$thisPage.";
   mail ($notifyEmail, "Email Notification: page accessed", $message,
"From: Notification\nX-Mailer: PHP/" . phpversion());
}
?>
Subject: Re: PHP Script Needed!
From: kayyak-ga on 04 Feb 2004 01:44 PST
 
If you are only looking for sbcydsl 3.12 on any Browser / OS, use the
strstr function, which checks for a substring in the HTTP_USER_AGENT
string. This will check over all browser versions and not a specific
combination.

if (strstr($_SERVER["HTTP_USER_AGENT"], "sbcydsl 3.12")) {
// send mail
}

hope this is a good tip for the above code.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy