Jump to content

Recommended Posts

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.