点击链接时的PHP跟踪

点击链接时的PHP跟踪,php,redirect,Php,Redirect,您好,提前感谢您提供的任何建议 我试图做到的是:当用户单击一个链接时,我想添加一个自动增量id,单击URL和时间戳到数据库,然后将它们发送到URL链接登录页 我遇到的问题是:单击链接时,URL没有添加到数据库中,重定向也失败 以下是我正在编写的代码: ad\u单击跟踪.php <?php include ("admin/includes/connect.php"); mysql_select_db("$database") or die(mysql_error()); //Col

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

我试图做到的是:当用户单击一个链接时,我想添加一个自动增量id,单击URL和时间戳到数据库,然后将它们发送到URL链接登录页

我遇到的问题是:单击链接时,URL没有添加到数据库中,重定向也失败

以下是我正在编写的代码:

ad\u单击跟踪.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";

?>

创建链接的更好方法是使用以下PHP代码:

$url = 'http://paws4autism.org';
echo '<a href="http://recyclingkansascity.com/ad_click_tracking.php?page='
       . htmlspecialchars(urlencode($url)) . '" target="_blank">...</a>';
在ad_click_tracking.php中,在继续之前,您应该检查是否设置了
$\u 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是首选。请看这里:

哦,至于HTTP重定向的安全性,请参阅本页(我建议通读所有答案)。唯一真正的问题与网络钓鱼诈骗有关。您没有提供用户通常无权访问的文件。

在ad\u click\u tracking.php上的var\u dump($\u GET)给了您什么?您可能还想将href=“”更改为href=“”,看看这是否有帮助(在index.php之前删除一个额外的“值”)你知道这是非常不安全的吗?只是一个小小的url操作,我可以得到任何东西:Maximus2012:切换到var_dump不起作用。起作用的是按照你的建议删除多余的内容。这完全修复了指向内部页面的第一个链接,现在该链接与添加到数据库的链接一样有效。但是,这并没有修复外部链接,它在单击时导航到recyclingkanscity.com/ad_click_tracking.php?page=并且没有将url添加到数据库中。aqab0N:我不知道它是不安全的,到目前为止,我甚至无法让它工作lol。你有什么建议如何使它更安全吗?感谢大家的帮助,我非常感谢信息技术narb:我实施了你的建议。现在登录页面时,只显示未指定的页面。如果我改变主意!isset to just isset然后页面确实加载得很好,但是当我单击链接时,它不会添加到数据库中,URL显示为“”,并且不会推送到实际的登录页面。出于某种原因,它保留了“”,而不仅仅是使用实际的登录url?@DisasterFaster听起来,在创建链接时,您在其中的某个地方有一个额外的引号。也许可以在浏览器中查看页面来源,看看是否真的没有额外的引用。我只是想向所有花时间帮助我解决这个问题的人表示衷心的感谢。在做出更改后,Narb建议代码是正确的,但我仍然有一个问题。问题是标题重定向只在内部页面上有效,在外部页面上会失败。事实证明数据中心出于安全原因阻止了这一点。联系他们后,他们白名单的脚本,现在外部重定向工作完美,现在一切都很好。
$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");