1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Refresh page X times and then stop using PHP

Discussion in 'PHP' started by qwikad.com, Apr 3, 2016.

  1. #1
    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.
     
    Solved! View solution.
    qwikad.com, Apr 3, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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.
     
    PoPSiCLe, Apr 3, 2016 IP
  3. Bitpalace

    Bitpalace Greenhorn

    Messages:
    53
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    13
    #3
    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.
     
    Bitpalace, Apr 3, 2016 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    Can you point me in the right direction. Is there an online example of how it's done?
     
    qwikad.com, Apr 3, 2016 IP
  5. #5
    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.
     
    PoPSiCLe, Apr 3, 2016 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #6
    SWEET!! Just tested it. That's pretty much what I wanted. Did you just whip it out? Man, I wish I had your brain.
     
    qwikad.com, Apr 3, 2016 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    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.
     
    PoPSiCLe, Apr 3, 2016 IP
  8. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #8
    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.
     
    qwikad.com, Apr 3, 2016 IP
  9. Bitpalace

    Bitpalace Greenhorn

    Messages:
    53
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    13
    #9
    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.
     
    Bitpalace, Apr 4, 2016 IP
  10. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #10
    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.
     
    qwikad.com, Apr 4, 2016 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    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.
     
    Last edited: Apr 4, 2016
    deathshadow, Apr 4, 2016 IP
    qwikad.com likes this.
  12. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #12
    The above won't work unless you want to limit revisits.
     
    NetStar, May 28, 2016 IP
  13. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #13
    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.
     
    PoPSiCLe, May 28, 2016 IP