PHP 链接被点击时跟踪
PHP tracking when links are clicked
您好,在此先感谢您提供的任何建议。
我想要完成的事情:当用户点击link我想添加一个自动递增的id,点击URL和时间戳到数据库,然后将它们发送到URL link 着陆页。
我遇到的问题:单击 link 时,URL 未添加到数据库并且重定向也失败。
这是我正在处理的代码:
ad_click_tracking.php
<?php
include ("admin/includes/connect.php");
mysql_select_db("$database") or die(mysql_error());
//Collecting the destination URL from the clicked link
$redirect = mysql_real_escape_string($_GET['page']);
//Insert destination URL and time stamp into MySQL
$page_insert = mysql_query("INSERT INTO ad_click_tracking (`url`, `date`) VALUES ('$redirect', now())") or die(mysql_error());
//Redirecting user to the clicked URL
header("Location: $redirect");
//Debugging to see if we collected the URL
echo "Redirect URL: $redirect";
?>
header.php(包含要跟踪的 links - 第一个 link 是内部的,第二个 link 是外部)
<a href="http://recyclingkansascity.com/ad_click_tracking.php?page="index.php" target="_blank"><img src="/images/header_banner/recycling_kansas_city_header.png" width="620px" height="340px" alt="Recycling Banner" title="Recycling Kansas City"></a></li>
<a href="http://recyclingkansascity.com/ad_click_tracking.php?page="http://paws4autism.org" target="_blank"><img src="/images/header_banner/funny_bunny_5k_autism_egg_hunt.png" width="620px" height="340px" alt="Paws 4 Autism" title="Paws 4 Autism Easter Event"></a></li>
当我单击内部或外部 link 时,浏览器将 URL 显示为 recyclingkansascity.com/ad_click_tracking.php?page= 然后当我检查数据库时ID 已自动递增并插入了时间戳,但 URL 为空。出于某种原因,$_GET['page']) 似乎无法抓取页面 URL,我至今仍无法弄清楚原因。我通读了相关内容 "similar questions",但未能找到答案。
创建链接的更好方法是使用 PHP 代码,例如:
$url = 'http://paws4autism.org';
echo '<a href="http://recyclingkansascity.com/ad_click_tracking.php?page='
. htmlspecialchars(urlencode($url)) . '" target="_blank">...</a>';
这会将 url 作为查询字符串转义。如果不这样做,它可能会或可能不会工作,但这是执行此操作的正确方法。例如,http://paws4autism.org
将变为 http%3A%2F%2Fpaws4autism.org
。如果你想知道双重转义,这里稍微分解一下:
$url = 'http://paws4autism.org';
// escape query string when constructing url:
// (this would be necessary even if you weren't rendering it as a link in html)
$href = 'http://recyclingkansascity.com/ad_click_tracking.php?page=' . urlencode($url);
// escape for html rendering:
echo '<a href="' . htmlspecialchars($href) . '">...</a>';
在 ad_click_tracking.php 中,您应该在继续之前检查是否设置了 $_GET['page']
。此外,重定向到页面参数的 MySQL 转义版本也没有意义。所以,而不是这个:
$redirect = mysql_real_escape_string($_GET['page']);
// (...insert with $redirect...)
header("Location: $redirect");
我会这样做:
if (!isset($_GET['page'])) {
// this is a little bit more informative than just dying
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
die('No page specified');
}
$redirect = $_GET['page'];
$s_redirect = mysql_real_escape_string($redirect);
// (...insert with $s_redirect...)
header("Location: $redirect");
最后,PHP 的普通 mysql 库并不真正推荐使用。首选 Mysqli(使用几乎相同的语法)或 PDO。看这里:MySQL vs MySQLi when using PHP
哦,关于执行 HTTP 重定向的安全性,请参阅此页面(我建议通读所有答案)。唯一真正的问题与网络钓鱼诈骗有关。您提供的不是用户通常无权访问的文件。
php security for location header injection via $_GET
您好,在此先感谢您提供的任何建议。
我想要完成的事情:当用户点击link我想添加一个自动递增的id,点击URL和时间戳到数据库,然后将它们发送到URL link 着陆页。
我遇到的问题:单击 link 时,URL 未添加到数据库并且重定向也失败。
这是我正在处理的代码:
ad_click_tracking.php
<?php
include ("admin/includes/connect.php");
mysql_select_db("$database") or die(mysql_error());
//Collecting the destination URL from the clicked link
$redirect = mysql_real_escape_string($_GET['page']);
//Insert destination URL and time stamp into MySQL
$page_insert = mysql_query("INSERT INTO ad_click_tracking (`url`, `date`) VALUES ('$redirect', now())") or die(mysql_error());
//Redirecting user to the clicked URL
header("Location: $redirect");
//Debugging to see if we collected the URL
echo "Redirect URL: $redirect";
?>
header.php(包含要跟踪的 links - 第一个 link 是内部的,第二个 link 是外部)
<a href="http://recyclingkansascity.com/ad_click_tracking.php?page="index.php" target="_blank"><img src="/images/header_banner/recycling_kansas_city_header.png" width="620px" height="340px" alt="Recycling Banner" title="Recycling Kansas City"></a></li>
<a href="http://recyclingkansascity.com/ad_click_tracking.php?page="http://paws4autism.org" target="_blank"><img src="/images/header_banner/funny_bunny_5k_autism_egg_hunt.png" width="620px" height="340px" alt="Paws 4 Autism" title="Paws 4 Autism Easter Event"></a></li>
当我单击内部或外部 link 时,浏览器将 URL 显示为 recyclingkansascity.com/ad_click_tracking.php?page= 然后当我检查数据库时ID 已自动递增并插入了时间戳,但 URL 为空。出于某种原因,$_GET['page']) 似乎无法抓取页面 URL,我至今仍无法弄清楚原因。我通读了相关内容 "similar questions",但未能找到答案。
创建链接的更好方法是使用 PHP 代码,例如:
$url = 'http://paws4autism.org';
echo '<a href="http://recyclingkansascity.com/ad_click_tracking.php?page='
. htmlspecialchars(urlencode($url)) . '" target="_blank">...</a>';
这会将 url 作为查询字符串转义。如果不这样做,它可能会或可能不会工作,但这是执行此操作的正确方法。例如,http://paws4autism.org
将变为 http%3A%2F%2Fpaws4autism.org
。如果你想知道双重转义,这里稍微分解一下:
$url = 'http://paws4autism.org';
// escape query string when constructing url:
// (this would be necessary even if you weren't rendering it as a link in html)
$href = 'http://recyclingkansascity.com/ad_click_tracking.php?page=' . urlencode($url);
// escape for html rendering:
echo '<a href="' . htmlspecialchars($href) . '">...</a>';
在 ad_click_tracking.php 中,您应该在继续之前检查是否设置了 $_GET['page']
。此外,重定向到页面参数的 MySQL 转义版本也没有意义。所以,而不是这个:
$redirect = mysql_real_escape_string($_GET['page']);
// (...insert with $redirect...)
header("Location: $redirect");
我会这样做:
if (!isset($_GET['page'])) {
// this is a little bit more informative than just dying
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
die('No page specified');
}
$redirect = $_GET['page'];
$s_redirect = mysql_real_escape_string($redirect);
// (...insert with $s_redirect...)
header("Location: $redirect");
最后,PHP 的普通 mysql 库并不真正推荐使用。首选 Mysqli(使用几乎相同的语法)或 PDO。看这里:MySQL vs MySQLi when using PHP
哦,关于执行 HTTP 重定向的安全性,请参阅此页面(我建议通读所有答案)。唯一真正的问题与网络钓鱼诈骗有关。您提供的不是用户通常无权访问的文件。 php security for location header injection via $_GET