Jump to content

SESSION variables


ICubis2

Recommended Posts

It is my understanding that for post data you need to save them into another session value to maintain them over pages.

Okay... what did I just say?

Here is what I came up with (I usually set each individually but I found this in another form ):

 

page1.php

 

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <title>Page 1 - Test Session and Post</title>
</head>

<body>
<h1>Test Session and Post</h1>
<h2>Page 1 - Form</h2>

<form name="theForm" action="page2.php" method="post">
<p>
  <label for="first">First: </label>
  <input class="text" type="text" name="first" id="first" size="55" value="<?=$_SESSION['formData']['first']?>" />
</p>
<p>
  <label for="second">Second: </label>
  <input class="text" type="text" name="second" id="second" size="55" value="<?=$_SESSION['formData']['second']?>" />
</p>
<p>
   <input type="submit" name="SUBMIT" value="Submit" />
</p>
</form>

</body>
</html>

 

page2.php

<?php
session_start();
$_SESSION['formData'] = $_POST;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <title>Page 2 - Test Session and Post</title>
</head>

<body>
<h1>Test Session and Post</h1>
<h2>Page 2 - Form Poster</h2>

<p>
  First: <?=$_SESSION['formData']['first']?>
</p>
<p>
  Second: <?=$_SESSION['formData']['second']?>
</p>
<p>
   <a href="page1.php">To Page 1</a>
</p>
</body>
</html>

 

The important part is in Page 2:  $_SESSION['formData'] = $_POST;

That will move any post date into a session variable and won't get destroyed by another post.

Then you would refer to the form data as $_SESSION['formData']['first'] where 'first' is the name of an input field.

 

You can verify this because if you run  page1.php which goes to page2.php with the post data then click link to go back to page1.php you will see your original values again on page1

- now -

Run page2.php and it willl show no values for First and Second.  That is because there was no post data coming in.

If you now click to return to page1...  it also will have no values... you have just destroyed your original data.

- so -

In page2.php before the statement $_SESSION['formData'] = $_POST; you probably want to check to see if there is any post data before destroying any previous values:  if(isset($_POST)) ...

 

Hope I didn't confuse the issue.  Simply put... You need to save the post data into your own session variable.

 

Link to comment
Share on other sites

Just be careful because what I showed will destroy all the postData.

Best to save each form value one at a time into sessions so you don't clear value if you don't want to.

I have examples if you need it.

 

Good Luck!

Link to comment
Share on other sites

 

The information that Krash posted was probably due to specific problems he was having and are not generally applicable to anything else and in fact since he specifically mentioned SMF and that has nothing to do with what you are doing, I would suggest that he didn't even read your thread before replying.

 

Actually, I did read (and understand) the OP's posts - would not have replied otherwise.  I had a similar problem with a project this past week.  The point I tried to make is that there are external factors that can interfere with session variables, even if the code is correct.

 

 

Link to comment
Share on other sites

OK, I have found the line(s) of code that are killing the session variables in my code.

 

It is in the section that builds my navigation menu.  Again, understand that I did not explicitly write this code - Yahoo! Sitebuilder did the writing.

 

Each menu option within the table is written like this:

 

<tr><td>
<a href="index.html">
<img name="i5" src="sitebuilder/images/navbar-0-inactive-39952.png" border="0" width="163" height="22" alt=""/>
</a></td></tr>

<tr><td height="17" width="0"> 
<!--<img src="" height="17" width="1" alt="">-->
</td></tr>

 

The line that is commented out is the line causing the problem.  I'm no expert on HTML.  Anyone see what parameters within this line might be causing the problem?

 

 

Link to comment
Share on other sites

I don't get this.  You are saying that the HTML is causing your sessions problems?

The only thing I can think of is that the HTML is invalid (could be since there is no src value) and thus terminating whole process.

So nothing is getting posted?

Link to comment
Share on other sites

The presence of this code in itself does not kill my session variables.  I can echo them after the nav bar code is executed and they are fine.

 

They do not get destroyed until the SUBMIT button of this displayed page is activated.  This is the action that then destroys the session variables.

 

I believe everything not dependent on these variables is fine.

Link to comment
Share on other sites

 

The line that's causing the problem serves no purpose - it may be there just as a spacer to keep the blank table cell open.  Delete the line - if it causes the cell to collapse and screws up the formatting, put a &nbsp in it.

 

Doesn't seem likely that line is affecting the session variables, but get rid of it and see what happens.

 

 

Link to comment
Share on other sites

I already did and by commenting each of them out is what fixed my session variable problem.  When I put just one back in then I lose the session variables again.

 

I saw no formatting issues removing these lines of code.

 

I was hoping someone much more knowledgeable than I would recognize what might be going on, but I'm just happy I can move on past this problem issue now.

 

Thanks all.

Link to comment
Share on other sites

 

Just a theory, but from my recent experience with a similar problem, and many previous problems with Firefox, I would guess that FF was unable to execute the crappy html in the <img> tags, which will cause it to behave unpredictably.  In this case, it dumped the session id, which caused the session variables to disappear.  If you run the original code in IE, it will probably work fine.  IE is much more tolerant of sloppy code (believe me, I know).

 

The table row that included the bad code is there just as a spacer between the lines that are displayed above and below it, and the empty <img> tags apparently were used to keep the table cell open.  Some browsers will collapse an empty <tr><td> </td></tr>, and you'll lose the space between lines.  It's a very bad way of doing something very simple.

 

 

Link to comment
Share on other sites

The form code you have posted works for me using FF4 and keeps the session variables that I have set.

 

I display a form, get user data submitted, then display a next form and after the user submits this next form all...

 

You have only posted code for one form page. Which of those forms that you mentioned is the code you have posted? We really need to have enough of your code that reproduces the problem.

 

However, I'm going to guess that you are changing the host-name (www. vs no www.) or path in the URL and the session cookie parameters are not set up to match the different URL's. What does a phpinfo() statement show for the following settings -

 

session.cookie_path

session.cookie_domain

register_globals

 

Also, what does a 'view source' of your page show, because the sitebuilder software may be adding things into the final output that is causing this problem.

 

I would also recommend echoing the value that session_id() gives, after the session_start() statement, on all relevant pages so that you can see if the id changes (it should be the same for all pages.)

Link to comment
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.