views:

123

answers:

7

Hi there, I have the following php code below...

if ($username == 'fredk')
{ $fname = 'Fred'; }
else if ($username == 'arbonk')
{ $fname = 'Arbon'; }
else if ($username == 'arsalana')
{ $fname = 'Arsalan'; }
else if ($username == 'minhn')
{ $fname = 'Minh'; }
else if ($username == 'nathanielg')
{ $fname = 'Nathaniel'; }

$msg = "Hi $fname, your login was successfull. <p></p>";

All i want to do is pass the $fname variable onto the next php page. On the same page I also have a form and when the submit button is clicked it goes onto the next page.

Anyone have any ideas??

+1  A: 

Put it into the session.

Ignacio Vazquez-Abrams
yeah, Im going to try and do something with session
freddy6
+1  A: 

Look into sessions. They're used for the exact reason in your example (persistent login credential data + more).

session_start(); // Do this at the very start of your script (on both pages).

$_SESSION['your_key_here'] = 'blah'; // value may be an object as well.

on the next page you can access it:

print_r($_SESSION['your_key_here']);

Koobz
You don't have to use print_r() if you know the key, you can just echo $_SESSION['your_key_here'];
Lex
The point was to demonstrate that you can store various objects there as well, not simply primitives. Of course, there's nuances as some objects can't be serialized/pickled trivially.
Koobz
I will need to look into how sessions works, thanks for you help and pointing me in the right direction.
freddy6
A: 

Looks like you need to use $_POST

for example if this is your form code:

<form action="page.php" method="post">
    <input name="fname" type="hidden" value="$fname" />
</form>

On page.php you would retrieve the fname variable like so:

$fname = $_POST['fname'];
kylex
The name is already known; there's no need to enter it again.
Ignacio Vazquez-Abrams
Changed the type to a hidden field and used the name as the value.
kylex
since i already have another form on the page, i think that it makes more sense (for me at least) to try and use sessions as Koobz has advised.
freddy6
+1  A: 

Session is the way to do that...

Avinash
yeah, Im going to try and do something with session
freddy6
+1  A: 

Or you can put the variable into the form as a hidden variable

<input type='hidden' name='who' value='$fname>

but, this is just for completeness sake,

I would probably use a session myself.

Don
yeah, Im going to try and do something with sessions, thanks don
freddy6
A: 

Where does $username come from? Could you perhaps write a function that takes $username as a parameter and returns $fname, and call it on both pages?

echo
+1  A: 

use session variable and put the fname in session.

praveen
yeah, Im going to try and do something with sessions, thanks praveen
freddy6