PDA

View Full Version : PHP Help Needed...



oenkitt
02-27-2010, 06:32 PM
There are a few pages of my site that I want users to be able to view only once a week/biweekly/monthly/etc (basically, just once per "round"). So far I've done this by just setting up a table in my database to keep track of how many times each user has viewed each page, and I just reset it whenever I want them to be able to view it again. But recently some of my users have told me that, occasionally, when they visit one of these pages, it tells them that they've already been there before?

The only thing I can think of is that their browsers are randomly reloading the pages for some reason, because the script on those pages is still being executed, but the users never get chance to see the output. It's fairly important for them to be able to see the script's output before they get "locked" out of the page, so is there a way to get around this?

Here's the part of the code that handles logging and such:



$activityinfo = get_table_info( activities, $activityid ); // gets the page's info
$log = $activityinfo['log']; // get the field name for the page's log
$loginfo = get_table_info_name( log, $_SESSION['username'] ); // gets user's page log info
if ( $loginfo[$log] == 0 && isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true ) { log ( $_SESSION['username'], $log ); $temppage = on; include 'temppage.php'; } // if they haven't viewed the page this round and they're logged in, log that they have viewed the page AND include the page
else if ( $loginfo[$log] > 0 ) { echo '<h1>Error</h1> Sorry, you\'ve already visited this round.'; } // OR if they have already viewed the page, show an error
else if ( !isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == '' ) { echo '<h1>Error</h1> You must be logged in to view this page! Please <a href="/login.php">log in</a> and try again.'; } // OR if they're not logged in, direct them to log in page
else { echo '<h1>Error</h1> There was an error processing the page.'; }

shadmego
02-27-2010, 08:37 PM
As a way to test, why not try putting the logging function, where users are actually logged as having viewed the page, at the top of the "temppage.php" instead of before the page loads?

That way, in case an error occurs and the page can't load for some reason, the user is not counted as having viewed the page and can come back when/if the error passes. Network congestion and proxy servers are only two examples of things I can think might cause problems with the way you have it set up right now.

I could be wrong though. If it were me, I would test what I recommended and see if that works.

oenkitt
03-06-2010, 01:26 PM
I tried what you suggested, and my users are still reporting the same problem. ):

I put my log() function at the top of "temppage.php" and now I've just moved it to the bottom to see if that does anything.. However, I'm worried that this might cause the script on the page to execute twice, if their browsers really are refreshing the page for some reason? Which is what I'm trying to prevent with this logging system.

I'm also wondering if this could have something to do with Javascript? It seems like the only pages that produce this problem are the ones that use Javascript to redirect the URL.

shadmego
03-06-2010, 01:35 PM
You can always try to use php redirection:



header( 'Location: http://www.yoursite.com/new_page.html' ) ;



Be careful if you want to use this. You will have to place the above code before any HTML output or it will cause an error.

oenkitt
03-06-2010, 03:05 PM
Is it possible to use a PHP redirect within Javascript? The reason I'm using Javascript is because the users have to complete a Javascript game before they're taken to a prize page (temppage.php), and so upon completion, Javascript redirects them.

sjlplat
03-07-2010, 12:11 PM
Is it possible to use a PHP redirect within Javascript? The reason I'm using Javascript is because the users have to complete a Javascript game before they're taken to a prize page (temppage.php), and so upon completion, Javascript redirects them.

PHP redirects have to be initiated before any other data is output, so it is not possible to include them with Javascript. Javascript writes data after the content of a page has been delivered.