简体   繁体   English

无法使php正常工作

[英]Having trouble getting php to work

I am just starting in web development, and I am having trouble getting php to work. 我刚刚开始进行Web开发,但是我无法使php正常工作。 I have access to a domain on my universities' server that should have php installed, although I'm not entirely sure about that. 我可以访问大学服务器上应安装php的域,尽管我对此不太确定。

This is the code I am trying to run. 这是我要运行的代码。

<html>
<body>
<p>
testing to see which scripting languages work
</p>
<?php
echo "php works";
?>
<script type="text/javascript">
document.write("javascript works")
</script>
</body>
</html>

I notice you are able to 'run' HTML and javascript code by opening the text file with a browser (I use Chrome). 我注意到您可以通过使用浏览器(我使用Chrome)打开文本文件来“运行” HTML和javascript代码。 But when I view the page source inside the browser, the php is commented out. 但是,当我在浏览器中查看页面源代码时,php被注释掉了。 This same code is loaded onto my website the university provides, and the same thing happens there. 相同的代码已加载到大学提供的我的网站上,并且发生了同样的事情。

I've looked around for how to solve this, I figured you need to have some sort of flag indicating the page runs php, but I can't find anything. 我到处寻找解决方法,我认为您需要某种标记来指示页面运行php,但是我什么也找不到。

Here is my webspace if it helps: Removed for privacy purposes 这是我的网站空间,如果有帮助的话:出于隐私目的而删除

Thanks everyone for the responses, I was able to get it to work by changing it to a php file. 感谢大家的答复,我将其更改为php文件,从而使其能够正常工作。

The usual flag for indicating that a file contains PHP is to give it a filename ending in .php . 指示文件包含PHP的通常标志是给它一个以.php结尾的文件名。

Support from the server is, of course, required. 当然,需要服务器的支持。

In more general terms, there are lots of different ways that this can be done, but they depend on the specific web server software that is being in use. 笼统地说,可以采用多种不同的方法来完成此操作,但是它们取决于所使用的特定Web服务器软件。

With Apache HTTPD, you use the directive SetHandler application/x-httpd-php inside a conditional. 在Apache HTTPD中,可以在条件内使用指令SetHandler application/x-httpd-php

For example, to take the Debian php5.conf file: 例如,获取Debian php5.conf文件:

<FilesMatch "\.ph(p3?|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

This causes any files with a name that matches that regular expression (ie end in .php, .php3 or .phtml) to be processed by PHP. 这将导致任何名称与该正则表达式匹配的文件(即以.php,.php3或.phtml结尾)都将被PHP处理。

For certain, your server is running php. 可以肯定的是,您的服务器正在运行php。 I tried to invent a web page name that i was certain didn't exist and got this error: 我试图发明一个我确定不存在的网页名称,并收到此错误:

Apache/2.2.3 (Red Hat) DAV/2 mod_pubcookie/3.3.4a PHP/5.1.6 mod_python/3.2.8 Python/2.4.3 mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5 mod_perl/2.0.4 Perl/v5.8.8 Server at home.engineering.iastate.edu Port 80

You're running PHP, alright. 您正在运行PHP,好的。 Like everyone else has mentioned, you'll have to ensure that the filename extension of that file whose code you posted above ends in .php then try it again. 就像其他所有人都提到的那样,您必须确保上面发布其代码的文件的文件扩展名以.php结尾,然后重试。

You can't just open a php script in a browser, because it has to be executed by the php program first. 您不能只在浏览器中打开php脚本,因为它必须首先由php程序执行。 This is done by a webserver (you can install one to test on your own machine, see, eg, XAMPP ). 这是由Web服务器完成的(您可以在自己的计算机上安装一个进行测试,例如XAMPP )。

If the server is configured to execute php (which it seems to) , you should be able to make it work by naming your script index.php . 如果将服务器配置为执行php (看起来是这样) ,则应该可以通过命名脚本index.php使其工作。 The file extension .php tells the server to run the script first before sending anything to the browser. 文件扩展名.php告诉服务器在向浏览器发送任何内容之前先运行脚本。

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

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