r/PHPhelp • • Aug 21 '20

Launch countdown for a website.

I have a website that launches on 28th @ 12:00 gmt.
Until that time, I want my page to display a countdown page.

Currently I have a html -page with a javascript -snippet that displays the countdown,
and when the counter reaches zero, it redirects to the real page.

Problem is that my javascript counter uses date(), which gets the time from client machine,
so you can fake your system time and get past the countdown- page.

What would be an efficient and sure way to prevent anyone from viewing my page before launch?

10 Upvotes

9 comments sorted by

14

u/g105b Aug 21 '20

Output the page with PHP, where PHP shows the countdown, then use JavaScript to do the ticking down using the starting point provided by PHP.

When the counter reaches 0, make JavaScript reload the page itself rather than redirecting. Then back in PHP you can do the redirect if the countdown is 0, but this being done on the server will prevent client side hacks.

1

u/[deleted] Aug 21 '20

This sounds like the right way to do it, but I have no previous knowledge of PHP or server-side scripting..

If you know any tutorials or anything, could you point me to a right direction? I only need an example of a very basic "if(servertime>releasetime)= display page1, else display page2" -type of implementation,

1

u/g105b Aug 21 '20

The browser is redirected by sending the "Location" header. I'd recommend reading the PHP docs about sending headers and working with dates, but a quick and dirty script could look like:

$now = new DateTime();
$launch = new DateTime("2021-04-05 17:24:00");

if($now < $launch) {
    // Display coming soon page.
}
else {
    header("Location: https://example.com", true, 307);
    exit;
}

2

u/Amunium Aug 22 '20

This is a suboptimal way of doing it, because anyone who guesses the redirection destination can still go directly to it and bypass the countdown.

If possible, it should be done directly on the page/index file with the rest of the content. Something like

if ($now < $launch) {
    include('countdown.html');
    exit;
}
// rest of the page here

3

u/aba2092 Aug 21 '20

This, if you really don't want anyone to know the final URL in advance.. they would be able to find it by looking at the JS script that does the redirect otherwise

1

u/[deleted] Aug 21 '20

Well done.

6

u/Amunium Aug 21 '20

Does that mean the website is actually running and completely available, you've just hidden it with some Javascript? That's a fundamentally bad way of doing that. Anyone with a script-blocker or the tiniest bit of knowledge of script is going to be able to get around that, not to mention that robots and web crawlers can see things they shouldn't.

You should only have the counter page available, the rest should either not exist until the set date, or be locked off with server side technology - e.g. PHP. Then it won't matter if someone tries to get around it.

3

u/jurgonaut Aug 21 '20

An easy solution would be to define yourself the date used. Store it somewhere on the backend and let javascript use that for the countdown.