samoht Posted October 26, 2009 Share Posted October 26, 2009 Hello all, Is there any way I can kill a $_SESSION when I user closes my web page or navigates away? Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 26, 2009 Share Posted October 26, 2009 No, because that is the domain of the web browser. Sessions have their own expiration mechanism, and while you can try and get in the middle of navigation using javascript, users really don't like it for good reason, and may avoid your site simply because you're interfering in things that aren't your business, as a website purveyor. Quote Link to comment Share on other sites More sharing options...
samoht Posted October 26, 2009 Author Share Posted October 26, 2009 OK, I'm cool with that. I don't want to be invasive. Here is my problem: I have a client that wants a US version of their site and an International version. The US version is identical except it is missing any reference to a certain medical procedure. My solution was to create a little form on the index page that set a session variable of USA or International for the user's $_SESSION['country'] - then I just put conditional statements around content I wanted to not show for US folks. My problem is that every time a user clicks on a internal link they are shown the form - unless I code the form like this: <?php // this goes on the very top of the index.php file in the template you are using if(!isset($_POST['location']) && !isset($_SESSION)){ ?> <!-- //Place the HTML for the landing page below here --> <html> <head> <meta http-equiv="Content-Language" content="en-us" /> <title>Surgimap Spine Choose Your Site</title> </head> <!-- // make sure you have the right path speced for the images --> <body style="background: url(welcomepage/images/background.gif) repeat-x #4fbbef; font-family: Helvetica, Arial, sans-serif; color: #474845; "> <div id="container" style="margin-top: 200px; text-align: center;"> <p align="center"><img border="0" src="welcomepage/images/logo.gif" ></p> <div align="center"> <p align="center"> </p> <!-- change the action to "index.php" for live site--> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table border="0" cellpadding="0" cellspacing="0" width="800" id="table1"> <tr> <td><input type="submit" value="USA Website" name="location" /></td> <td><input type="submit" value="International Website" name="location" /></td> </tr> </table> </form> </div> </div> </body> </html> <?php }else{ session_start(); $_SESSION['country'] = $_POST['location']; $country = $_SESSION['country']; // this ends the head section modification of the index.php ?> but this will not allow a user to change locations until the SESSION is terminated. Is there a better way to do this? Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 2, 2009 Share Posted November 2, 2009 Well it looks like you are misunderstanding how to use sessions. You should have your session_start() call at the top of every page. You can't read a previously set session variable without doing session_start() first. Quote Link to comment Share on other sites More sharing options...
samoht Posted November 2, 2009 Author Share Posted November 2, 2009 I only have one page technically (index.php) and this is it. But I did move the session_start() to the very top and I also moved the splash starter form to the bottom of the page instead of the top. However, I still needed to refresh the page to get the Session conditionals to work when the actual page first appears. Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 2, 2009 Share Posted November 2, 2009 Let's see your current code. Quote Link to comment Share on other sites More sharing options...
samoht Posted November 2, 2009 Author Share Posted November 2, 2009 <?php session_start(); function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } ?> <?php // this goes on the very top of the index.php file in the template you are using if(isset($_POST['location']) && !isset($_SESSION['country'])){ $_SESSION['country'] = $_POST['location']; } if(isset($_SESSION['country'])){ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>" > <head> <jdoc:include type="head" /> <script type="text/javascript" src="<?php echo $this->baseurl ?>/media/system/js/modal.js"> </script> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/media/system/css/modal.css" type="text/css" /> <?php $this->setGenerator('TNTmax'); ?> <meta name="verify-v1" content="49AdKyXiy2RsoyhmTG8MmweqidsW1vWtby3av6uusvs=" /> <script type="text/javascript"> // <!-- window.addEvent('domready', function(){ var JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: 'false'}); }); // --> </script> <script type="text/javascript"> // <!-- window.addEvent('domready', function() { SqueezeBox.initialize({}); $$('a.modal').each(function(el) { el.addEvent('click', function(e) { new Event(e).stop(); SqueezeBox.fromElement(el); }); }); }); // --> </script> <link rel="stylesheet" href="templates/<?php echo $this->template ?>/css/template.css" type="text/css" /> <script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template?>/scripts/site.js" type="text/javascript"></script> <script type="text/javascript"> //<!-- Tips.implement({ initialize: function(elements, lasthope,options){ this.setOptions(options); this.lasthope = lasthope; this.toolTip = new Element('div', { 'class': 'cf_'+this.options.className + '-tip', 'id': this.options.className + '-tip-' + this.options.elementid, 'styles': { 'position': 'absolute', 'top': '0', 'left': '0', 'visibility': 'hidden' } }).inject(document.body); this.wrapper = new Element('div').inject(this.toolTip); $$(elements).each(this.build, this); if (this.options.initialize) this.options.initialize.call(this); }, build: function(el){ el.$tmp.myTitle = (el.href && el.getTag() == 'a') ? el.href.replace('http://', '') : (el.rel || false); if (el.title){ var dual = el.title.split('::'); if (dual.length > 1){ el.$tmp.myTitle = dual[0].trim(); el.$tmp.myText = dual[1].trim(); } else { el.$tmp.myText = el.title; } el.removeAttribute('title'); } else { var dual = this.lasthope.split('::'); if (dual.length > 1){ el.$tmp.myTitle = dual[0].trim(); el.$tmp.myText = dual[1].trim(); } else { el.$tmp.myText = el.title; } } if (el.$tmp.myTitle && el.$tmp.myTitle.length > this.options.maxTitleChars) el.$tmp.myTitle = el.$tmp.myTitle.substr(0, this.options.maxTitleChars - 1) + "…"; el.addEvent('mouseenter', function(event){ this.start(el); if (!this.options.fixed) this.locate(event); else this.position(el); }.bind(this)); if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this)); var end = this.end.bind(this); el.addEvent('mouseleave', end); el.addEvent('trash', end); }, start: function(el){ this.wrapper.empty(); if (el.$tmp.myTitle){ this.title = new Element('span').inject(new Element('div', {'class': 'cf_'+this.options.className + '-title'}).inject(this.wrapper)).setHTML(el.$tmp.myTitle); } if (el.$tmp.myText){ this.text = new Element('span').inject(new Element('div', {'class': 'cf_'+this.options.className + '-text'}).inject(this.wrapper)).setHTML(el.$tmp.myText); } $clear(this.timer); this.timer = this.show.delay(this.options.showDelay, this); } }); window.addEvent('domready', function() { $ES('.tooltipimg').each(function(ed){ var Tips2 = new Tips(ed, $E('div.tooltipdiv', ed.getParent().getParent()).getText(), {elementid:ed.getParent().getParent().getFirst().getNext().getProperty('id')+'_s'}); }); if($chk($E('input[type=file]', $('ChronoContact_mywebpageReg')))){ $('ChronoContact_mywebpageReg').setProperty('enctype', 'multipart/form-data'); } }); //--> </script> <?php if($this->countModules('right') == 0) $contentwidth = "100"; if($this->countModules('right') >= 1) $contentwidth = "80"; ?> </head> <?php $user =& JFactory::getUser(); $myTitle =& JDocument::getTitle(); if($myTitle == "mywebpage | Download"){ if($user->id>0){ echo '<body onload="javascript:window.location.href=\'http://www.mywebpage.com/mywebpage_api/download.php?login='.$user->username.'&file=mywebpageSetup.exe\';">'."\n"; }else{ header ('Location: http://www.mywebpage.com/index.php?option=com_content&view=article&id=72&Itemid=87'); } }else{ echo "<body>\n"; } ?> <?php echo '<div style="display:none;visibility:hidden;">'.$_SESSION['country'].'</div>'."\n"; ?> <div id="wrap"> <div id="header"> <a href="index.php"><span id="logo"> </span></a> <div id="top"><jdoc:include type="modules" name="top" style="xhtml" /> <?php /* <jdoc:include type="modules" name="top" /></div> <div id="AccordionContainer" class="AccordionContainer"> <div onclick="runAccordion(1,101);"> <div class="AccordionTitle" onselectstart="return false;"> <span> <?php $user = & JFactory::getUser(); $type = (!$user->get('guest')) ? 'logout' : 'login';?> <?php if( $type == 'logout' ) : ?> <img src="images/stories/logout.jpg" border="0" alt="account" title="account" align="right" onmouseover="this.src='images/stories/logout_hover.jpg';" onmouseout="this.src='images/stories/logout.jpg';" /></span> <?php else : ?> <img src="images/stories/account.jpg" border="0" alt="account" title="account" align="right" onmouseover="this.src='images/stories/account_hover.jpg';" onmouseout="this.src='images/stories/account.jpg';" /></span> <?php endif; ?> </div> </div> <div id="Accordion1Content" class="AccordionContent"> <jdoc:include type="modules" name="login" style="xhtml" /> </div> </div>*/ ?> </div> <div id="menu-bottom"><div class="menu"><jdoc:include type="modules" name="user1" /></div></div> </div> <div id="container"> <?php if($this->countModules('right')) : ?> <div id="right"> <jdoc:include type="modules" name="right" style="xhtml" /> </div> <?php endif; ?> <?php if($this->countModules('banner')) : ?> <div id="banner"><jdoc:include type="modules" name="banner" style="xhtml" /></div> <?php endif; ?> <div id="content<?php echo $contentwidth; ?>"> <jdoc:include type="component" style="xhtml" /> </div> <div id="bottom"> <div id="user2"><jdoc:include type="modules" name="user2" style="xhtml" /></div> <div id="user3"><jdoc:include type="modules" name="user3" style="xhtml"/></div> <?php if($this->countModules('user5')) : ?> <div id="user5"><jdoc:include type="modules" name="user5" style="xhtml"/></div> <?php endif; ?> <div id="user4"><jdoc:include type="modules" name="user4" style="xhtml"/></div> </div> </div> <div id="footer"> <div id="bottom-menu"><jdoc:include type="modules" name="bottom" style="xhtml" /></div> <div id="footer2"><p>c2009 Nemaris <br /> Designed and Powered by <a href="http://www.tntmax.com" title="TNTMAX">TNTMAX</a></p></div></div> </div> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-9296300-1"); pageTracker._trackPageview(); } catch(err) { } </script> </body> </html> <?php }else{ ?> <!-- //Place the HTML for the landing page below here --> <html> <head> <meta http-equiv="Content-Language" content="en-us" /> <title>mywebpage Spine Choose Your Site</title> </head> <!-- // make sure you have the right path speced for the images --> <body style="background: url(welcomepage/images/background.gif) repeat-x #4fbbef; font-family: Helvetica, Arial, sans-serif; color: #474845; "> <div id="container" style="margin-top: 200px; text-align: center;"> <p align="center"><img border="0" src="welcomepage/images/logo.png" ></p> <div align="center"> <p align="center"> </p> <!-- change the action to "index.php" for live site--> <form method="post" action="/"> <table border="0" cellpadding="0" cellspacing="0" width="900" id="table1"> <tr> <td><input type="submit" value="USA Website" name="location" /></td> <td><input type="submit" value="International Website" name="location" style="float: right" /></td> </tr> </table> </form> </div> </div> </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
nehamadurai Posted November 2, 2009 Share Posted November 2, 2009 you have something called the unset _SESSION () and the Destroy_SESSION() try using those. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.