thecactusjack Posted April 14, 2013 Share Posted April 14, 2013 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:<?phpif ($_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 More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 session_start() must be at least in the file. preferably the first line of each file. Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424704 Share on other sites More sharing options...
thecactusjack Posted April 14, 2013 Author Share Posted April 14, 2013 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 Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424707 Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 Honestly your code and description doesn't make any sense. Start debugging by echoing every variable when you need to use it and see what it is. Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424710 Share on other sites More sharing options...
Barand Posted April 14, 2013 Share Posted April 14, 2013 As the function name implies, you need session_start() before you use session variables Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424713 Share on other sites More sharing options...
thecactusjack Posted April 14, 2013 Author Share Posted April 14, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424719 Share on other sites More sharing options...
Jessica Posted April 14, 2013 Share Posted April 14, 2013 Don't PM members asking for help, that's what the forum is for. Obviously $result isn't set so figure out why. Link to comment https://forums.phpfreaks.com/topic/276941-variable-wont-store-in-session/#findComment-1424725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.