1
<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'])'"; ?>
3
  • 2
    There is an error. It would be great if you would actually show us the error. But well, if you use single quotes, variables arent recognized. This is only happening when you use double quotes. Commented Jan 15, 2014 at 8:01
  • @Boann why even bother commenting? Commented Jan 15, 2014 at 8:13
  • @Boann it is trash to someone experienced, but we were all there once upon a time. I recall my very first script didn't run...someone pointed out you have to have the .php extension. Who knew :P Commented Jan 15, 2014 at 8:21

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, i did. I want to put it in a hidden field, thus i echo it out with the hidden field. However, I couldn't echo it out
Do some basic trouble shooting. Can you echo it out onto the page at all? Try setting a value on the page and see if that displays, then track through your code until you find where it does display at one point and doesn't display at another. That is where the error is. Basic troubleshooting, if you can't do it, you will fail at coding.
I found out my mistakes, thanks.. btw, u mind helping me? stackoverflow.com/questions/21134121/…
3
<?php echo $_SESSION['MM_Username']; ?>

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

1 Comment

Hi,I want to put it in a hidden field, thus i echo it out with the hidden field. However, I couldn't echo it out
0

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,

i.e. $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.

1 Comment

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.