A quick php session question

  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 25th, 2008, 3:56 pm

Hello all :)

I read the tutorial on php sessions, but I have a question. I understand about adding variables, but I don't know how I'm supposed to use something they typed in as a variable within that session.

For instance, let's say a client enters in information on 3 different pages. The 4th page will be the preview page and then they'll submit it and it'll go into the database. I decided probably the best way to do this is through starting a session. I know how to start a session and add variables, but how do I add variables someone posted?

Thanks in advance,
~Amy
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 25th, 2008, 3:56 pm

  • dr4gon
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Feb 22, 2006
  • Posts: 6
  • Loc: Portugal
  • Status: Offline

Post June 25th, 2008, 4:52 pm

Hey Amy.. that's pretty simples...

Page 1

<?php
session_start();
$_SESSION["page1"] = "info on the page one";
?>

Page 2

<?php
session_start();
$_SESSION["page2"] = "info on the page two";
?>

Page 3

<?php
session_start();
$_SESSION["page3"] = "info on the page three";
?>


Page 4


<?php
session_start();
echo $_SESSION["page1"]."<br />";
echo $_SESSION["page2"]."<br />";
echo $_SESSION["page3"]."<br />";

?>


This would output:

info on the page one
info on the page two
info on the page three


Hope this helps. Feel free to ask if you need further help
  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7130
  • Loc: Ozzuland
  • Status: Online

Post June 25th, 2008, 6:00 pm

Here's how... the above example isn't exactly what you wanted...
page#.php
PHP Code: [ Download ] [ Select ]
<?php
sessionm_start();
if(!isset($_POST['submit']))
{
 // your form here
} else {
 $formInput1 = $_POST['formInput1'];
 $formInput2 = $_POST['formInput2'];
 $formInput3 = $_POST['formInput3'];
 $formInput4 = $_POST['formInput4'];
 // form validation
 
 $_SESSION['name1'] = $formInput1:
 $_SESSION['name2'] = $formInput2:
 $_SESSION['name3'] = $formInput3:
 $_SESSION['name4'] = $formInput4:
}
?>
  1. <?php
  2. sessionm_start();
  3. if(!isset($_POST['submit']))
  4. {
  5.  // your form here
  6. } else {
  7.  $formInput1 = $_POST['formInput1'];
  8.  $formInput2 = $_POST['formInput2'];
  9.  $formInput3 = $_POST['formInput3'];
  10.  $formInput4 = $_POST['formInput4'];
  11.  // form validation
  12.  
  13.  $_SESSION['name1'] = $formInput1:
  14.  $_SESSION['name2'] = $formInput2:
  15.  $_SESSION['name3'] = $formInput3:
  16.  $_SESSION['name4'] = $formInput4:
  17. }
  18. ?>

That's all there is to it... just put a variable into the session variable. (Just be sure not to use the same name for session variables on other pages... they will over-ride the first set sessions)... it also assumes it's on the same URL (e.g: http://www.domain.com is NOT the same as http://sub.domain.com... I'm not sure about http://domain.com though).
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 6:41 am

Thank you Bogey! That answered all questions I had. Looking forward to your blog :)

~Amy
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 6:50 am

Oh, wait, one question. If I am starting the session, why do I need to add if(!isset($_POST['submit']))? I don't understand why I need to know if a submit button is checked. Is that only for if the client logged in?

What I want to do is once a person starts filling out the first page to become a client, start a session to carry all his information to the last page. That way if he decides on the 2nd page he wants to not be a member yet for whatever reason, his info won't go into the database to clog it up. I'm guessing that is the best way to do it?

So in that case, I wouldn't need if(!isset($_POST['submit']))
would I?

Thanks for your help.
~Amy
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 7:26 am

I'd like to ask you another question. If a client closes out the page he's filling out, will a session automatically end?

~Amy
  • mishka
  • Novice
  • Novice
  • No Avatar
  • Joined: May 28, 2005
  • Posts: 20
  • Loc: Toronto
  • Status: Offline

Post June 26th, 2008, 8:22 am

Hi,
KISS is very important principal and that's why I like dr4gon's post here rather then Bogey's one. It answers to the core - how to reuse your session vars, would it be echo'ing (like he demonstrated) or assigning its value to local var.

IMHO, the submit here is just validation step to ensure that data posted with the current page have been submitted from stage.
And again, U see it presumes that U use "Submit" name/value html object in previous page .... it's design details....

For your last question ... hmm, not necessarily... If that's the only window he's negotiating with your server then definitely yes, otherwise No/May Be, depends if his other pages are using session.
If we talking about secure site, I would strongly recommend to carry out session_id via for each logged user ... it can be implemented via PHPSESSID or cookies technique.
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 8:55 am

Hmmmm...Sorry dr4gon for not using your example...I have never seen it done that way and I understood more of Bogey's post.

Mishka, I understand what you mean. I'm not sure what to do. I have a lot of information that has to be carried to that preview page.

Let's say I need someone's name, address, and phone # to carry from the first page to the 4th page and then be posted in a form. I'm not sure I understand how to do that with dr4gon's post. I believe I should assign variables to certain things (name, address, phone) to be carried over, instead of the entire page.

If I'm wrong, please say so :) I'm new, so I could be way off and skipping and singing with no idea I'm doing it wrong. Hehe!

I don't think this part should be secure yet. Pretty much everything they are entering in these fields will be posted on the site for everyone to see. It won't be until they log into the members area to change their post that it will be secure. I will keep the PHPSESSID in mind for when I get to building that. Thank you for your advice :)

~Amy
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 9:31 am

Okay, would someone tell me why the following wouldn't work?

Code: [ Download ] [ Select ]
<?php
session_start();

$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['phone'] = $_POST ['phone'];

?>
  1. <?php
  2. session_start();
  3. $_SESSION['name'] = $_POST['name'];
  4. $_SESSION['address'] = $_POST['address'];
  5. $_SESSION['phone'] = $_POST ['phone'];
  6. ?>


And then to retrieve it down a few pages...

Code: [ Download ] [ Select ]
<?php
session_start();

echo "Name: " $_SESSION['name'];
echo "Address: " $_SESSION['address'];
echo "Phone: " $_SESSION['phone'];

?>
  1. <?php
  2. session_start();
  3. echo "Name: " $_SESSION['name'];
  4. echo "Address: " $_SESSION['address'];
  5. echo "Phone: " $_SESSION['phone'];
  6. ?>


Am I getting warmer? Marco?

~Amy
  • spork
  • HB
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5487
  • Loc: Rochester, NY
  • Status: Online

Post June 26th, 2008, 9:33 am

You aren't concatenating your strings correctly.

PHP Code: [ Download ] [ Select ]
<?php
session_start();
 
echo "Name: " . $_SESSION['name'];
echo "Address: " . $_SESSION['address'];
echo "Phone: " . $_SESSION['phone'];
 
?>
  1. <?php
  2. session_start();
  3.  
  4. echo "Name: " . $_SESSION['name'];
  5. echo "Address: " . $_SESSION['address'];
  6. echo "Phone: " . $_SESSION['phone'];
  7.  
  8. ?>


Note the dots between the strings/variables.
How to Maintain Simple, Static Pages in a CakePHP Application
EEEEEEEEE! It's here!!
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 9:40 am

Oh, thank you! That would have been bad.

Does everything else look right, like it would work?

~Amy


A morning without coffee is like sleep. ~Author Unknown
  • mishka
  • Novice
  • Novice
  • No Avatar
  • Joined: May 28, 2005
  • Posts: 20
  • Loc: Toronto
  • Status: Offline

Post June 26th, 2008, 9:57 am

Hi Amy,
Came back because something uprise across my mind that may be helpful to you ...
First, if you clear with Bogey's post then fine .... I just pointed that dr4gon's one clear enough from conceptual POV.

Now, I wouldn't use session to carry out data trough the pages in your case ... it can be done thought but there is no real need. U can use instead hidden type object .... something like:

Page 1
<?php
echo "<input type='text' value='' name='name'>";
echo "<input type='text' value='' name='address'>";

# Submit to page 2
?>

Page 2
<?php
# Assign submitted values from previous page 1
echo "<input type='HIDDEN' value='".$_REQUEST['name']."' name='name'>";
echo "<input type='HIDDEN' value='".$_REQUEST['address']."' name='address'>";

# Other page-2 Form fields

?>

session use would be more suitable for its abstractive meaning as it is ... like to identify user id/login trough/during whole his SESSION (process) when he connected to your server. Another option from convenience POV to session's use is for PASSING ARRAY vars between the pages (and that's not your case).

Also, keep in mind that closing window event is no equivalent to closing browser event ... so in closing browser event the session file which includes user session vars (take a look at your temp server defined folder) will be definitely vanished while in multiple -window negotiating env ... it will NOT if other pages use session as well. If other pages are not using the same named session vars than no problem otherwise mutual based problems may float up. I can't provide so common solution to it but basically it should derived by initiating
unset($_SESSION['userDefinedVar'];
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 10:14 am

Oh, okay I see...

So I can have everything entered on page 1 be sent to page2, then page 3 and then have it viewed in a form on page 4 using HIDDEN? As well as everything on page 2 be sent and 3? I've never looked into HIDDEN before..or even $_REQUEST, learn something new everyday :)

~Amy
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 10:20 am

That does seem like a whole lot of more work than just putting it into a SESSION, doesn't it?

~Amy
  • designingamy
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jun 25, 2008
  • Posts: 40
  • Status: Offline

Post June 26th, 2008, 10:29 am

Okay question on what you wrote...

If I put what you wrote into the 2nd page, will it post the name from the first page? I want it to just keep the info until the 4th page.

Or since it says HIDDEN, it won't show up on the 2nd page?

~Amy
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 26th, 2008, 10:29 am

Post Information

  • Total Posts in this topic: 29 posts
  • Users browsing this forum: No registered users and 172 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.