简体   繁体   中英

PHP counter file and page forwarding

So i'm writing this code so that you either get forwarded to a certain page if you're the first one to hit the link, or you are sent back to the original page after being displayed a message if you're not what beginner mistake am i making?

<?php
$count = file_get_contents('counter.txt');
$count = trim($count);
if ($count="0")
{
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
header("Location: newpage.html");
}
else
{
fclose($fl);
echo "Sorry but the item has already been sold out";
header("Location: oldpage.html");
}
?>

As for the delay, you can accomplish it two different ways. The first is to use PHP header (like you are currently doing), but change it to look like this:

<?php
header("refresh:5;url=oldpage.html");
echo "Sorry but the item has already been sold out";
?>

The other way is to echo out a piece of HTML code, the meta-refresh:

 <?php
 echo '<meta http-equiv="refresh" content="2;url=oldpage.html">';
 echo "Sorry but the item has already been sold out";
 ?>

In both examples, 5 is the amount of seconds until the refresh. Experiment with each one to see if it will fit your needs.

This might be some sort of syntax that I'm not familiar with, but none of my scripts have ever had the

<? code

I simply use

<? 

Also since you did not delay our header tag the user will not see the previously echoed statement above it. It will automatically redirect before the page has time to output fully.

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