1

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??

mopoke
  • 10,555
  • 1
  • 31
  • 31
freddy6
  • 137
  • 2
  • 5
  • 12
  • The questions gets confusing when you mention the form. Do you want to use the form to pass the value via a hidden input? – Jason Rowe Jan 27 '10 at 04:39
  • I don't get why people are downvoting the session answers. It is really the only way to do it. That or cookies. – Tyler Carter Jan 27 '10 at 04:59
  • 1
    Got your dupe right here: http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page – random Jan 27 '10 at 05:08
  • Those answers are more comments, if even half-done attempts at an answer. They're the equivalent of RTFM. @cha – random Jan 27 '10 at 05:52

7 Answers7

2

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']);
Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
Koobz
  • 6,928
  • 6
  • 41
  • 53
  • You don't have to use print_r() if you know the key, you can just echo $_SESSION['your_key_here']; – user254875486 Jan 27 '10 at 07:50
  • 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 Jan 27 '10 at 09:07
  • I will need to look into how sessions works, thanks for you help and pointing me in the right direction. – freddy6 Jan 28 '10 at 02:13
1

Put it into the session.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Session is the way to do that...

Avinash
  • 6,064
  • 15
  • 62
  • 95
1

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
  • 4,583
  • 1
  • 26
  • 33
1

use session variable and put the fname in session.

praveen
  • 55
  • 1
  • 7
0

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
  • 14,178
  • 33
  • 114
  • 175
0

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
  • 7,737
  • 2
  • 35
  • 33