spires Posted September 2, 2012 Share Posted September 2, 2012 Hi I'm trying to save a $_GET[] in to a session. It's working on the page that is GETTING the variables from the url, but soon as I click to the next page the SESSION seems to empty out. Any Ideas? Header Script <?PHP session_start(); require_once('../../../includes/initialize.php'); global $page; global $metTitle; global $metDesc; global $metKey; $getFM = $_GET['fm']; $getKW = $_GET['kw']; $getGR = $_GET['gr']; if(!empty($getFM)){ $from = $_SESSION['FM'] = $getFM; }else{ } if(!empty($getGR)){ $interest = $_SESSION['GR'] = $getGR; }else{ } if(!empty($getKW)){ $ref = $_SESSION['KW'] = $getKW; }else{ } echo $getFM.' - '.$interest.' - '.$from.' - '.$ref; ?> Thanks Link to comment https://forums.phpfreaks.com/topic/267925-session-question/ Share on other sites More sharing options...
Christian F. Posted September 2, 2012 Share Posted September 2, 2012 Do you start the session on that next page? You'll need to show us the code for that page as well. Also, don't use global; It's messy, and is ripe for making your code buggy. In your instance, I'd guess that constants is what you're looking for. PS: You can safely remove the empty else blocks, as they literally don't do anything (except adding complexity to your code). Link to comment https://forums.phpfreaks.com/topic/267925-session-question/#findComment-1374738 Share on other sites More sharing options...
spires Posted September 2, 2012 Author Share Posted September 2, 2012 Hi Thanks for your reply. This is the header for every page. So what happens here happens all over. I'm guessing that the SESSION is getting populated with the GET. But as the next page has not GET, the SESSION end up blank. Which is why I have the IF. But that dose not seem to work. Global. How do I pass a variable from my main file to the header file, without global ? Thanks Link to comment https://forums.phpfreaks.com/topic/267925-session-question/#findComment-1374741 Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2012 Share Posted September 2, 2012 The global keyword is only used inside of function definitions (and even then it should not be used.) Everywhere else, it doesn't have any effect at all (except to take up processing time.) Link to comment https://forums.phpfreaks.com/topic/267925-session-question/#findComment-1374742 Share on other sites More sharing options...
DavidAM Posted September 3, 2012 Share Posted September 3, 2012 This line: $getFM = $_GET['fm']; Is setting the variable to null if the value is not in the URL, then you set the session value to this variable (which sets it to null as well). It is also throwing a warning about an undefined index. You need to turn on error reporting. To do what you are trying to do, you could try: $getFM = (isset($_GET['fm']) ? $_GET['fm'] : (isset($_SESSION['FM']) ? $_SESSION['FM'] : '')); $_SESSION['FM'] = $getFM; Which is the same as: if (isset($_GET['fm'])) { $getFM = $_GET['fm']; } else if (isset($_SESSION['FM'])) { $getFM = $_SESSION['FM']; } else { $getFM = ''; } $_SESSION['FM'] = $getFM; Link to comment https://forums.phpfreaks.com/topic/267925-session-question/#findComment-1374784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.