簡體   English   中英

如何在PHP中的回聲中回顯會話變量

[英]how to echo out a session variable within an echo in php

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

我想回應一下。 但是,無論我如何嘗試,都存在錯誤。 有人可以幫我嗎?

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

你有session_start(); 在腳本頂部的某個地方?

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

應該管用。 如果不是,則MM_Username為空。

嘗試執行此操作。請記住一件事,您必須在編碼的頂部開始會話。 然后,使用變量$ a並存儲$ _SESSION ['MM_Username']的值; ?>放入$ a,

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

現在回顯$ a,

如果沒有,那么您要么必須輸入一個空條目,要么試圖在隱藏字段中顯示結果,那么您將一無所獲。

看一下我的頁面,它以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);
    ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM