473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Storing $_SESSION variables

KeredDrahcir
426 256MB
I've got a problem. I'm sure I'm being really stupid but I can't seem to be able to rertive a $_SESSION variable.

I run through the code three times using a variable called $setup which I post each time as reset. The first time I go through the code, $setup has no value. The second time, it has the value of 1 and a SESSION variable called value 1 gets set from a POST. The third time, setup has the value of 2. I set a SESSION variable to the value of another POST but the orignal SESSION variable seems to have lost its value.

This is the code when the page is loaded.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   session_start();
  3.   $setup=$_POST['reset'];
  4.   if ($setup==NULL)
  5.   {
  6.     $setup=0;
  7.   }
  8.   elseif ($setup==1)
  9.   {
  10.     $_SESSION['value1']=$_POST['value1'];
  11.     $value1=$_SESSION['value1'];
  12.   }
  13.   elseif ($setup==2)
  14.   {
  15.     $value1=$_SESSION['value1'];
  16.     $_SESSION['value2']=$_POST['value2'];
  17.     $value2=$_SESSION['value2'];
  18.   }
  19. ?>
  20.  
When setup is one I can print out value1 however when setup is two is use this code
Expand|Select|Wrap|Line Numbers
  1. echo $value2 . " " . $value1 . ".";
  2.  
All I get is value2 followed by a dot. Am I doing something wrong here?
Sep 2 '13 #1

✓ answered by KeredDrahcir

If would help if I took the PHP out of the Reset button at the bottom. I must have been getting confused with PHP and JavaScript. I was destroying the SESSION each time.
As I said "I'm probably being really stupid" as I was.

9 1704
KeredDrahcir
426 256MB
Can anyone help me. I can't get any further until this is sorted out. I don't often get large amounts of time so while I have I want to put it to good use.
Sep 4 '13 #2
Exequiel
288 256MB
Can you post your fields or form? I've created like your code, here it is,
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     session_start();
  3.     ob_start();
  4.     ?>
  5.  
  6.     <form method="post">
  7.         value1: <input type="text" name="value1" />  
  8.         value2: <input type="text" name="value2" />  
  9.     <input type="submit" name="submit" value="Submit" />
  10.     <br />
  11.     <hr/>
  12.      <?php 
  13.         if(isset($_POST['submit']))
  14.         {
  15.             $_SESSION['value1']=$_POST['value1'];
  16.             $_SESSION['value2']=$_POST['value2'];
  17.             echo 'value1 : '.$_SESSION['value1'].'<br>value2 :  '.$_SESSION['value2'];
  18.         }
  19.     ?>
Sep 5 '13 #3
KeredDrahcir
426 256MB
Here's the full program.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   session_start();
  3.   $setup=$_POST['reset'];
  4.   if ($setup==NULL)
  5.   {
  6.     $setup=0;
  7.   }
  8.   elseif ($setup==1)
  9.   {
  10.     $_SESSION['value1']=$_POST['value1'];
  11.     $value1=$_SESSION['value1'];
  12.   }
  13.   elseif ($setup==2)
  14.   {
  15.     $value1=$_SESSION['value1'];
  16.     $_SESSION['value2']=$_POST['value2'];
  17.     $value2=$_SESSION['value2'];
  18.   }
  19. ?>
  20. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  21.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  22.  
  23. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  24.  
  25.   <head>
  26.     <title>
  27.       Program
  28.     </title>
  29.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
  30.     <meta name="author" content="Richard Derek Young"/>
  31.   </head>
  32.  
  33.   <body>
  34.     <h1>
  35.       Program
  36.     </h1>
  37.     <p>
  38.       Use this program to create it.
  39.     </p>
  40. <?php
  41.     if ($setup==0)
  42.     {
  43. ?>
  44.       <p>
  45.         <form method="post" name="world" action="program.php">
  46.           Enter value 1. <br/>
  47.           <input type="text" name="value1"/>
  48. <?php
  49.           $setup++;
  50. ?>
  51.           <input type="hidden" name="reset" value="<?=$setup;?>"/>
  52.           &nbsp; <input type="submit" value="Next"/>
  53.         </form>
  54.       </p>
  55. <?php
  56.     }
  57.     elseif ($setup==1)
  58.     {
  59.       $setup++;
  60. ?>
  61.       <form method="post" name="description" action="program.php">
  62.         <input type="hidden" name="reset" value="<?=$setup;?>"/>
  63.         <p>
  64.           <input type="radio" name="value2" value="1" onclick="javascript:document.description.submit()"/> &nbsp; Option 1 <?=$value1;?>.
  65.         </p>
  66.         <p>0
  67.           <input type="radio" name="value2" value="2" onclick="javascript:document.description.submit()"/> &nbsp; Option 2 <?=$value1;?>
  68.         </p>
  69.         <p>
  70.           <input type="radio" name="value2" value="3" onclick="javascript:document.description.submit()"/> &nbsp; Option 3 <?=$value1;?>.
  71.         </p>
  72.         <p>
  73.           <input type="radio" name="value2" value="4" onclick="javascript:document.description.submit()"/> &nbsp; Option 4 <?=$value1;?>.
  74.         </p>
  75.         <p>
  76.           <input type="radio" name="value2" value="5" onclick="javascript:document.description.submit()"/> &nbsp; Option 5 <?=$value1;?>.
  77.         </p>
  78.         <p>
  79.           <input type="radio" name="value2" value="6" onclick="javascript:document.description.submit()"/> &nbsp; Option 6 <?=$value1;?>.
  80.         </p>
  81.         <p>
  82.           <input type="radio" name="value2" value="7" onclick="javascript:document.description.submit()"/> &nbsp; Option 7 <?=$value1;?>.
  83.         </p>
  84.       </form>
  85. <?php
  86.     }
  87.     elseif ($setup==2)
  88.     {
  89.       $setup++;
  90.       echo $value2 . " " . $value1 . ".";
  91. ?>
  92.       <p>
  93.         More Stuff.
  94.       </p>
  95. <?php
  96.     }
  97. ?>
  98.     <p>
  99.       <button onclick="<? session_destroy();?>window.location.href = 'program.php';">Reset</button>
  100.     </p>
  101.   </body>
  102. </html>
  103.  
Is that what you were asking for? It's on line 90 that I only get value2 echoed.
Sep 6 '13 #4
Dormilich
8,658 Expert Mod 8TB
best thing you can do is print out the $_POST and $_SESSION arrays to look at the data directly.

turning on error reporting might help as well.
Sep 7 '13 #5
KeredDrahcir
426 256MB
How do I do that? Is there a special piece of code to print the arrays?
I take it you can't see anything obvious wrong with the code?
Sep 7 '13 #6
Dormilich
8,658 Expert Mod 8TB
Is there a special piece of code to print the arrays?
print_r(), var_dump(), var_export()
Sep 7 '13 #7
KeredDrahcir
426 256MB
It gives me .Array ( [value2] => value2 ) where value2 is the expected value. Do you know where the first $_SESSION is getting lost? Am I setting it wrong?
Sep 8 '13 #8
Dormilich
8,658 Expert Mod 8TB
also check the $_POST array for each request.
Sep 8 '13 #9
KeredDrahcir
426 256MB
If would help if I took the PHP out of the Reset button at the bottom. I must have been getting confused with PHP and JavaScript. I was destroying the SESSION each time.
As I said "I'm probably being really stupid" as I was.
Sep 8 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott Lyon | last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to rewrite it) an application that is suddenly giving me grief. The reason I say suddenly, is because we're in the...
3
by: Jessica Loriena | last post by:
I'm trying to write a simple "register form / validate and store in database / show welcome screen" application with ASP.Net. With conventional ASP, I used Session variables and it went something...
8
by: Dave Wurtz | last post by:
All, I'm new to ASP development and I have a basic design question: Is it ok to store business objects to session variables or is there a better way to keep object information? For example,...
31
by: Harry Simpson | last post by:
I've come from the old ASP camp where session variables were not used. When i started using ASP.NET in 2001, I started using them again because it was ok from what I'd read. I've been merrily...
3
by: Galina Grechka | last post by:
Hi, I have a really bad problem with ASP.net application. It seemed fine until it was tested in multisession environment. When 2 users access the same aspx page at the same time sometimes the...
9
by: Randy | last post by:
Hello, I'm having a strange problem. I've got a .NET web app which uses Session variables. Sometime, not all the time, they get cross threaded...that is...one user will have another user's Session...
12
by: Alex D. | last post by:
is it possible? pros and cons?
4
by: Andy Kasotia | last post by:
I have read couple of articles warning against the use of storing VB COM objects (Apartment Threading) in Session Variables due to the fact that these variables could go bad. My question is...
7
by: Erik | last post by:
I have an application that uses sessions variables a lot but one I publish the application on the prod server these variables are lost. The application is written i c# 2.0 and I've set the...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.