简体   繁体   English

在HTML页面中运行PHP脚本

[英]Running PHP script in html page

I didn't notice this question on the site already and I am having a hard time with it. 我还没有在网站上注意到这个问题,对此我很难过。

I simply want to run a php script at the top of a html page that will check for the existence of a cookie, making in essence a password protected website. 我只想在html页面顶部运行一个php脚本,该脚本将检查cookie的存在,从本质上讲是一个受密码保护的网站。 If the person browsing has the cookie, they are allowed to continue, otherwise they are redirected to a login page. 如果浏览的人拥有cookie,则允许他们继续,否则,它们将被重定向到登录页面。 I am pretty new to web development, but I was led to understand that.... 我是Web开发的新手,但是我被领会到了这一点。

<html>
I am in html!
<?php
//Code goes here
echo "I am in php!";
?>
I am back in html!
</html>

Would work...But I only get the output of the html sections. 可以...但是我只得到html部分的输出。

PHP is processed server side, not client side... HTML on the other hand is processed client side. PHP是服务器端处理的,而不是客户端...另一方面,HTML是客户端处理的。

What you should be doing is rather than running php in an html page is to run html in a php page. 您应该做的不是在html页面中运行php,而是在php页面中运行html。

The reason to this is that if you send both the content and the security mechanism to the client, it would be easy for someone just to bypass the security - look here for example and simply look at the source code. 这样做的原因是,如果将内容和安全性机制都发送给客户端,那么对于某些人来说,仅绕过安全性将很容易- 例如在这里查看并仅查看源代码。

If you wanted proper PHP, You would have something along the lines of: 如果您想要合适的PHP,则可能会遇到以下问题:

if(isset($_COOKIE['MyCookie'])){; 
// Escape PHP and put html here...


//go back to php.
}

You will need a server that can process PHP, but, I should say that there are much better way of protecting pages, such as .htaccess and similar which I urge you to go down. 您将需要一台可以处理PHP的服务器,但是,我应该说,还有一种更好的方法来保护页面,例如.htaccess之类,我敦促您拒绝使用它。

Normally a .html page will not run php codes. 通常,.html页面不会运行php代码。 But we can run PHP code by creating a .htaccess file in the same folder where the .html file is held. 但是我们可以通过在保存.html文件的文件夹中创建一个.htaccess文件来运行PHP代码。 in .htaccess file add this code 在.htaccess文件中添加此代码

AddHandler application/x-httpd-php5 .html .htm AddHandler应用程序/ x-httpd-php5 .html .htm

this code can vary depending on the php version and server you are using 此代码可能会有所不同,具体取决于您使用的php版本和服务器

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

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