简体   繁体   中英

how to echo out a session variable within an echo in php

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

I would like to echo it out. But no matter how I try there is an error. Can some one help me out?

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

Do you have session_start(); somewhere at the top of the script?

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

should work. If it does not, then MM_Username is empty.

Try to do this... Remember one thing, you must start session at the top of your coding. Then, take a variable $a and store the value of $_SESSION['MM_Username']; ?> into $a,

ie $a=$_SESSION['MM_Username'];

now echo $a,

if it does not then either you must have an empty entry or as you are trying to display the result in hidden field you will not be able to get anything.

Take a good look at my page it begins with the session_start(); Then in my HTML it contains variables i have echoed. Hope it helps.

<?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);
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM