crmamx Posted February 9, 2011 Share Posted February 9, 2011 I am trying to define a session variable where I can save it and use it as the user surfs the site. I need the variable saved as $amano so I can use it in my select from/where statement and to echo within a table. This is a test trying to capture and define the variable and works, but I can't get the variable $amano into the session. If I am then I don't know how to display it. <?php> session_start(); $id = $_POST['amano']; $_SESSION['amano'] = '$amano'; echo "Pageviews = ". $_SESSION['amano']; // My effort to see what is happening. echo "<br />"; echo "AMA # = ". $_POST['amano']; // I have it just like I want it here. echo "<br />"; echo "Sessions AMA # = ".$_SESSION['amano']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/ Share on other sites More sharing options...
btherl Posted February 9, 2011 Share Posted February 9, 2011 Try this: $_SESSION['amano'] = $_POST['amano']; Make sure you only run that code if $_POST['amano'] is supposed to be stored into the session, otherwise it'll get overwritten. Then you can access $_SESSION['amano'] on subsequent calls to the script, the same way as you would have used $_POST['amano'] Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1171977 Share on other sites More sharing options...
MadTechie Posted February 9, 2011 Share Posted February 9, 2011 First of all $_SESSION['amano'] = '$amano'; // will set the session to the acual word $amano you should do this $_SESSION['amano'] = $_POST['amano']; to set it if its a number then do this to make sure it IS a number $_SESSION['amano'] = (int)$_POST['amano']; Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1171979 Share on other sites More sharing options...
crmamx Posted February 9, 2011 Author Share Posted February 9, 2011 Works perfect. Thanks. But why am I not able to echo the variable $amamo by itself? <?php> session_start(); $id = $_POST['amano']; $_SESSION['amano'] = $_POST['amano']; //$_SESSION['amano'] = (int)$_POST['amano']; // Do this if number echo "Pageviews = ". $_SESSION['amano']; //Works echo "<br />"; echo "AMA # = ". $_POST['amano']; // Works echo "<br />"; echo "AMANO:" .$amano; // This won't work and I have tried single and double //quotes, period, no period...everything I can think of to output #amano by itself echo "<br />"; echo "Sessions AMA # = ".$_SESSION['amano']; // Works ?> Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1172014 Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2011 Share Posted February 9, 2011 because you didn't define $amamo $_SESSION['amano'] is not the same as $amamo Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1172017 Share on other sites More sharing options...
btherl Posted February 9, 2011 Share Posted February 9, 2011 In some old versions of php, you could access session variables directly as $amano. This caused more problems than it solved, so the standard now is that you must use $_SESSION['amano'] instead. Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1172025 Share on other sites More sharing options...
MadTechie Posted February 9, 2011 Share Posted February 9, 2011 if you want you can just set it session_start(); $amano = (int)$_POST['amano']; $_SESSION['amano'] = $amano; echo "Pageviews = $amano"; Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1172040 Share on other sites More sharing options...
crmamx Posted February 9, 2011 Author Share Posted February 9, 2011 Just exactly what I wanted. Whether it will help me or not is another question. But at least now I can tear ahead. Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/227198-help-with-defining-the-session-variable/#findComment-1172080 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.