Connecting Tech Pros Worldwide Forums | Help | Site Map

How many sessions can I have at one time

Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#1: Jun 25 '09
I'm using this in a form:
Expand|Select|Wrap|Line Numbers
  1. value='"; if (isset($_POST['title'])) echo $_POST['title']; echo "'
The idea was to save the inputs so that if a user had to go back and make a correction they wouldn't have to re-enter all of their information. Nice idea, but it doesn't work. At least not with the above method.

I'm not quite sure what to do at this point. I'm already using a login in session that keeps up with the user's name, user_id, city, etc. But is it possible to use a session to keep up with what they enter into a form text-input and textarea, at the same time as the login session?

Once the information was entered successfully into the database the information in that form session could be vaporized.

Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#2: Jun 25 '09

re: How many sessions can I have at one time


I know there are tons of session tutorials on the web. I've seen them all. See one and you've basically seen them all, since they're pretty much the same tutorial with only the names changed.

They assume that you're only going to use a session for a login feature and that's it. Well, I could use a dozen or more sessions at one time. I have many different forms on a single website.

I use a generic session for a user login. But I could use a different session for this form and another session for that form, and so on and so on. Each form is for a different thing and I don't want the information from one form to get mixed up with another.

What I need is the ability to create a session called "Form 1" and somehow be able take the inputs from "Form 1" and I guess on upon hitting the "Submit" button the user inputs from the form sets a session variable. Maybe like this:
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. session_name(Form 1);
  3. $_SESSION['first_name'] = $first_name; // from a text input
  4. $_SESSION['dog_name'] = $dog_name; // from a text input
  5. $_SESSION['message'] = $message; // from a textarea
I don't know if the above is correct or not.

How would you take the information one enters in a form and turn them into a session variable?

If there is a form on the "form_1.php" page, and the action is set to go to the "form_1_results.php" page, where would you put what in order to set the session variables so that if the user encountered an error and had to hit the back button to return to the "form_1.php" page the user would not have to re-enter in all of their information again?

And then when they hit Submit again, and this time everything checks out OK the information is entered into the database? AND upon a successful database entry, i.e.,
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO dogs VALUE ('$first_name', '$dog_name', '$message');
  2. $result = mysql_query($query);
  3. if (mysql_affected_rows() == 1)
  4. {
  5. echo "Information Entered!";
  6. unset($_SESSION['first_name']);
  7. unset($_SESSION['dog_name']);
  8. unset($_SESSION['message']);
  9. session_destroy('Form 1');
  10.  
  11. // unset and destroy this session so they can fill out another form and information won't get mixed up.
  12.  
  13. } else {
  14.  
  15. echo "There was a problem. Hit your "Back" button and fix it.";
  16. }
Is this possible, and if so what would be the proper way of implementing such a thing?
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#3: Jun 25 '09

re: How many sessions can I have at one time


I tried using these sessions. I have not tried to unset them yet. The information shows back up in the form OK except for the textareas aren't right. It was showing the textarea information with the \r\n\r\n instead of dropping down two lines.

I used the str_replace and then it just replaced the \r\n\r\n with <br><br>, again instead of dropping down two lines.

To get it to show right I had to do the below - look at the field_content. It works but it seems stupid.


This on the form page
Expand|Select|Wrap|Line Numbers
  1. $field_contact = ""; //default as blank
  2. if (isset($_SESSION['contact'])) $field_contact = $_SESSION['contact']; 
  3.  
  4. $field_contact = ""; //default as blank
  5. if (isset($_SESSION['contact'])) $field_contact = $_SESSION['contact']; 
  6.  
  7. $field_content = ""; //default as blank
  8. if (isset($_SESSION['content'])) $field_content = nl2br($_SESSION['content']); 
  9.  
  10. $field_content = str_replace(array("\\r\\n", "\\r", "\\n"), "<br>", $field_content);
  11. $field_content = nl2br($field_content);
  12. $field_content = str_replace(array("<br>"), "\r\n", $field_content);
  13.  
  14.  
  15. <form>
  16. <strong>Enter Your Contact Info:</strong><br>
  17. <textarea rows='3' name='contact' wrap='virtual'>$field_contact</textarea>
  18.  
  19. <br><br>
  20.  
  21. <strong>Enter Your Classified Ad:</strong><br>
  22. <textarea rows='10' name='content' wrap='virtual'>$field_content</textarea>
And this on the receiving page:
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['contact'] = $_POST['contact'];
  2. $_SESSION['content'] = $_POST['content'];
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#4: Jun 25 '09

re: How many sessions can I have at one time


nice.. have look on this articles

Session Handling


Cookies versus PHP Sessions
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#5: Jun 25 '09

re: How many sessions can I have at one time


Thanks, I'll take a look at them.
Reply