【问题标题】:PHP tracking when links are clicked点击链接时的 PHP 跟踪
【发布时间】:2015-03-05 02:12:58
【问题描述】:

您好,提前感谢您提供的任何建议。

我想要完成的工作:当用户单击链接时,我想将自动增量 ID、单击 URL 和时间戳添加到数据库,然后将它们发送到 URL 链接登录页面。

我遇到的问题:单击链接时,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(包含要跟踪的链接 - 第一个链接是内部链接,第二个链接是外部链接)

<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>

当我单击内部或外部链接时,浏览器将 URL 显示为recyclingkansascity.com/ad_click_tracking.php?page=,然后当我检查数据库时,ID 已自动递增并插入时间戳,但 URL 为无效的。出于某种原因, ($_GET['page']) 似乎无法获取页面 URL,我至今无法弄清楚原因。我通读了相关的“类似问题”,但无法找到答案。

【问题讨论】:

  • ad_click_tracking.php 上的 var_dump($_GET) 给你什么?
  • 您可能还想将 href="recyclingkansascity.com/…" 更改为 href="recyclingkansascity.com/ad_click_tracking.php?page=index.php" 看看是否有帮助(在 index.php 之前删除一个额外的 ")
  • 你知道它非常不安全吗?只需一个小小的 url 操作,我就能得到任何东西:recyclingkansascity.com/ad_click_tracking.php?page=passwd
  • Maximus2012:切换到 var_dump 无效。正如你所建议的那样,工作是删除额外的“。这完全修复了指向内部页面的第一个链接,现在该链接可以正常工作,就像它添加到数据库一样。但这并没有修复外部链接,它导航到回收站。 com/ad_click_tracking.php?page=paws4autism.org 时单击并且不将 url 添加到数据库中。
  • aqab0N:我不知道它是不安全的,到目前为止我什至无法让它工作,哈哈。你有什么建议我可以让它更安全吗?

标签: php redirect


【解决方案1】:

创建链接的更好方法是使用如下 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

【讨论】:

  • 感谢大家的帮助我不胜感激! narb:我实施了你的建议。现在登陆页面时,它只显示未指定页面。如果我将 !isset 更改为 isset,那么页面确实加载得很好,但是当我单击链接时,它不会添加到数据库中,并且 URL 显示为“recyclingkansascity.com/…”并且不会推送到实际的登录页面.出于某种原因,它保留了“recyclingkansascity.com/ad_click_tracking.php?page=”,而不仅仅是使用实际的登陆网址?
  • @DisasterFaster 听起来您在创建链接时在某处有一个额外的引号。也许在浏览器中检查页面源代码,看看是否真的没有多余的引号。
  • 我只想非常感谢所有花时间帮助我解决这个问题的人。在做出 Narb 建议的更改后,代码是正确的,但我仍然遇到问题。问题是标头重定向只能在内部页面上工作,而在外部页面上会失败。事实证明,出于安全原因,数据中心正在阻止它。在与他们联系后,他们将脚本列入白名单,现在外部重定向工作完美,现在一切都刚刚好。
猜你喜欢
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多