How can I set a time limit for a user to stay on a web page. Let me explain it to you;

Let suppose I've two web pages, page-A and page-B. When the user is on page-A, he needs to stay at this page for 30 secs before going to page-B. If the user directly visit the page-b, then the error is shown to the user with the remaining sec left to visit page-B. When the timer reaches to 0 sec or you can say 30 secs are over, then the user can view the content of page-b..

Can anyone let me know how can I do it?

With the help of Ajax you can achieve this.
Call a timer function onload inside body tag.After its execution do your task.May be you can show the link to pageB only at that time.

Member Avatar for diafol

Ajax can do it. Plain js can do it. OR php can do it via header refresh. The plain js window.location using time intervals is the usual method, but if js is not enabled, I don't think that the page will be redirected.

header( 'refresh: 30; url=http://www.example.com/index.php' );
...(rest of page)....

After 30 seconds, the page is redirected or refreshed (if sent to itself).

Alright, i make it easy for you guys. You can take the example of a rapidshare where we've to wait few seconds before downloading the file.. My case is kinda different as I want the user to wait for the specific time before going to page b. If the user visits page b before the counter ends, the error is shown to the user. When the counter ends, the user is allowed to view the content of page b...

Also, I would be grateful if anyone can provide me the code.. I've searched on Google too much but can't find a way... It's urgent

EDIT: BTW, you can also take the example of neobux.com .. If you closed the ad before 30 seconds and try to visit a new ad, the error is shown to you with the remaining time you've to wait...

Member Avatar for diafol

To show a countdown, you'll need js. To provide a 'sorry you're too early', an easy solution would be to set a session variable (or cookie).

first page

<?php
session_start();
if(isset($_SESSION['allowed_view'])){
    $delay = $_SESSION['allowed_view'] - time(); //for redirects back to this page
    $error_msg = "<p>You cannot proceed until the countdown is finished.</p>";
}else{
    $delay = '30'; //countdown time in seconds for first time page entry
    $error_msg = "";
    $_SESSION['allowed_view'] = time() + $delay; //set time at which page can be redirected
}
//place your timer (either js for coundown display - place in head area OR php with refresh method, if countdown not impt - USING $delay as timer).
?>

second page

<?php
session_start();
$current = time();
if(!isset($_SESSION['allowed_view']) || $current < $_SESSION['allowed_view']){
   //redirect back to sending page
}else{
  unset($_SESSION['allowed_view']); //safely remove this variable - or do something more exotic.
}
?>

Top of head stuff, so don't know if it'll work without a fiddle. Haven't included a coundown script - that you can do yourself - loads of free scripts available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.