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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题