【问题标题】:How come when I run a .php script in Ubunutu it just echos the script itself?当我在 Ubuntu 中运行 .php 脚本时,它为什么会回显脚本本身?
【发布时间】:2011-11-26 19:52:47
【问题描述】:

我必须解码我命名为 cache.log 的 Chrome 缓存文件之一的二进制内容数据。我试图解码它的方式是使用我在这里找到的 php 脚本

http://www.frozax.com/blog/2011/05/recover-file-google-chrome-cache-gzipped/

我已经用下面的命令安装了php

sudo apt-get install php5 libapache2-mod-php5
sudo apt-get install php5-cgi

当我尝试运行 index1.php 脚本时,我在终端中得到以下信息

s3z@s3z-laptop:~/Desktop$ php index1.php 
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
// cache.log is a copy of chrome cache page with only the file content section
$cacheString = file_get_contents("cache.log");
$matches = array();
preg_match_all('/\s[0-9a-f]{2}\s/', $cacheString, $matches);
$f = fopen("t.bin","wb");
foreach ($matches[0] as $match)
{
  fwrite($f,chr(hexdec($match)));
}
fclose($f);

ob_start();
readgzfile("t.bin");
$decoded_data=ob_get_clean();
echo $decoded_data;

如您所见,它所做的只是回显脚本。

如何实际运行脚本?

【问题讨论】:

  • 看起来像 PHP 的东西实际上是输出的,纯文本。检查文件的语法并确保您的代码包含在<?php ... ?> 标记中。请阅读有关 PHP 基本语法的手册:php.net/manual/en/language.basic-syntax.php

标签: php linux ubuntu terminal


【解决方案1】:

它看起来不像代码被包裹在:

<?php 

?> 

如果没有,它只会回显而不是执行。这样做的原因是 PHP 允许您在同一个文件中混合服务器脚本和 HTML。

<html>
    <head><title></title></head>
    <body>
    <?php 
        //Inside the php block so it gets executed and prints 'Hello World'
        echo "Hello, World!"; 
    ?>
    <!--Outside the php block so it just gets echoed and prints 'echo "Hello, World!";' -->
    echo "Hello, World!"; 
    </body>
</html>

【讨论】:

  • 我遇到了这样一种情况,即代码 确实 具有开头 &lt;?php 和关闭 ?&gt; 但它仍然只回显 HTML 而不是执行 php 代码。 ? (不是 Ubunto;通过远程 shell 连接到共享主机 Web 服务器——Linux 版本未知)
  • 这一定是我正在使用的脚本的问题,因为如果我执行这些命令,我​​会从phpinfo(); 获得执行输出:(第一个命令:)echo "&lt;?php phpinfo() ; ?&gt;" &gt; tst_blargh.php(第二个命令:) php tst_blargh.php(我不必在第二个命令之前运行 chmod +x tst_blargh.php 以使其可执行)
猜你喜欢
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 2020-07-15
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2018-09-23
相关资源
最近更新 更多