如何在php中回显一个echo中的会话变量

时间:2014-01-15 08:00:00

标签: php

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

我想回应一下。 但无论我如何尝试,都会出现错误。 有人可以帮助我吗?

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

4 个答案:

答案 0 :(得分:5)

你在脚本顶部的某个地方有session_start();吗?

答案 1 :(得分:2)

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

应该有效。如果没有,则MM_Username为空。

答案 2 :(得分:0)

尝试这样做...... 记住一件事,你必须在编码的顶部开始会话。 然后, 取一个变量$ a并存储$ _SESSION ['MM_Username']的值; ?&GT;进入$ a,

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

现在回显$ a,

如果没有,那么你必须有一个空条目,或者当你试图在隐藏字段中显示结果时,你将无法得到任何东西。

答案 3 :(得分: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);
    ?>