将会话中的值输出到表单中

时间:2014-04-27 11:52:14

标签: php session

我在尝试输出2个会话值时遇到了一些问题。 当我只输出$ _SESSION [' mypassword'] =" myusername";

时,它工作正常

但是后来我尝试输出两个会话值,它不起作用。这是我到目前为止所做的一个例子。

<?php
session_start();


$_SESSION['mypassword']="myusername";
$_SESSION['studentid']="studentid";
echo "Logged in as".$_SESSION['studentid'];
echo "<h3 class='velkommen'>Logga inn som:<span class='spanclass'>" .$_SESSION['myusername']. "</span></h3>";

?>

studentid输出为studentid。 我之前的页面:

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$studentid=$_POST['studentid'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$studentid = stripslashes($studentid);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$studentid = mysql_real_escape_string($studentid);
$sql="SELECT * FROM $tbl_name WHERE myusername='$myusername' AND password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['studentid'] = $studentid;
header("location:login_success.php" );

任何提示为什么只有myusername谁是正确的? 我需要两个值都是一个表格

<?php
    echo '<input type="hidden" name="myusername" value="'.$_SESSION['myusername'].'">';
?>

任何提示?

2 个答案:

答案 0 :(得分:0)

您正在为mypassword

设置会话
$_SESSION['mypassword']="myusername";
$_SESSION['studentid']="studentid";

而您正在为echo

执行$_SESSION['myusername']
echo "<h3 class='velkommen'>Logga inn som:<span class='spanclass'>" .$_SESSION['myusername']. "</span></h3>";

我看不到任何$_SESSION['myusername']

你需要做的是,这个

$_SESSION['myusername']="myusername";
$_SESSION['studentid']="studentid";

echo "Logged in as: ".$_SESSION['studentid'];

    echo "<h3 class='velkommen'>Logga inn som:<span class='spanclass'>" .$_SESSION['myusername']. "</span></h3>";

答案 1 :(得分:0)

试试这个:

   <?php
    session_start();
    $_SESSION['mypassword']="myusername";
        $_SESSION['studentid']="studentid";
        echo "Logged in as".$_SESSION['studentid'];
        echo "<h3 class='velkommen'>Logga inn som:<span class='spanclass'>". $_SESSION['mypassword'] . "</span></h3>";
    ?>