开发者

Passing a PHP variable

I have the followi开发者_如何学运维ng 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??


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']);


Put it into the session.


Session is the way to do that...


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.


use session variable and put the fname in session.


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'];


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜