Is there a way of doing this in php (not javascript, not jquery)? I need to refresh a page X number of times and then stop. Can't find anything working online. I do not want to use header('Refresh: 5; URL=someurl.com'); unless there's a way to stop it from looping after a few times. I'd appreciate if nobody asks me why I need it. It doesn't matter. I just need it.
Just create a cookie on first load, and up the count for each reload. Then when the cookie has reached whatever limit you set, stop the reload. You can of course also run this as a session, if you prefer that. Or a DB pull.
We don't see a way of acomplishing this without using JavaScript. Once the page is delivered, it cannot be withdrawn from the client. The client can also not be server-side instructed to reload the page. The action must be started from within the page.
Here's an example: <?php if (!isset($_COOKIE['counter'])) { setcookie('counter',0); } if (isset($_COOKIE['counter']) && $_COOKIE['counter'] < 10) { $current_val = $_COOKIE['counter']; $current_val++; setcookie('counter',$current_val); echo $_COOKIE['counter']; header('refresh: 4'); } else { echo 'no more reloads'; } ?> PHP: This checks to see if the cookie is set, and if it isn't, it sets it Then it checks (again) to see if the cookie is set, and what the value is - if it's less than 10 (can be anything, of course) it adds 1 to the cookie, and reloads, until it reaches 10, when it goes to the else and displays whatever you want it to display there. The above function refreshes every 4 seconds, this can also be set to anything, of course.
SWEET!! Just tested it. That's pretty much what I wanted. Did you just whip it out? Man, I wish I had your brain.
Yes, I made it when you asked, it could probably be optimised a bit. It's not really hard when you know the functions, although I had to use a separate variable which might not be needed (hence the optimisation I mentioned). Glad you could use it.
You will regret mentioning that. I can hear his footsteps (@deathshadow), oh boy, all the modifications that will be needed now for it to work right... Joking aside, I can use it as is.
I wonder why this is a good solution for you now, because line 12 of the code puts the header-refresh into the header that you did NOT want according to your initial post.
I never said I didn't want a header. I said: "I do not want to use header('Refresh: 5; URL=someurl.com'); unless there's a way to stop it from looping after a few times." Posicles solution does exactly that.
I would suggest using sessions instead of cookies -- just since you probably will already have sessions by the time you do anything serious. If it's the ONLY cookie you'd be setting, fine, but if not, one session cookie beats multiple smaller ones in the "grand scheme". People who want to "skip" your little set of refreshes could also just set the cookie ahead of time, the session makes it harder to bullshit. ... and yeah, you can lose the var and simplify the logic somewhat. <?php session_start(); session_regenerate_id(); if ($_SESSION['rCount'] = isset($_SESSION['rCount']) ? max($_SESSION['rCount'] - 1, 0) : 10 ) { header('refresh:4'); // content on refresh } else { // content when not refreshing } // content either way Code (markup): Commented the three possible content types you might want something like this to output. If you're not using a whole slew of different cookies and can get away with just one, that's easy too. <?php setcookie('rCount', isset($_COOKIE['rCount']) ? max($_COOKIE['rCount'] - 1, 0) : 10 ); if ($_COOKIE['rCount']) { header('refresh:4'); // content on refresh } else { // content when not refreshing } // content either way Code (markup): The big trick being that zero is always a loose false, and non-zero is a loose true.
Depends on the expiration of the cookie, what kind of session-control you run, and/or what kind of timer you have on your stored DB-value. Doesn't have to affect revisits at all, but of course, if you put up something like this without thinking about that, it won't show the count on a revisit, that is correct - however, that might be what you want.