-1

I'm having two issues with a function that should add +1 to a database field and redirect to the target URL upon completion.

The problem is that unless I clear cache, my UPDATE query is not working. While trying to find why this happens I've come across another strange issue which doesn't update the function after I change it.

What I did was trying to comment header and it's still redirecting me until the cache is cleared. On the other hand when header is commented and after I clear cache my UPDATE function works every single time.

    public function clickShortlink($slug) {
        $connect = $this->connect();

        $stmt = $connect->prepare("UPDATE `shortlink` SET clicks = clicks + 1 WHERE slug = :slug;");
        $stmt->bindValue(":slug", $slug);
        $stmt->execute();

        $stmt = $connect->prepare("SELECT target FROM `shortlink` WHERE slug = :slug");
        $stmt->bindValue(":slug", $slug);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $target = $row['target'];
        }

        if (isset($target)) {
            $connect = null;
            header("Location: ".$target);
            exit;
        }

        $connect = null;
    }

What am I missing about header?

EDIT

This is how I call clickShortLink():

    <?php 
        if (isset($_GET['gototarget'])) {
            $shortlink = new Shortlink();
            $url = $shortlink->clickShortlink($_GET['slug']);
        } 
    ?>

<form>
    <br>
    <br>
    <input type="text" name="slug" value="di5gV">
    <button name="gototarget">Go to target</button>
</form>
Ricardo
  • 1,653
  • 8
  • 26
  • 51
  • There's nothing in your above code that should be causing the issue. One thing to note about `header()` though is that it requires there to be no DOM output before the headers are sent. How is `clickShortLink()` called, and is there any output before that? Note that you also might want to wrap your `if (isset($target)` logic inside of a check against the query, such as `if($stmt->execute())`... – Obsidian Age Jun 26 '18 at 21:54
  • If you are having cache related issues maybe this will help: https://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers – Mr Glass Jun 26 '18 at 21:54
  • @ObsidianAge edited the question, and noted about the additional `if`. @Mr Glass this doesn't help unfortunately, can PHP get into browser cache anyway? – Ricardo Jun 26 '18 at 22:00
  • 1
    If the code works after you clear your cache then the code works. Your issue is with the browser caching your page. If you try your code in a browser that you have not used to visit this page, and it works, then you know your code works (and you need to address the caching issue). – Mr Glass Jun 26 '18 at 22:03
  • If you cannot get past this you can always pass the desired URL as a javascript variable, have the javascript check if the `location.href` == `yourURL`, and if not set `location.href = yourURL`. – Mr Glass Jun 26 '18 at 22:05
  • If your response provides a `cache-control` or `expires` header, the browser should cache the request until that time. You can try to add an explicit one or both of the above to try to override the response. Probably wouldn't hurt to just try that. – Jonathan Jun 26 '18 at 22:07
  • That's some interesting information about `cache-control` but unfortunately, it doesn't help. – Ricardo Jun 26 '18 at 22:14

1 Answers1

-1

So like @Mr Glass spotted in the comments the issue had to do with a code I had in my .htaccess.

<IfModule mod_headers.c>
Header set Connection keep-alive
Header set Cache-Control "max-age=1209600, must-revalidate"
</IfModule>

Unfortunately adding the "no-cache" meta tags didn't help but after removing this code from my .htaccess file everything seems to work.

Ricardo
  • 1,653
  • 8
  • 26
  • 51