0
<?php session_start(); ?>
<html>
<head>
    <title>

    </title>
</head>
<body>
    <?php
        if($_SESSION['status']=='loggedin'){
            echo $_SESSION['name'];
        }
    ?>
</body>
</html>    

I started a session for keeping track of logged in users. The same code I put into a profile.html file and tried to redirect to this page if a log in attempt is successful, but I couldn't fetch any information from $_SESSION variables into that file, but when I changed the file extension to profile.php it worked nicely.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
buet_baba
  • 59
  • 1
  • 1
  • 7
  • While what `Marcel Korpel` stated below is true, you can run `.html` files as `php`, simply by adding this to your `.htaccess` file `AddType application/x-httpd-php .html` – Funk Forty Niner Jun 09 '13 at 18:25
  • @Fred But then *all* html files will be treated as PHP scripts, with a minor drawback on performance. – Marcel Korpel Jun 09 '13 at 18:27
  • @MarcelKorpel Yes that's true. But if the OP really wants to run scripts under the `.html` file convention, then he/she will have to make that choice. It's a last resort which personally, one that I don't use. – Funk Forty Niner Jun 09 '13 at 18:28
  • @Fred will u please explain it more generally or provide me a link where i can understand this thoroughly. i mean i am not understanding where is this .htaccess file or i have to create it and where to put "AddType application/x-httpd-php .html" this line? – buet_baba Jun 09 '13 at 18:31
  • @buet_baba You can Google it with the following link that will show you many pages for you to read up on. https://www.google.com/search?q=run%20html%20as%20php&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Funk Forty Niner Jun 09 '13 at 18:33
  • possible duplicate of [Using .htaccess to make all .html pages to run as .php files?](http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files) – Marcel Korpel Jun 09 '13 at 18:34
  • @buet_baba In short, you would be telling the server to `add a condition` to any (all) .html files to run as if they were .php script files. – Funk Forty Niner Jun 09 '13 at 18:35
  • Read up on what PHP is at https://en.wikipedia.org/wiki/PHP - http://php.net/manual/en/intro-whatis.php – Funk Forty Niner Jun 09 '13 at 18:36
  • @buet_baba You're welcome. Enjoy the adventure and happy coding! Cheers – Funk Forty Niner Jun 09 '13 at 18:37

2 Answers2

1

PHP scripts are by default only interpreted in .php files (unless you tell the PHP interpreter to do otherwise, e.g., using an .htaccess file). So, you can't use $_SESSION variables in an .html file.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
0

You can write php code only in php file and html in html file or php file.

Macbric
  • 472
  • 4
  • 10