简体   繁体   中英

PHP MySQL query only runs the first time the page loads

I am using a url shortener, and I am trying to make it so that when they click the url and are redirected to the intended page, it adds + 1 to the hit counter in the database. What is happening is that the first time I click the link, it works as intended, but unless I clear my browser history, the counter will not update again. The redirect still works. Here is the code:

if (!empty($_GET['url'])) {
$redirect = mysql_fetch_assoc(mysql_query("SELECT link FROM urls WHERE short_url =         '".addslashes($_GET['url'])."'"));
$update = mysql_query("UPDATE urls SET hits = hits + 1 WHERE short_url = '".addslashes($_GET['url'])."';");
$redirect = "http://".str_replace("http://","",$redirect[link]);
header('HTTP/1.1 301 Moved Permanently');  
header("Location: ".$redirect);  
} 

Any suggestions are appreciated.

I would say that, since you are returning a 301 (Moved Permanently), the browser caches the information and won't bother to load your PHP page again. That can be useful, if you only want to keep track of the number of people who clicked on the link; otherwise you could try to return a 302 (Found).

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