简体   繁体   中英

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. 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. I have tried this by clicking multiple times and only the values after +-1 min get stored.

Opening a new Chrome window does not work. Opening an incognito screen within 1 min does work and this click gets saved.

Addition info: in the SQL database there is a primary ID column with AUTO_INCREMENT.

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

//---------
// 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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