-5

How do I print the $_SESSION['...'] in my external html? It would be for Welcoming the user on my webpage.

My PHP and HTML are two separate files. I wanted to show the username of the account that is currently logged on in the webpage.

session_start();
$_SESSION['user_name']=$username;

That's what is written in my php file. But how do I print it on HTML?

Thanks

juju17
  • 271
  • 4
  • 15
  • *"But how do I print it on HTML?"* - as in what, `.html` file? – Funk Forty Niner Dec 14 '15 at 14:26
  • You can't use php variables in html, you should convert your `file.html` to `file.php` – Armen Dec 14 '15 at 14:26
  • 3
    Don't add `$` in front of `session_start()` – Daan Dec 14 '15 at 14:27
  • Error reporting as @Daan stated, would have thrown you an undefined `$` variable notice, **if** that's your real code. – Funk Forty Niner Dec 14 '15 at 14:28
  • I edited it. And yes this is my code. But that's isn't my problem. Please refer to the question. – juju17 Dec 14 '15 at 14:29
  • [and still, this comment never gets answered...](http://stackoverflow.com/questions/34269368/sessionuser-name-on-html#comment56279979_34269368) well take it up with the answer below, because I for one, have no idea how to help you. Good luck in your project; *sincerely*. – Funk Forty Niner Dec 14 '15 at 14:40
  • 1
    @user4932301 - fine I'll take fred's position and assume `$session_start()`is valid code since you said that isn't your problem and you'll never get any help. cheers. good luck. – I wrestled a bear once. Dec 14 '15 at 14:41
  • 1
    @user4932301 If the question is about "how to parse PHP/sessions in an `.html` file", you can do that, but you need to instruct Apache to treat them as PHP, as I posted a link under that guy's answer below, being http://stackoverflow.com/q/4687208/ - You just need to set the session in all files, assign it to a variable that you want and put it in the other file. Use error reporting to check for errors, just in case. – Funk Forty Niner Dec 14 '15 at 15:13

1 Answers1

1

You can include the PHP within your HTML but would need to use the file extension '.php' instead of '.html' (unless you inform your web server to parse html files as PHP), because in this case it is not mixing concerns.

<?= $_SESSION['user_name']; ?>

You could use the full PHP tag also and echo the value. But remember to also escape the value using htmlentities() like so:

<?= htmlentities($_SESSION['user_name']); ?>

Sorry for not being more clear...The above examples would assume you had session_start() somewhere earlier in your code, otherwise it would not be available.

Adam Culp
  • 500
  • 2
  • 11
  • 2
    You can set an .htaccess rule that will allow .html pages to process PHP properly. – Jay Blanchard Dec 14 '15 at 14:33
  • 2
    not entirely true about the `.html` files. Plus, you've not started the session here. Edit: *GMTA Sam* @JayBlanchard ;-) – Funk Forty Niner Dec 14 '15 at 14:34
  • Good points. But that was not in the question. And the person asking the question already had the session_start(), so I did not include it again. ;-) – Adam Culp Dec 14 '15 at 14:36
  • tell me Adam, what does `$_SESSION['user_name'];` do exactly here? and how does that session get populated? – Funk Forty Niner Dec 14 '15 at 14:36
  • *"And the person asking the question already had the session_start(), so I did not include it again."* - the idea here is that they're using a seperate file. Not stating that the session also needs to be started in the other file, or not including `session_start();` will fail and for future readers to the question/answer who have no idea what sessions are about or how to use them. You need to think about those people also here Adam. **edit:** ok, you made the edit for it. – Funk Forty Niner Dec 14 '15 at 14:39
  • 1
    FYI: http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Funk Forty Niner Dec 14 '15 at 14:54