Jump to content


Photo

Use a session variable that's not from $_POST or $_GET?


  • Please log in to reply
7 replies to this topic

#1 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 07 September 2006 - 12:21 PM

Okay, on page 1 there is an HTML form where they enter Employee Number and Password.

Page 2 - These values are used with $_POST. The database displays their First name Last Name, and Middle Initial, and other info.

Page 3 - Is there a way to take and use First Name, Last Name, and Middle Initial from Page 2 that were NOT posted by a form? I want to pass these to page 3 using a session statement.



#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 September 2006 - 12:34 PM

well, you can definitely do what you're after with session variables. you'd want to have session_start() at the top of each page where you want the session variables to be available. for instance:
<?php
// page 1 submits the info

// page 2 retrieves it and assigns it to a session variable:
$_SESSION['fName'] = $_POST['fName'];

// page 3 retrieves and uses session variables:
echo $_SESSION['fName'];
?>

that's really all there is to it, but you need to make sure to be starting the session for any page on which you need the variables.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 07 September 2006 - 12:44 PM

well, you can definitely do what you're after with session variables. you'd want to have session_start() at the top of each page where you want the session variables to be available. for instance:

<?php
// page 1 submits the info

// page 2 retrieves it and assigns it to a session variable:
$_SESSION['fName'] = $_POST['fName'];

// page 3 retrieves and uses session variables:
echo $_SESSION['fName'];
?>

that's really all there is to it, but you need to make sure to be starting the session for any page on which you need the variables.


Not working.

On Page 2, I have...

<?php
session_start();
$empcode = $_POST['empcode'];
$_SESSION['empcode'] = $empcode;
$lastname = $_POST['NAMEL'];
$_SESSION['NAMEL'] = $lastname;
?>

On page 3 at the top, I have...

<?php
session_start();
$_SESSION['empcode'];
$_SESSION['NAMEL'];
?>

Further down, I have....

$_SESSION['empcode'];
$_SESSION['NAMEL'];

echo "<CENTER>";
echo "<font size=4 color=#000000 face=arial>";
echo "<b>";
echo $_SESSION['empcode'];
echo $_SESSION['NAMEL'];
echo "</b>";
echo "</font>";

It echoes empcode (Employee Number), but not NAMEL (Last Name).





#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 07 September 2006 - 03:06 PM

Doing this doesn't do ANYTHING.

<?php
session_start();
$_SESSION['empcode'];
$_SESSION['NAMEL'];
?>

You only need session_start() at the top of the 3rd page.  The session variables (if set previously) are already there.

You can also take those 2 lines out further down.

The rest should work if you've got all your capitalization correctly. 

Also, throw in "print_r($_SESSION);" to make sure all your variables are making it into the session.

Info: PHP Manual


#5 shoombooltala

shoombooltala
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 07 September 2006 - 03:15 PM

i think what you need to do is the following:

top of page2 you put:
<?php
session_start();

//in the middle of the page where the user gets authenticated and you get their first, middle and last name you would use this:

$_Session['fname'] = $firstName;   //where $firstname is the variable containing the name you got form the database of the user.
$_Session['mname'] = $middleName;   //where $middleNameis the variable containing the name you got form the database of the user.
$_Session['lname'] = $lastName;   //where $lastNameis the variable containing the name you got form the database of the user.
?>

then on top of page 3 also you put:

<?php
session_start();

//then you would retreive the values using this:

echo $_Session['fname'];
echo $_Session['mname'];
echo $_Session['lname'];
?>


#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 07 September 2006 - 03:23 PM

Page 1:

<?php
session_start();

$_SESSION['variable'] = 'Hello world!';

header('Location: page2.php');

?>

Page2.php:
<?php
session_start();

echo $_SESSION['variable'];

?>


#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 September 2006 - 03:26 PM

shoombootala, you do not want to assign your session variables as you have shown. if you do that, the session variable 'fname' will actually hold the value "$firstName" and so on. you never want to try to assign variables from within single quotes.

<?php
// change this idea:
$_SESSION['fname'] = '$firstName';

// to this:
$_SESSION['fname'] = $firstName;
// or this:
$_SESSION['fname'] = "$firstName";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 shoombooltala

shoombooltala
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 07 September 2006 - 03:29 PM

shoombootala, you do not want to assign your session variables as you have shown. if you do that, the session variable 'fname' will actually hold the value "$firstName" and so on. you never want to try to assign variables from within single quotes.

<?php
// change this idea:
$_SESSION['fname'] = '$firstName';

// to this:
$_SESSION['fname'] = $firstName;
// or this:
$_SESSION['fname'] = "$firstName";
?>


you are totally correct. I edited and fixed it now. Thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users