Search code examples
phphtmlurl

Why does the URL's query string change when loaded for the first time or the page has been opened for awhile?


The issue is; if a person clicks a link to an article, event, post etc. on my site, the URL paramaters (after the "content?data=") break. I am using Google Chrome as my primary browser while testing and InifinityFree as my webhost.

For example, a person on WhatsApp, or another other place aside from my site, may click a link. The link then goes to the webpage correctly, for a moment. The URL appears in the URL bar while the page loads. Once loaded however, it changes the URL content to a mess of 0%0 characters. From research, these are apparently "null" characters. Why is this happening?

Link clicked: https://clarity.rf.gd/i?i=test-invite

Link opened: https://clarity.rf.gd/i?i= 0%00%00%00%00%00%00%00%00

For futher clarification regarding the link, the parameters at the end are correct. The file is called "i.php" as opposed to "invitation.php" and the "GET" data is "i" as opposed to "url" to minimise the URL length (https://clarity.rf.gd/inivtation?url=test-invite). Also, ignore the single space between the "...i=" and "0%0...", Stack Overflow's input makes the trailing paramaters disappear.

Here is the main code for the page:

i.php:

$page_url = urlencode($_GET['i']);

if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['i'])) {
    $myEvent = $mysqli->prepare('SELECT * FROM event WHERE url=?');
    $myEvent->bind_param('s', $page_url);

    if ($myEvent->execute()) {
        $result = $myEvent->get_result();
        if ($result->num_rows == 1) {
            $row = $result->fetch_assoc();
            // Fetch Primary Database Data
            $id = $row['id'];
            $account_id = $row['account_id'];
            $url = $row['url'];
            // Fetch Other Database Data
            $title = $row['title'];
            $desc = $row['description'];
            $date_time_start = $row['date_time_start'];
            $date_time_end = $row['date_time_end'];
            $location = $row['location'];
            $locationPin = $row['locationPin'];
            $colour = $row['bg_colour'];
            $image = $row['bg_image'];
        }
    }
    $myEvent->close();
}

I have tried with and without the "urlencode" function, in the hopes it would help. Each page works completely fine when navigating the site, it is only when clicking the link from an external place or the page is left opened for some time and the page gets refreshed. I'm aware it's likley some sort of caching or redirect but I have no idea where this is coming from.

Also, let me know if I am doing anything wrong, generally speaking. I am learning different ways to display unique content on each page and if this is incorrect, let me know.

I'd appreciate the support, thank you!


Solution

  • Apologies for the oversight. The webhost I am using actually uses the "?i=" for some kind of security thing; resulting in "index.php?i=1". It kept rewriting the URL in an unexpected way.

    I have since rewritten all of my "i"s to "l"s to still the character width to a minimum and it appears to have worked. The URL, when loaded for the first time now appears; "index.php?l=home-page&i=1". I've had no issues within the past week I've been testing.

    ~ Thank you for your comments, I appreciate them.