Jump to content

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


Recommended Posts

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.

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:
[code]
<?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'];
?>
[/code]

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.
[quote author=obsidian link=topic=107220.msg429821#msg429821 date=1157632445]
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:
[code]
<?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'];
?>
[/code]

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.
[/quote]

Not working.

On Page 2, I have...

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

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

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

Further down, I have....

[code]$_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>";
[/code]

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



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.
i think what you need to do is the following:

top of page2 you put:
[code]
<?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.
?>
[/code]

then on top of page 3 also you put:

[code]
<?php
session_start();

//then you would retreive the values using this:

echo $_Session['fname'];
echo $_Session['mname'];
echo $_Session['lname'];
?>
[/code]
shoombootala, you [b]do not[/b] 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.

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

// to this:
$_SESSION['fname'] = $firstName;
// or this:
$_SESSION['fname'] = "$firstName";
?>
[/code]
[quote author=obsidian link=topic=107220.msg429958#msg429958 date=1157642800]
shoombootala, you [b]do not[/b] 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.

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

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

you are totally correct. I edited and fixed it now. Thanks :)
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.