douyan4243 2013-02-01 18:04
浏览 54
已采纳

.php文件在屏幕上显示代码。 我该如何解决?

The code in my .php file is displayed as it is on the browser. I'm running apache 2.4 with the XAMPP server and adding <?php phpinfo(); ?> works so I'm guessing the php version 5.4 is working fine too. My index.php file looks like this:

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Something</title>
    </head>
    <body>
        <?php 
        include 'json_encoded.php'; 
        ?>
        <script type='text/javascript'>

            /* @var $image_urls type */
            var jsarray = ["<?php echo join("\", \"", $image_urls); ?>"];
            //document.write(getlength(jsarray));
        </script>
    </body>
</html>

and my json_encoded.php file looks like this:

<?php
        header("Content-type: text/javascript");
        error_reporting(E_ALL);

ini_set('display_errors', '1');
        $url = 'some url';

        $var = fread_url($url);
            preg_match_all ("@((http://web)([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@", 
                            $var, $matches);

            $matches = $matches[1];
            $image_urls = array();
            foreach($matches as $var)
            {   
                $var1 = str_replace("/med/", "/lg/", $var);
                $image_urls[] = $var1;
            }


        // The fread_url function allows you to get a complete
        // page. If CURL is not installed replace the contents with
        // a fopen / fget loop

            function fread_url($url,$ref="")
            {
                if(function_exists("curl_init")){
                    $ch = curl_init();
                    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                                  "Windows NT 5.0)";
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
                    curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
                    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
                    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
                    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
                    curl_setopt( $ch, CURLOPT_URL, $url );
                    curl_setopt( $ch, CURLOPT_REFERER, $ref );
                    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
                    $html = curl_exec($ch);
                    curl_close($ch);
                }
                else{
                    $hfile = fopen($url,"r");
                    if($hfile){
                        while(!feof($hfile)){
                            $html.=fgets($hfile,1024);
                        }
                    }
                }
                return $html;
            }
?>

and my output when running localhost:... looks like this

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Today's Front Pages</title>
    </head>
    <body>
        <script type='text/javascript'>

            /* @var $image_urls type */
            var jsarray = [...contents of array];
                        //document.write(getlength(jsarray));
    </script>
</body>

</html>

Does anyone know what I'm doing wrong? I saw several posts on the topic but they all indicated that php wasn't installed correctly but that's not my case. Incase it's of any importance, I'm using NetBeans 7.2 as my IDE.

Any help is appreciated. Thanks!

  • 写回答

2条回答 默认 最新

  • drbhjey445647 2013-02-01 18:20
    关注

    My comment as an answer:

    Remove header("Content-type: text/javascript"); from json_encoded.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题