Missing PHP Session in .html file

2019-08-29 11:10发布

I'm trying to migrate a Webshop to a new Webserver. It is working perfectly fine on the old Webserver, however, when I try to login at the Webshop (index.html) it returns to the homepage, not logged in as the Session Variable is empty. Then I noticed, when I opened another Site called request.php) the Session Variable was set and I was logged in. So I tried several things, I renamed the index.html to index.php and the session was there.

My question is now: Can I get the Session in .html files, too (as on the previous Webserver) or do I have to rename all my .html files?

Note: The index.html file contains php code as well and is parsed as php, just the session variable is empty.

Thanks in advance for every answer!

4条回答
一夜七次
2楼-- · 2019-08-29 11:59

The files need to be renamed to have a .php extension on them, even if they include html. Simply include your php code before the html starts, like so:

<?php
 //code goes here
?>

<html>
  <body>
    //etc...

This way, you can load up your session variable and any other PHP variables you need, then they'll be accessible in the html as well.

If you have several pages that will need the same thing (session variable), you could put the session code into a php file, something like 'session.php'. Then, at the top of every page you're converting from html to php, put this code at the top:

<?php
  require_once("session.php");
?>

Now all of your pages will have access to the same information, and it helps to cut down on the code too.

查看更多
Explosion°爆炸
3楼-- · 2019-08-29 11:59

AddType application/x-httpd-php .html

In your .htaccess file will do the job, still I believe it's rather unsafe to do it :)

查看更多
欢心
4楼-- · 2019-08-29 12:00

if you are using a Apache Server, try add this to the config file.

AddType application/x-httpd-php .html
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-08-29 12:01

You need to set a handler in your web server to treat html files as php files.

查看更多
登录 后发表回答