2014-01-15 46 views
1
<input type="hidden" name="username" value="<?php echo ($_SESSION['MM_Username']) ?>" > 

我想回顯一下。 但無論我如何嘗試都有錯誤。 有人可以幫我嗎?如何在php中回顯echo中的會話變量

<?php echo "<input type='hidden' name='username' value='($_SESSION['MM_Username'])'"; ?> 
+2

這是一個錯誤。如果你真的會向我們顯示錯誤,那將是非常棒的。但是,如果您使用單引號,則不會識別變量。這隻在使用雙引號時纔會發生。 –

+0

@Boann爲什麼還要麻煩評論? – Steve

+0

@Boann這對有經驗的人來說是垃圾,但我們曾經在那裏。我記得我的第一個腳本沒有運行...有人指出你必須有.php擴展名。誰知道:P – Steve

回答

2
<?php echo $_SESSION['MM_Username']; ?> 

應該工作。如果不是,則MM_Username爲空。

+0

嗨,我想把它放在一個隱藏的領域, 因此我回聲與隱藏的領域。 但是,我無法迴應它 – user3156220

4

您是否有session_start();位於腳本頂部的某個位置?

+0

是的,我做到了。 我想把它放在一個隱藏的領域, 因此我回聲與隱藏的領域。 但是,我無法迴應出 – user3156220

+0

做一些基本的故障排除。你能把它反映到頁面上嗎?嘗試在頁面上設置一個值,看看是否顯示,然後跟蹤代碼,直到找到它在某個點顯示的位置,而不在另一個位置顯示。那是錯誤的地方。 基本故障排除,如果你不能這樣做,你將在編碼時失敗。 – Steve

+0

我發現了我的錯誤,謝謝.. 順便說一句,你介意幫助我嗎? http://stackoverflow.com/questions/21134121/unset-session-does-not-work-well-php – user3156220

0

試着做這個... 記住一件事,你必須開始你的編碼頂部的會話。 然後, 接受變量$ a並存儲$ _SESSION ['MM_Username']的值; ?> into $ a,

即 $ a = $ _ SESSION ['MM_Username'];

現在回聲$一個,

,如果沒有的話要麼你必須有一個空項或作爲您要顯示的結果中隱藏字段,你將無法得到任何東西。

+0

http://stackoverflow.com/questions/21134121/unset-session-does-not-work-well -php 可以提供幫助嗎? – user3156220

0

仔細看看我的頁面,它以session_start()開頭; 然後在我的HTML中它包含我已經迴應的變量。 希望它有幫助。

<?php 
    if (!isset($_SESSION)) { 
     session_start(); 
    } 
    $MM_authorizedUsers = ""; 
    $MM_donotCheckaccess = "true"; 

    // *** Restrict Access To Page: Grant or deny access to this page 
    function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
     // For security, start by assuming the visitor is NOT authorized. 
     $isValid = False; 

     // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
     // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
     if (!empty($UserName)) { 
     // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
     // Parse the strings into arrays. 
     $arrUsers = Explode(",", $strUsers); 
     $arrGroups = Explode(",", $strGroups); 
     if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
     } 
     // Or, you may restrict access to only certain users based on their username. 
     if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
     } 
     if (($strUsers == "") && true) { 
      $isValid = true; 
     } 
     } 
     return $isValid; 
    } 

    $MM_restrictGoTo = "l.php"; 
    if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { 
     $MM_qsChar = "?"; 
     $MM_referrer = $_SERVER['PHP_SELF']; 
     if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; 
     if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
     $MM_referrer .= "?" . $_SERVER['QUERY_STRING']; 
     $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); 
     header("Location: ". $MM_restrictGoTo); 
     exit; 
    } 
    ?> 
    <?php 
    if (!function_exists("GetSQLValueString")) { 
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    { 
     if (PHP_VERSION < 6) { 
     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
     } 

     $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

     switch ($theType) { 
     case "text": 
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
      break;  
     case "long": 
     case "int": 
      $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
      break; 
     case "double": 
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
      break; 
     case "date": 
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
      break; 
     case "defined": 
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
      break; 
     } 
     return $theValue; 
    } 
    } 

    $editFormAction = $_SERVER['PHP_SELF']; 
    if (isset($_SERVER['QUERY_STRING'])) { 
     $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
    } 

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "new_info")) { 
     $insertSQL = sprintf("INSERT INTO new (level, test_question_no) VALUES (%s, %s)", 
          GetSQLValueString($_POST['level'], "text"), 
          GetSQLValueString($_POST['test_question'], "text")); 

     mysql_select_db($database_noundb, $noundb); 
     $Result1 = mysql_query($insertSQL, $noundb) or die(mysql_error()); 

     $insertGoTo = "variable.php"; 
     if (isset($_SERVER['QUERY_STRING'])) { 
     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
     $insertGoTo .= $_SERVER['QUERY_STRING']; 
     } 
     header(sprintf("Location: %s", $insertGoTo)); 
    } 

    $colname_passVar = "-1"; 
    if (isset($_SESSION['MM_Username'])) { 
     $colname_passVar = $_SESSION['MM_Username']; 
    } 
    mysql_select_db($database_noundb, $noundb); 
    $query_passVar = sprintf("SELECT * FROM counts, users WHERE users.username=%s", GetSQLValueString($colname_passVar, "text")); 
    $passVar = mysql_query($query_passVar, $noundb) or die(mysql_error()); 
    $row_passVar = mysql_fetch_assoc($passVar); 
    $totalRows_passVar = mysql_num_rows($passVar); 
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Select Variable Value</title> 
    </head> 

    <body> 
    <p><?php echo $row_passVar['test_question_no']; ?></p> 
    <p></p> 
    <p><a href="<?php echo $logoutAction ?>">logout</a></p> 
    <p>&nbsp;</p> 
    <p><?php echo $row_passVar['accesslevel']; ?></p> 
    <form id="new_info" name="new_info" method="POST" action="<?php echo $editFormAction; ?>"> 
     <p> 
     <input name="test_question" type="hidden" id="test_question" value="<?php echo $row_passVar['test_question_no']; ?>" /> 
     </p> 
     <p> 
     <input name="level" type="hidden" id="level" value="<?php echo $row_passVar['accesslevel']; ?>" /> 
     </p> 
     <p> 
     <input type="submit" name="submit" id="submit" value="Submit New" /> 
     </p> 
     <input type="hidden" name="MM_insert" value="new_info" /> 
    </form> 
    <p>go to <a href="index">home</a></p> 
    </body> 
    </html> 
    <?php 
    mysql_free_result($passVar); 
    ?>