【问题标题】:How to prevent echo in PHP and get what it is inside?如何防止 PHP 中的 echo 并获取它的内部内容?
【发布时间】:2016-08-31 12:22:55
【问题描述】:

参考这篇文章how-to-prevent-echo-in-php-and-catch-what-it-is-inside 我正在尝试从下面提到的 php 文件中获取输出值,但我仍然可以看到这些值正在打印在我的 php 页面的输出中。也欢迎任何其他建议来获取输出内容从我的 php 文件到字符串而没有得到回显。谢谢!

<?php include('Crypto.php')?>
<?php       
    $workingKey='XXXX';     //Working Key should be provided here.
    $encResponse=$_POST["encResp"];         //This is the response sent by the Server
    $rcvdString=decrypt($encResponse,$workingKey);      //Crypto Decryption used as per the specified working key.      
    $order_status="";
    $order_id=0;        
    $decryptValues=explode('&', $rcvdString);
    $dataSize=sizeof($decryptValues);
    echo "<center>";    
    for($i = 0; $i < $dataSize; $i++) 
    {
        $information=explode('=',$decryptValues[$i]);
        if($i==0)   $order_id = $information[1];
        if($i==1)   $tracking_id = $information[1];
        if($i==3)   $order_status = $information[1];
    }
       ob_start();
       echo $order_id."_";  
       $out1 = ob_get_contents();
      echo $tracking_id."_";
      $out2 = ob_get_contents();
      echo $order_status;
      $out3 = ob_get_contents();
      ob_end_clean();
      var_dump($out3);
?>

以 HTML 格式获取回显值的 JAVASCRIPT 代码

class MyJavaScriptInterface
        {
            @JavascriptInterface
            @SuppressWarnings("unused")
            public void processHTML(final String html)
            {


               String order_page = ""+Html.fromHtml(html);//process php output to html

               String CCAvenueOrder_id = order_page.split("\\_")[0];
               String CCAvenueTacking_id=order_page.split("\\_")[1];
               String CCAvenueOrderStatus=order_page.split("\\_")[2];


                // process the html as needed by the app
                String status = null;

                if(html.indexOf("Failure")!=-1){
                    status = "Transaction Declined!";
                }else if(html.indexOf("Success")!=-1){
                    status = "Transaction Successful!";
                }else if(html.indexOf("Aborted")!=-1){
                    status = " Transaction Cancelled!";
                }else{
                    status = "Status Not Known!";
                }
                //Toast.makeText(getApplicationContext(), status,     Toast.LENGTH_SHORT).show();
                Intent intent = new     Intent(getApplicationContext(),StatusActivity.class);

                startActivity(intent);
            }
        }

【问题讨论】:

  • 你的意思是像 JavaScript 中的“console.log”吗?
  • 存储数据而不是回显它。然后您可以将该信息写入文件、数据库、error_log、通过电子邮件发送等。
  • 你为什么要这样做?不能在变量中存储数据?
  • 我正在设计一个应用程序,来自服务器的响应在上面的 php 文件中,现在我希望这个响应显示在我的应用程序页面中

标签: javascript php android html


【解决方案1】:

就像在问题的 cmets 中一样,您可以将输入存储在变量中。没有必要使用输出缓冲区,至少在您的示例中没有。

<?php       
    $workingKey='XXXX';     //Working Key should be provided here.
    $encResponse=$_POST["encResp"];         //This is the response sent by the Server
    $rcvdString=decrypt($encResponse,$workingKey);      //Crypto Decryption used as per the specified working key.      
    $order_status="";
    $order_id=0;        
    $decryptValues=explode('&', $rcvdString);
    $dataSize=sizeof($decryptValues);
    $output = "<center>";    
    for ($i = 0; $i < $dataSize; $i++) {
        $information=explode('=',$decryptValues[$i]);
        if($i==0)   $order_id = $information[1];
        if($i==1)   $tracking_id = $information[1];
        if($i==3)   $order_status = $information[1];
    }
    $output .= $order_id."_";  
    $output .= $tracking_id."_";
    $output .= $order_status;
    echo $output; // response for ajax request as simple string
?>

如果这不适合您,请告诉我们正在回显的内容。

【讨论】:

  • 是的,通过使用您的代码,什么都不会被回显,但我需要在 String 中获取这些变量值(以便在我的应用程序中进行进一步处理),所以我使用 Javascript 类 [已编辑问题代码] 来获取php页面的html内容
  • 那么您使用的是 Ajax 并希望从服务器获取响应?
  • 是的,Philipp 正是,你有我的问题
  • 如果您使用的是 JQuery,您可以定义响应的类型。在这种情况下,您还必须将您定义的类型发回。我记得一个简单的字符串或 json 是最常见的。应该提供响应的脚本必须回显它。我修改了答案。
【解决方案2】:

检查运行代码内部情况的最佳方法是使用调试器,例如xdebug。了解如何安装它并与您选择的 IDE 一起使用,然后放置断点并使用监视来查看变量内部。 Here's how to do it with PhpStorm.

如果您无法安装调试器,或者您绝对需要保留这些值,请学习日志的概念。有一个用于日志记录的 PHP-FIG 标准 (PSR-3) 和至少一个实现它的工具 - 例如,Monolog

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2021-11-09
    • 2023-01-09
    相关资源
    最近更新 更多