SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member lionking2's Avatar
    Join Date
    May 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I want to increase variable one every refresh of the page

    Hello everybody

    I want to increase variable one every refresh of the page
    Without the use of the database or session variable only

    Is it possible that

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could write to a text file.
    PHP Code:
    <?php
    $aStats 
    unserialize(@file_get_contents('stats.log'));
    if(
    is_array($aStats) === false)
    {
        
    $aStats = array(
            
    'visits'    =>    0
        
    );
    }
    $aStats['visits']++;
    file_put_contents('stats.log'$aStats);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict telos's Avatar
    Join Date
    Mar 2005
    Location
    192.168.2.34
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't believe it's possible on a "refresh" without sessions, but you could on a "submit" of a form

    What you need to do is:
    PHP Code:
    <?php
    if(!isset($_POST['var']) || !is_numeric($_POST['var'])) $_POST['var'] = 1;
    else 
    $_POST['var']++;
    ?>
    <input type="hidden" value="<?php echo $_POST['var']; ?>" />
    Obviously, that would need to be surrounded by a form and submitted.

    I can think of a few ways in theory that you might be able to do it with JavaScript, but I'm not sure if they'd work.

    You're best bet is sessions.

  4. #4
    SitePoint Addict telos's Avatar
    Join Date
    Mar 2005
    Location
    192.168.2.34
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice idea, Anthony. I got away from file-writing that sort of thing so long ago it didn't even cross my mind.

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure this is possible your page needs to maintain some kind of state, so that is either session or cookie.

    Session should be fine what a simple thing like integer count as it is light weight. I like the form post idea too but that will not cover refresh.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A session variable would be the easiest solution. Do you mind if I ask why you can't you use one here?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •