Sergio Pinna

Web Designer

email-alert-404

Email automatica quando un utente non trova la pagina (404 error)

Email automatica quando un utente non trova la pagina (404 error), capita a tutti di andare in un sito internet e trovarsi la temuta pagina 404…

…ma se io costruisco siti internet come faccio a sapere se un utente ha ricevuto un 404 da parte del mio sito?

Per rispondere a questa domanda googlando qua e la ho trovato uno script molto semplice, c’è da fare giusto un copia e incolla, da inserire nella pagina 404.php

In pratica tale script ti invierà un email ogni volta un utente si imbatte in un URL non trovato del tuo sito …veramente molto comodo!

Per ricevere una email automatica quando un utente non trova la pagina desiderata e riceve un errore 404, devi procedere così:

Email automatica quando un utente non trova la pagina (404 error)

Nel file 404.php, prima di

<?php get_header(); ?>

aggiungi il codice:

<?php // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/

// set status
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");

// site info
$blog  = get_bloginfo('name');
$site  = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');

// theme info
if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
	$theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
} else {
	$theme_data = wp_get_theme();
	$theme = clean($theme_data->Name);
}

// referrer
if (isset($_SERVER['HTTP_REFERER'])) {
	$referer = clean($_SERVER['HTTP_REFERER']);
} else {
	$referer = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
	$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
} else {
	$request = "undefined";
}
// query string
if (isset($_SERVER['QUERY_STRING'])) {
	$string = clean($_SERVER['QUERY_STRING']);
} else {
	$string = "undefined";
}
// IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
	$address = clean($_SERVER['REMOTE_ADDR']);
} else {
	$address = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
	$agent = clean($_SERVER['HTTP_USER_AGENT']);
} else {
	$agent = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
	$remote = clean($_SERVER['REMOTE_IDENT']);
} else {
	$remote = "undefined";
}
// log time
$time = clean(date("F jS Y, h:ia", time()));

// sanitize
function clean($string) {
	$string = rtrim($string); 
	$string = ltrim($string); 
	$string = htmlentities($string, ENT_QUOTES); 
	$string = str_replace("\n", "<br>", $string);

	if (get_magic_quotes_gpc()) {
		$string = stripslashes($string);
	} 
	return $string;
}

$message = 
	"TIME: "            . $time    . "\n" . 
	"*404: "            . $request . "\n" . 
	"SITE: "            . $site    . "\n" . 
	"THEME: "           . $theme   . "\n" . 
	"REFERRER: "        . $referer . "\n" . 
	"QUERY STRING: "    . $string  . "\n" . 
	"REMOTE ADDRESS: "  . $address . "\n" . 
	"REMOTE IDENTITY: " . $remote  . "\n" . 
	"USER AGENT: "      . $agent   . "\n\n\n";

mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "Da: $email"); 

?>

Se ti è piaciuto l’articolo,, potrebbe anche interessarti Personalizzare completamente la pagina login

Fonte:
How to set up an email alert for 404s

Condividi questo articolo