-1

I have this at the top of my page:

<?php
    //start session
    session_start();
    print_r($_SESSION);
?>

then the html below.

It doesn't actually print out the $_SESSION array and I am assuming that is because the html is overriding it and that is ok.

But I want to use show the $_SESSION['username'] as an input value in a form.

<label for="obsName">Observer's Name:</label>
<input type="text" name="ObsName" class="form-control" id="obsName" value="<?php echo $_SESSION['username'];?>">

I use this and what is being shown in the box is just that:

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

How can I fix this?

James Teague II
  • 279
  • 5
  • 13
  • arent you running this in a server?make sure that apache is turned on.. – Lal May 10 '15 at 17:41
  • is the session array not been assigned? you didn't include that – Funk Forty Niner May 10 '15 at 17:42
  • Yeah I think so, it is actually being hosted on AWS right now. Everything else is working just fine except for this. But I can't get anything to print to actually see if the variable is even there. I have another .php file that I used to test and that was fine. But here no dice. – James Teague II May 10 '15 at 17:43
  • simply add `` to the start of your HTML page and see if `hello` is printed.. – Lal May 10 '15 at 17:44
  • The session array is being assigned on a previous page. I ran a test by redirecting to a .php file and just dumping out the $_SESSION array to ensure it was correct and it was. – James Teague II May 10 '15 at 17:44
  • The session variable is not the problem here, be it filled or not. The issue is that apparently your environment does not interpret the embedded php tags. Which is _very_ strange. – arkascha May 10 '15 at 17:45
  • _Maybe_ you had a typo at first, fixed that after you realized and now you reload and get this undesired result? Please make a _deep reload_ in your browser to clear your cache (or clear it manually). Also check your http servers error log file. – arkascha May 10 '15 at 17:47
  • There is definitely something strange happening because the the php scripts are working on the page when the form is submitted. But it does not work in the html file itself. So I am at a loss. I have cleared the cache and done reload and still no help. – James Teague II May 10 '15 at 17:50
  • What is the extension of the file? Is it `.php` ? – Lal May 10 '15 at 17:51
  • The extension of the file that has the input is `.html` – James Teague II May 10 '15 at 17:52
  • 2
    That is the problem..change that to `code.php` – Lal May 10 '15 at 17:52
  • 2
    *"The extension of the file that has the input is `.html`"* - there's your problem then and I had a feeling that's what the problem was earlier. Either you change that to a `.php` extension, or instruct Apache to treat `.html` files as PHP. This question has already been asked before, and often. You can delete the question if by doing that, solved the problem. – Funk Forty Niner May 10 '15 at 17:54

1 Answers1

1

For the PHP code that you have written to work, your filename should have the extension .php

The reason why your PHP codes rendered as normal texts is because the browser identified it as just HTML since you had the extension as .html.

For more details see http://en.wikipedia.org/wiki/PHP

Lal
  • 14,726
  • 4
  • 45
  • 70