Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Send an eMail to an address set by <txp:variable/> for site errors.
Good morning!
I’m migrating a content-intensive site to Textpattern, and to make sure all our 301 redirects are in place I’d like to add a bit of code to the 404 page to send error reports to an address set by a txp:variable
. I’ve seen the plugin wan_error_email, but that only allows you to specify an eMail recipient by privileges in the user table.
This code seems to perform the function, but I’m not sure how to call the variable from within the script; while the parser can recognize txp:tags within tags, it doesn’t expand them when nested in txp:php
.
I figure it’s probably a pretty simple task for those with PHP chops— let me know what you think!
warmest regards!
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
johnstephens wrote:
while the parser can recognize txp:tags within tags, it doesn’t expand them when nested in
txp:php
.
Two main ways to approach it — aren’t there always with TXP?! :-)
global $variable;
$myemail = $variable['email_var_name'];
OR
$myemail = parse('<txp:variable name="email_var_name" />');
The first is probably quicker. Hope that helps.
Note to self: Need to find a place in Textbook for this kind of info. Haven’t decided where best to put it yet
Last edited by Bloke (2009-12-11 16:09:21)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
Thanks again, Stef! That part is working.
I also replaced line three in the script with this:
$page = parse('<txp:site_url/><txp:pax_grep from="'^\/'" to=""><txp:page_url type="request_uri"/></txp:pax_grep>');
(pax_grep is there just to yank out the leading slash.)
But I get a PHP error when I load the page without a referrer, so I think a conditional to check for HTTP_REFERER is needed. Maybe it’s not worth it— the main dude who will be running the site can check the server’s error logs. It’s not a high-traffic site, so an eMail might be a good flag, but Google Webmaster Tools is also a viable option.
Have you done something similar on any sites? What do you recommend?
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
johnstephens wrote:
I get a PHP error
It might be the single quote in your from
attribute, closing the opening single quote of the parse()
function and making TXP throw a wobbly.
Short of using from="\'^\/\'"
(which may or may not work) you could split up the calls:
$ref = substr(parse('<txp:page_url type="request_uri"/>'), 1);
and then use $ref
in subsequent code (substr() chops off the first char in this case — not sure what would happen if there’s no referrer, though).
Perhaps more robust might be:
$ref = page_url(array("type" => "request_uri"));
$page = site_url(array()) . ($ref ? substr($ref, 1) : '');
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
Thanks again— your method is much cleaner, and echos the same URL.
I’m sorry I didn’t specify the error:
Tag error: <txp:php> -> Notice: Undefined index: HTTP_REFERER on line 8
On line eight, I have this:
Visitor came from: ".$_SERVER['HTTP_REFERER']."
I’m thinking that the entire message should be couched within a conditional that checks if there is an HTTP_REFERER. Maybe something like this?
if (defined('.$_SERVER['HTTP_REFERER'].')) {
global $variable;
$myemail = $variable['email_var_name'];
// ...
mail($myemail,$subject,$message,"From: 404errorpage");
}
…Obviously, I don’t know the correct way to check if there is an HTTP_REFERER.
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
johnstephens wrote:
…Obviously, I don’t know the correct way to check if there is an HTTP_REFERER.
If you are calling global/variable that is not always set, like this one, you need to first check if it is set.
$ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
Or you can use TXP’s core function:
serverSet('HTTP_REFERER');
So:
if(serverSet('HTTP_REFERER')) {
[...]
Offline
Re: Send an eMail to an address set by <txp:variable/> for site errors.
Thanks, Jukka! I tested this today and it works great. Hopefully, we’ll have all our 301s in order soon, and we can deactivate this script and just keep an eye on the server logs.
Offline