...

View Full Version : Run a function once per session



tonyjms2005
10-25-2009, 04:42 PM
Hi

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


<!-- 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 -->

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

Sussex_Chris
10-25-2009, 07:04 PM
Set a cookie once it has been displayed and then check for that cookie the next time before displaying it.

tonyjms2005
10-25-2009, 08:15 PM
Hi
I have a javascript 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;i < 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

Fou-Lu
10-25-2009, 08:40 PM
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

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum