Missing PHP Session in .html file

孤街浪徒 提交于 2020-01-06 08:13:25

问题


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!


回答1:


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

AddType application/x-httpd-php .html



回答2:


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




回答3:


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.




回答4:


AddType application/x-httpd-php .html

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



来源:https://stackoverflow.com/questions/17186458/missing-php-session-in-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!