Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Run a function once per session

    Hi

    I have a phpbb3 based site and I would like to run this script:

    PHP Code:
    <!-- IF not S_USER_LOGGED_IN and not S_IS_BOT and not S_IN_UCP -->
    <
    div id="pm-popup" class="panel">
    <
    a href="ucp.php?mode=register" style="padding-top: 3px; padding-bottom: 3px;">Welcome to PhpBB3 BBCodesClick here to register</a><br />
    </
    div></div>
    <!-- ENDIF --> 
    It is contained within a .html template file, but I would only like it to appear once per visitor, per session.

    Any ideas

    Thanks

    Antony

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    Set a cookie once it has been displayed and then check for that cookie the next time before displaying it.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi
    I have a javascript code:

    PHP Code:
    // ++++++++++++++++++++++++++++++++++++++++++ 
    // Run Once Per Session 
    // 
    // Replace the alerts by functions that need to 
    // be run once per session. 
    // 
    // Written by: Michael Regan 
    // Website   : www.owt4nowt.ca 
    // 
    // Released under the GPL. 
    // ++++++++++++++++++++++++++++++++++++++++++ 
    var key_value "myTestCookie=true"
    var 
    foundCookie 0

    // Get all the cookies from this site and store in an array 
    var cookieArray document.cookie.split(';'); 

        
    // Walk through the array 
        
    for(var i=0;cookieArray.length;i++) 
            { 
                   var 
    checkCookie cookieArray[i]; 
            
    // Remove any leading spaces 
                   
    while (checkCookie.charAt(0)==' '
                   { 
                     
    checkCookie checkCookie.substring(1,checkCookie.length); 
                   } 
            
            
    // Look for cookie set by key_value 
                    
    if (checkCookie.indexOf(key_value) == 0
                   { 
                      
    alert("Found Cookie "); 
                
    // The cookie was found so set the variable 
                       
    foundCookie 1
                   } 
        } 
        
    // Check if a cookie has been found 
        
    if ( foundCookie == 0
        { 
            
    // The key_value cookie was not found so set it now 
            
    document.cookie key_value
            
    alert("Setting Cookie"); 
        } 
    Unfortunately, being unexperienced with coding I am not sure how to implement it in the file.

    The file is a html template file.

    Thanks

    Antony

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you're using an actual session (which you should be, cookies are unreliable and unsafe) than its as easy as checking to see if its already been executed:
    PHP Code:
    <?php

    session_start
    ();
    if (isset(
    $_SESSION['POPUPEXEC']) && $_SESSION['POPUPEXEC'])
    {
        
    // Draw this out however you need to, I'm just going to print it:
        
    print <<<POPUP
            <!-- IF not S_USER_LOGGED_IN and not S_IS_BOT and not S_IN_UCP -->
            <div id="pm-popup" class="panel">
            <a href="ucp.php?mode=register" style="padding-top: 3px; padding-bottom:                 3px;">Welcome to PhpBB3 BBCodes. Click here to register</a><br />
            </div></div>
            <!-- ENDIF -->  
    POPUP;
    // Note that end of heredoc MUST be on a new line and MUST have no space padding

        
    $_SESSION['POPUPEXEC'] = true;
    }

    //...
    If you're using a custom session handler, its a matter of flagging that this event has occurred in you're persistent storage. Logic is similar to above, though $_SESSION would no longer be available to you.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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