简体   繁体   English

如何在Chrome或Firefox中“控制台记录” PHP代码?

[英]How to 'console log' PHP code in Chrome or Firefox?

I've been looking up how I can debug PHP code in Chrome or Firefox but I can;t really find a solution. 我一直在寻找如何在Chrome或Firefox中调试PHP代码的方法,但我找不到真正的解决方案。 This is my PHP: 这是我的PHP:

<?php
    if(isset($_POST["data"]))
    {
        $var = $_POST["data"];
        print "your message: " . $_POST["data"];
        if(!empty($_POST['ip.data'])){
        $data = $_POST['ip.data'];
        $fname = mktime() . ".txt";//generates random name

        $file = fopen("upload/" .$fname, 'w');//creates new file
        fwrite($file, $data);
        fclose($file);
        }
    }
?>

I want to be able to see the output of print "your message: " . $_POST["data"]; 我希望能够看到print "your message: " . $_POST["data"];的输出print "your message: " . $_POST["data"]; print "your message: " . $_POST["data"]; or any errors in Chrome or Firefox. 或Chrome或Firefox中的任何错误。 I've tried Firefox Quantum that should be able to debug php? 我试过了应该可以调试php的Firefox Quantum? Anyways, how can I console log this? 无论如何,我该如何控制台登录?

The first step is to recognize that PHP, which is generally a server side language is a completely different context than the browser's console, which is fundamentally Javascript. 第一步是要认识到,通常是服务器端语言的PHP与从根本上是Javascript的浏览器控制台是完全不同的上下文。 Thus, to show messages to the browser's console from the server, you will need to find some way to communicate those messages (eg, errors) to the browser. 因此,要从服务器向浏览器的控制台显示消息,您将需要找到某种方式将这些消息(例如错误)传达给浏览器。

At that point, you might consider something as simple as embedding a script tag with your PHP: 到那时,您可能会考虑将脚本标签嵌入PHP那样简单:

function debugToBrowserConsole ( $msg ) {
    $msg = str_replace('"', "''", $msg);  # weak attempt to make sure there's not JS breakage
    echo "<script>console.debug( \"PHP DEBUG: $msg\" );</script>";
}
function errorToBrowserConsole ( $msg ) {
    $msg = str_replace('"', "''", $msg);  # weak attempt to make sure there's not JS breakage
    echo "<script>console.error( \"PHP ERROR: $msg\" );</script>";
}
function warnToBrowserConsole ( $msg ) {
    $msg = str_replace('"', "''", $msg);  # weak attempt to make sure there's not JS breakage
    echo "<script>console.warn( \"PHP WARNING: $msg\" );</script>";
}
function logToBrowserConsole ( $msg ) {
    $msg = str_replace('"', "''", $msg);  # weak attempt to make sure there's not JS breakage
    echo "<script>console.log( \"PHP LOG: $msg\" );</script>";
}

# Convenience functions
function d2c ( $msg ) { debugToBrowserConsole( $msg ); }
function e2c ( $msg ) { errorToBrowserConsole( $msg ); }
function w2c ( $msg ) { warnToBrowserConsole( $msg ); }
function l2c ( $msg ) { logToBrowserConsole( $msg ); }

if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
    if ( isset( $_POST['data'] ) ) {
        d2c( "Your message: {$_POST['data']}" 
        e2c( "This is an error from PHP" );
        w2c( "This is a warning from PHP" );
        l2c( "This is a log message from PHP" );
        ...
    }
}

But this will be a fundamentally weak and brittle approach. 但这将是一个根本上薄弱的方法。 I would suggest instead tailing your log files on the server directly. 我建议改为直接将日志文件拖到服务器上。 If you are after some color, consider using clog , lwatch , or grc : 如果您追求某种颜色,请考虑使用cloglwatchgrc

$ grc tail -f /var/log/syslog

echo“ console.log('Debug Objects:”。$ output。“');”;

I ran through the same problem recently, just couldn't find a simple enough way without installing some large external package. 我最近遇到了同样的问题,只是在没有安装一些大型外部软件包的情况下找不到足够简单的方法。

I first tried the obvious way: 我首先尝试了明显的方法:

<?php echo "<script>console.log(".$myVar.")<script>" ?>

but it only works with scalar types. 但它仅适用于标量类型。 For example: 例如:

<?php
    $arr = [ 'x' => 42 ];
    echo "<script>console.log(".$arr.")</script>";
?>

will output to the html 将输出到html

<script>console.log(Array)</script>

a solution to this is to use json_encode on the variable in the php side, then JSON.parse it in the javascript and finally console.log . 一个解决方案是在php端的变量上使用json_encode ,然后在javascript中使用JSON.parse ,最后在console.log

However this approach fails to capture non public properties of objects: 但是,这种方法无法捕获对象的非公共属性:

<?php
    class Test {
        private $x = 42;
        public $y = 13;
    }
    $obj = json_encode(new Test());
    echo "<script>console.log(JSON.parse('".$obj."'))</script>";
?>

will output to the browser console: 将输出到浏览器控制台:

{y: 13}

Because private/protected fields can't be accessed by json_encode . 因为json_encode不能访问私有/受保护的字段。

The solution here is either to add a __toString method to your class where you properly expose those fields as strings, or use some hack like calling var_export then process the output string to make it json_encode -able. 这里的解决方案是在类中添加__toString方法,在其中您将这些字段正确地公开为字符串,或者使用某些技巧,例如调用var_export然后处理输出字符串以使其成为json_encode -able。

I ended up writing a small helper using the latter approach, and an output prettifier in the javascript side 我最终使用后一种方法编写了一个小助手,并在javascript端编写了一个输出前缀

Leaving the link here if anyone wants to use it. 如果有人要使用链接,请把链接留在这里

I hope will help: 希望对您有所帮助:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

If you want to see errors on an Ubuntu machine and you run an Apache server, you can constantly monitor and output changes to the error.log file in the apache folder with this command: 如果您想在Ubuntu机器上看到错误并运行Apache服务器,则可以使用以下命令不断监视并输出对apache文件夹中error.log文件的更改:

tail -f /var/log/apache2/error.log

If you have a server running on apache then this will output any errors occurred. 如果您的服务器在apache上运行,则将输出发生的任何错误。

The tail command simply outputs the last 10 lines of a file and updates when new data is piped into the file. tail命令仅输出文件的最后10行,并在将新数据传递到文件中时更新。

Try this 尝试这个

  <?php
  function temp()
  {
   if(isset($_POST["data"]))
   {
    $var = $_POST["data"];
    print "your message: " . $_POST["data"];
    if(!empty($_POST['ip.data'])){
    $data = $_POST['ip.data'];
    $fname = mktime() . ".txt";//generates random name

    $file = fopen("upload/" .$fname, 'w');//creates new file
    fwrite($file, $data);
    fclose($file);
    }
  }
 }//function
 ?>
 <script>
   console.log(".<?php temp(); ?>.");
 </script>

On Chrome, you can use phpconsole which works quite well. 在Chrome上,您可以使用效果很好的phpconsole If anybody knows of something similar for Firefox Quantum please comment. 如果有人知道Firefox Quantum的相似之处,请发表评论。

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

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