Jump to content

Email Notification


soiler

Recommended Posts

I apologize ahead if this is really an Apache question, but I suppose it could be in either forum.

 

I have a web directory that when anything is accessed within it or its children from a range of IP addresses, they are redirected to a new URL, like so:

 

.htaccess

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REMOTE_ADDR} ^111\.111\.111\. [OR]

RewriteCond %{REMOTE_ADDR} ^222\.222\.222\.

RewriteRule !^(.*redirects.*)$ http://www.somedomain.com/something.php [L,NC,R]

 

something.php

<?php

    $to        = "Someone <some@email.com>";

    $subject    = "Some Subject";

    $headers    = 'From: Somebody <no_reply@someone.com>' . "\r\n" .

                        'X-Mailer: PHP/' . phpversion();

 

    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))

        $VisitorIp=$_SERVER['HTTP_X_FORWARDED_FOR'];

    else

        $VisitorIp=$_SERVER['REMOTE_ADDR'];

    trim($VisitorIp);

   

    $body      = "Someone of IP " . $VisitorIp . " accessed resource\n";

 

    mail($to, $subject, $body, $headers);

?>

 

The problem is that I do not know on how to get the specific resource (URL) the client tried to access into the email, which I'd also like (e.g. more referrer information). The IP address is no problem though. Any ideas?

Link to comment
Share on other sites

Thanks for your help requinix. Two things.

 

What does the [R] flag do? I couldn't seem to find it in the Apache documentation (which is pretty intimidating).

 

The REQUEST_URI variable just seems to always contain the URI of the page being redirected to, not the URI that the client originally tried to access before being redirected. Any help?

Link to comment
Share on other sites

Normally Apache will do URL rewriting in the background - the client doesn't know anything is happening. The REQUEST_URI will be whatever the request was originally for, but the script that actually runs will be whatever the rewritten URL referenced.

The [R] flag means to redirect instead of rewrite. The client gets redirected to the new URL and so it submits a new request. Then the REQUEST_URI will be whatever it was for the new request.

 

So remove the [R] and you should be good. As for documentation, the two things in mod_rewrite you'll use most are RewriteCond and RewriteRule. It helps to know how HTTP works but a quick read-through of the documentation for those two directives should get you what you need.

Link to comment
Share on other sites

Hey requinix. Even after removing the [R] flag, the REQUEST_URI variable still only contains the new URL, not the one the client had originally intended to access. Thanks for your help in this. It's been driving me nuts for a long time now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.