PHP include 在短标签中运行,即使短标签已关闭

标签 php

NOTE:
This question is NOT about using short tags in PHP. The issue is not the short tags themselves but the way that include functions seem to ignore the disabled status of short tags on this Plesk server.

我有一个新网站需要照顾和修复。该站点是由另一个人构建的,并且是多年前使用非最佳实践构建的。该站点最近已移至 Plesk 服务器。

我不熟悉 Plesk,所以一直在学习它的例程。

我的问题是:

Plesk PHP 设置的核心设置是禁用短标签 (<? ... ?>)。我正在处理的网站广泛使用了短标签(以及完整的 <?php 标签)。

问题是 PHP include 短标签内的代码函数仍然执行和加载,其内容仍然输出到浏览器源 HTML,但短标记中的任何其他内容都不会执行。

为什么会这样?

  • 这是 Plesk 的问题吗?
  • 这是 include 的错误吗? ?

服务器设置:

  • Plesk Onyx(我找不到版本号)
  • PHP 5.6.31(处理程序:FPM 应用程序)
  • PHP ini(直接从站点加载):short_open_tag: Off
  • 包含的 PHP 文件使用完整标签和短标签,结果相同。

我的代码:

HTML 页面(index.php,各种页面):

<?
session_start();
include "inc/dbi.php";
if(isset($_REQUEST['id'])){
    $id = substr($_REQUEST['id'],0,6);
}
else{
    header("Location: index.php?msg=No image specified");
    exit;
    }
$qry        = mysqli_query($MySQlink,"...");
$row        = mysqli_fetch_array($qry);
$docwidth   = floor($row['width']*4.26);
$docwidth  /= 100;
$docheight  = floor($row['height']*04.26);
$docheight /= 100;
$descr      = nl2br($descr);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
...
...

inc/dbi.php 页面:

<?php
$user     = "some_user";
$pass     = "some_pass";
$db       = "some_db";
$MySQlink = mysqli_connect( "localhost" , $user, $pass, $db );
if ( ! $MySQlink )  {   mysqli_close($MySQlink);    }
$now      = date('Y-m-d H:i:s');    
$today    = date('Y-m-d '); 

我希望在 HTML 文件的源代码中看到的内容:

<?
session_start();
include "inc/dbi.php";
if(isset($_REQUEST['id'])){
    $id = substr($_REQUEST['id'],0,6);
}
else{
    header("Location: index.php?msg=No image specified");
    exit;
    }
$qry        = mysqli_query($MySQlink,"...");
$row        = mysqli_fetch_array($qry);
$docwidth   = floor($row['width']*4.26);
$docwidth  /= 100;
$docheight  = floor($row['height']*04.26);
$docheight /= 100;
$descr      = nl2br($descr);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

我实际看到的:

<?
session_start();
$user     = "some_user";
$pass     = "some_pass";
$db       = "some_db";
$MySQlink = mysqli_connect( "localhost" , $user, $pass, $db );
if ( ! $MySQlink )  {   mysqli_close($MySQlink);    }
$now      = date('Y-m-d H:i:s');    
$today    = date('Y-m-d '); 

if(isset($_REQUEST['id'])){
    $id = substr($_REQUEST['id'],0,6);
}
else{
    header("Location: index.php?msg=No image specified");
    exit;
    }
$qry        = mysqli_query($MySQlink,"...");
$row        = mysqli_fetch_array($qry);
$docwidth   = floor($row['width']*4.26);
$docwidth  /= 100;
$docheight  = floor($row['height']*04.26);
$docheight /= 100;
$descr      = nl2br($descr);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

请注意:

我很清楚上面代码的缺点,但这不是我的工作,我的工作是修复这些缺点并使它 [much] 更好。

但是,我对 include 函数的行为感到困惑,并且已经阅读了 various PHP 错误报告以及阅读 include 的手册和 PHP.ini short tags .其中都没有提到这个问题。


我主要担心的是,在一段时间内,数据库连接详细信息会输出到 HTML(显然,此后已更改)。

最佳答案

包含的文件不会在编译阶段读取,而是在运行时读取。

由于您的 PHP 解释器不解释短标记中的代码并将其直接转储到浏览器,因此将 include 语句替换为包含文件的内容是无罪的。
没有 php.ini 设置可以说服它这样做。

我能想到其他原因:

  • 用包含文件的内容替换 include/require 语句的 PHP 扩展;
  • 一个预处理脚本,它执行相同的操作和/或将多个 PHP 文件组合成一个文件(Symfony 做类似的事情);

这种处理的目的是通过最小化磁盘访问来优化脚本。

关于PHP include 在短标签中运行,即使短标签已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45843239/

相关文章:

php - MySQL选择流派问题(php)

php - 拉维尔 : 1066 Not unique table/alias

javascript - jScroll.js 与 Laravel 5 : nextSelector not found - destroying

php - 在不同插件中使用 add_submenu_page 到 add_menu_page 时出错

javascript - 限制 XML 文件中回显的 Ajax 回复数量

php - 从 PHP 脚本启动 FOREVER 或 PM2 作为 WWW-DATA

PHP MySQL 选择 $id 在字符串中的位置

PHP和测试驱动开发-日志类测试

php - SaaS 的身份验证

javascript - 获取 html 复选框列表选中值到隐藏字段