简体   繁体   English

在.html文件中使用PHP

[英]Using PHP in a .html document

I'm working on a job at work where I need to create a web forum. 我正在工作,需要创建一个网络论坛。 In my stupidity I decided to use javascript, not knowing beforehand that javascript is a client-side language. 出于愚蠢,我决定使用javascript,但事先不知道javascript是一种客户端语言。 I need a way to save the data from the javascript onto the server and then be able to read the data. 我需要一种将javascript中的数据保存到服务器上,然后能够读取数据的方法。 I tried looking at things like node.js, however I would have reconfigure the entire web server (which isn't mine) in order to do this. 我尝试查看诸如node.js之类的内容,但是我会重新配置整个Web服务器(这不是我的)才能做到这一点。 The other solution is to use php. 另一种解决方案是使用php。 Here's the problem: There's a bunch of includes used in the html file that set up the layout of the webpage (ie css files, html files, and even a php include). 这是问题所在:在html文件中使用了一堆包含项来设置网页的布局(即css文件,html文件,甚至包括php)。 I can't change the name of the index file to index.php because it breaks all of the includes inside of the file. 我无法将索引文件的名称更改为index.php,因为它破坏了文件内部的所有包含。 So I need a way to save, say a text file, using javascript and html. 因此,我需要一种使用javascript和html保存文本文件的方法。 If there's a way to, I would like to do something very simple, like include a php file in the html and then call a php function in my javascript code to get the contents of the file into my index.html page. 如果有办法,我想做一些非常简单的事情,例如在html中包含一个php文件,然后在我的javascript代码中调用一个php函数,以将文件的内容放入我的index.html页面。 I thought there was a way to call a simple command like this: 我认为有一种方法可以像这样调用一个简单的命令:

<script>
    var thedata = <?php getData() ?>
</script>

where the php function getData() would return a json encoded string with all of the data in it (handled from a separate php file). php函数getData()将返回一个json编码的字符串,其中包含所有数据(从单独的php文件处理)。 Is there any way to do this? 有什么办法吗? Any other suggestions for how to handle data storage on a server without changing my index.html file to index.php? 关于如何在不将我的index.html文件更改为index.php的情况下处理服务器上的数据存储的其他建议?

Note: I tried accessing the apache httpd.conf file and adding a handler to pre-process .html files as php files, but that doesn't seem to work (nothing as simple as echo 'test' works on the html file). 注意:我尝试访问apache httpd.conf文件并添加一个处理程序以将.html文件作为php文件进行预处理,但这似乎不起作用(没有比echo'test'更为简单的html文件了)。

Add this to your .htaccess : 将此添加到您的.htaccess

AddHandler application/x-httpd-php5 .html .htm

source 资源

It causes Apache to treat HTML files as files that contain PHP. 它使Apache将HTML文件视为包含PHP的文件。 Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though. 但是要小心,不要以某种方式意外地在常规HTML文件中使用PHP语法。

Remember that you'll still need to use PHP tags to enter PHP mode. 请记住,您仍然需要使用PHP标记进入PHP模式。 This works as expected: 这按预期工作:

<p>html content ...  <?php echo 'hello, world'; ?></p>

But this will output the the echo command: 但这将输出echo命令:

<p>echo 'hello, world'</p>

If can make your webserver process your pages thru the mod_php. 如果可以使您的Web服务器通过mod_php处理您的页面。 if you are using apache just add this to your .htaccess 如果您使用的是apache,请将其添加到您的.htaccess中

AddHandler x-httpd-php .html .htm 
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm 
AddType application/x-httpd-php .html

I hope you understand the repercussion of doing this. 希望您了解这样做的影响。 all your pages will be processed like that and the memory/cpu use of your pages will be way greater. 您所有的页面都会像这样处理,并且页面的内存/ CPU使用率会更高。

if this is to happen inside one single file, make sure to add it inside a statement. 如果要在单个文件中执行此操作,请确保将其添加到语句中。

and within your example you should add: 在示例中,您应该添加:

<script>
    var thedata = <?php echo getData(); ?>
</script>

文件扩展名必须是.php ,以便服务器知道解析该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM