当有人在电子邮件正文中打开包含IP地址等内容的页面时,我会发送触发电子邮件。
我用它从另一个页面运行PHP脚本。
<script src="trigger.php">
</script>
<!DOCTYPE html>
<html>
<head>
Here is the rest of the HTML page.......
此PHP脚本需要一些时间来处理,并且会减慢客户加载页面的速度。
在trigger.php中的是(我已经取出一些隐私线)
$site = "Main Page";
$email = "info@webhost.com";
$ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
$identify = $_SERVER['HTTP_USER_AGENT'];
if (isset($_GET['source'])){$source=$_GET['source'];}
date_default_timezone_set('Australia/Melbourne');
$date = date('l jS \of F Y h:i:s A');
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success') {
$country = $query['country'];
$city = $query['city'];
$lat = $query['lat'];
$lon = $query['lon'];
}
require_once "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->From = "Trigger@webhost.com";
$mail->FromName = "Website";
//To address and name
$mail->addAddress($email, "Web Trigger");
//Address to which recipient will reply
$mail->addReplyTo("info@webhost.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $site." Trigger";
$mail->Body = $message;
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
有没有办法在HTML页面上运行PHP脚本而不会减慢页面速度?
由于
罗布
答案 0 :(得分:1)
您应该使用Javascript触发此脚本。您应该使用Ajax(jQuery)或Fetch Api来触发Http请求。我建议使用Fetch,但请查看http://caniuse.com/#search=fetch以获取浏览器支持。
获取Api示例:
<html>
<head>
</head>
<body>
<span>Content</span>
<script>
(function() {
fetch('/trigger.php');
})();
</script>
</body>
</html>
在加载DOM后调用脚本,首先表示内容,然后是trigger.php的请求。