Jump to content

Variable wont store in session


thecactusjack

Recommended Posts

Hello, new to php. having trouble storing one particular variable into the session.

Here is where I want to echo the result once the user has returned to the page:

<?php
if ($_POST['submit']) {
$show = $result;
$_SESSION['show'] = $show;

else {
$show = $_SESSION['show'];
echo "The last result was: ". $show;
}
?>

 

//inbetween here a form is created

Here is where the $result variable is created:

<?php 
$decision = $_POST['option'][array_rand($_POST ['option'])];
if (!$decision){
$result = $_POST['default'];
echo $result;
}

else{
$result = $decision;
echo $result;
}
?>




Now the strange thing is if I change "$show = $result;"
to "$show = $_POST['default']; 
That does store and displays when the user returns t the page.

 

So everything else works apart from "$result" being stored. 

Any help would be great! spent hours going mad on this!

 

Link to comment
https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/
Share on other sites

Hi yeah I have :

 

 

<?php 
session_name('Session1');
session_set_cookie_params(6000);
session_start(); 
 
?>
 
All at the start of my code, still the "$result" variable does not store or display, however "$_POST['default']" does, any idea what the issue might be?
Thanks 
 
*Edit "$result" variable

As the function name implies, you need session_start() before you use session variables

 

Yes I do have this at the very start.

Like I said if I store the "$_POST['default']" variable as opposed to the "$result" variable in the session, it stores and echos once the user returns to the page as I intended.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.