Accept Disclaimer and Redirect or Sessions in Joomla
Customer wants users to accept a disclaimer one time each visit to the site, then be redirected to the page. So if the user accepts the disclaimer, a session variable will be written and they will be redirected. This is a Joomla site, so the framework is already built for sessions and I wouldn't want to use regular php sessions.
So first, we'll put a checker on the page to see if they've accepted the disclaimer. I just included this php page in a custom code module on the menu item.
//load joomla files
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// get the session
$session =& JFactory::getSession();
$defaultvalue = 'reject';
$discl = $session->get('discl', $defaultvalue, 'disclaimer'); //get discl session value or 'reject' if not set
if($discl <> 'accept'){
$link = JRoute::_('index.php?option=com_content&view=article&id=43&Itemid=52');
$mainframe->redirect($link);//route them back to disclaimer page
}
So we have to load the Joomla files or none of the built in stuff will work. Then we get the session with JFactory::getSession. When doing a ->get, we can define a default variable. It's not necessary as if it's not set, the value will be null, but I set one just because. So then we test and see if the value is set to accept, and if not, route them to the disclaimer page. JRoute is pretty neat cause you put in the real url and it'll change it to the SEF url if you have that enabled. Pretty!
Now on the disclaimer page, they will accept and we will write the session variable.
Get the session then add our own variable using ->set('sessionName', 'sessionValue', ['uniqueNameSpace']). You may have noticed the namespace defined in the last script. The namespace isn't required, if you don't define it, any session variables you state here will be added to the default namespace. // load joomla files
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// get the session
$session =& JFactory::getSession();
$session->set('discl', 'accept', 'disclaimer');//set accept
$link = JRoute::_('index.php?option=com_content&view=article&id=1&Itemid=49');
$mainframe->redirect($link);//route them to page
The way I implemented this in the Joomla site was create a page, add a menu item, then add a custom code module including this file to the page. It redirects so quickly, it's barely even noticeable.
So the result is user comes to site, session variable isn't 'accept' so they are redirected to the disclaimer. They accept and are directed to the site. They are free to move about the site until they close the browser or the session times out, at which point, when they come back, they must accept again.
1 comment:
Hi,
to what Joomla versions would this apply?
thx,
BC
Post a Comment