简体   繁体   English

PHP 重定向链接每分钟仅更新一次 sql 数据库

[英]PHP redirect link only updates sql database once every minute

For tracking the number of clicks (and IP) for a website I created a php script that stores the date and IP info and only once the query was executed successfully it redirects the user to the link.为了跟踪网站的点击次数(和 IP),我创建了一个 php 脚本来存储日期和 IP 信息,只有在成功执行查询后,它才会将用户重定向到链接。 Even though the following script is very minimal, it works.尽管以下脚本非常小,但它可以工作。

However, if I click the link twice within one minute (tested both in Chrome and Edge) it does not save this as a click but it does redirect me to the correct page which I find very odd.但是,如果我在一分钟内点击该链接两次(在 Chrome 和 Edge 中都进行了测试),它不会将此保存为一次点击,但它会将我重定向到正确的页面,我觉得这很奇怪。 I have tried this by clicking multiple times and only the values after +-1 min get stored.我已经通过多次单击来尝试此操作,并且仅存储 +-1 分钟后的值。

Opening a new Chrome window does not work.打开新的 Chrome window 不起作用。 Opening an incognito screen within 1 min does work and this click gets saved.在 1 分钟内打开隐身屏幕确实有效,并且此点击会被保存。

Addition info: in the SQL database there is a primary ID column with AUTO_INCREMENT.添加信息:在 SQL 数据库中,有一个带有 AUTO_INCREMENT 的主 ID 列。

Question : is there any reason why this happens (perhaps caching?) and would there be any way to prevent this?问题:发生这种情况有什么原因(也许是缓存?),有什么办法可以防止这种情况发生吗?

The URL to redirect to link 1 is: thisphpfile.php?link=1重定向到链接 1 的 URL 是: thisphpfile.php?link=1

//---------
// Get info on which link to point to
$link = $_GET["link"];

// Check link value to prevent SQL injection
if($link =='1'){
    $link_sql = "https://www.url1.com";
}
if($link =='2'){
    $link_sql = "https://www.url2.com";
}
    


// If link was a valid input, continue, else back to homepage
if($link =='1' || $link == '2'){


$date = date("Y-m-d H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];


$sql = "INSERT INTO tablename (DATE , IP, LINK) VALUES ('$date', '$ip','$link_sql')";

//Execute query
if ($conn->query($sql) === TRUE) {
ob_end_flush();

// Redirect link
header("Location: " . $link_sql);
}

}else{
    // Redirect link
    header("Location: https://www.default.com" );
}

Adding the following fixed the issue:添加以下内容解决了问题:

    // Redirect link
header('HTTP/1.1 307 Temporary Redirect');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
header('Pragma: no-cache');
header('Location: ' . $link_sql, true, 307);

I don't see the reason, why it shouldn't get saved.我不明白为什么不应该保存它的原因。 But your code structure is a little bit unclear.但是你的代码结构有点不清楚。 Maybe you can try it with the following code and we can work it out somehow:也许您可以使用以下代码尝试一下,我们可以以某种方式解决它:

$links = [
    1 => 'https://www.url1.com',
    2 => 'https://www.url2.com'
];

// Check if GET-parameter is numeric and if the number is in the links array above
if (is_numeric($_GET["link"]) && isset($links[intval($_GET["link"])])) {
    $link = $links[intval($_GET["link"])];

    $sql = sprintf("INSERT INTO tablename (DATE, IP, LINK) values ('%s', '%s', '%s')",
        date("Y-m-d H:i:s"),
        $_SERVER['REMOTE_ADDR'],
        $link
    );

    if ($conn->query($sql) === TRUE) {
        header("Location: " . $link);
        exit;
    }
}

// This will be the default header when the exit above cannot be reached (because of errors)
header("Location: https://www.default.com");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM