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

4
  • The questions gets confusing when you mention the form. Do you want to use the form to pass the value via a hidden input? Commented Jan 27, 2010 at 4:39
  • I don't get why people are downvoting the session answers. It is really the only way to do it. That or cookies. Commented Jan 27, 2010 at 4:59
  • 1
    Got your dupe right here: stackoverflow.com/questions/871858/… Commented Jan 27, 2010 at 5:08
  • Those answers are more comments, if even half-done attempts at an answer. They're the equivalent of RTFM. @cha Commented Jan 27, 2010 at 5:52

7 Answers 7

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']);
Sign up to request clarification or add additional context in comments.

3 Comments

You don't have to use print_r() if you know the key, you can just echo $_SESSION['your_key_here'];
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.
I will need to look into how sessions works, thanks for you help and pointing me in the right direction.
1

Put it into the session.

1 Comment

yeah, Im going to try and do something with session
1

Session is the way to do that...

1 Comment

yeah, Im going to try and do something with session
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.

1 Comment

yeah, Im going to try and do something with sessions, thanks don
1

use session variable and put the fname in session.

1 Comment

yeah, Im going to try and do something with sessions, thanks praveen
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'];

3 Comments

The name is already known; there's no need to enter it again.
Changed the type to a hidden field and used the name as the value.
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.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.