doubu7425 2012-01-11 01:51
浏览 57
已采纳

PHP重定向会拉出空白页面(仅在部署在服务器上时)

I am deploying a CMS I developed on my computer to my first domain server and I am running into issues with redirecting to pages in PHP (works in javascript) using a function that calls:

    header("Location: {$location}");

Everything works properly on my home machine through WAMP. But after I deployed on the server it is redirecting to a blank page. I have tested the issue in a separate file and found that it WORKS as long as only the file that contains the redirect_to function is included, so it looks like this:

    <?php //require_once("includes/session.php");?>
    <?php require_once("includes/functions.php");?>
    <?php //require_once("includes/constants.php"); ?>
    <?php //require_once("includes/connection.php"); ?>
    <?php 
        define("DB_SERVER", "mysite.ipagemysql.com");
        define("DB_USER", "root");
        define("DB_PASS", "notreal");
        define("DB_NAME", "myCMS");

        redirect_to("login.php");
        phpinfo();
     ?>

This is the redirect function I have defined in functions.php:

     // redirects the user to the page/path specified by $location
     function redirect_to($location){
         if($location != NULL){
             header("Location: {$location}");
             exit();
          }
     }

Notice that I can only include the functions.php file, if I include any other the redirect no longer works, just goes to a blank page... I can copy the code that was in the included files into this file like I did above with the constants.php file which looks like this:

     <?php

            // Database constants
        define("DB_SERVER", "mysite.ipagemysql.com");
        define("DB_USER", "root");
        define("DB_PASS", "notreal");
        define("DB_NAME", "myCMS");
     ?>

I CAN load all the CMS pages regularly from the Domain server, access the DB and save to it. Since I can save to the DB I am assuming there are no issues with including any of the files I am testing to include because they are all needed to access the DB. The problem is purely the redirect. Everything works fine how it is set up running on my comp as localhost.

I have been looking up this issues and trying solutions all day. I have narrowed it down to the problem of not being able to include other files, but I don't understand why and it's only happening on the Domain server...

Any help or insight would be greatly appreciated, thanks for taking the time to read this.

  • 写回答

4条回答 默认 最新

  • doupi4649 2012-01-11 01:55
    关注

    It might help if you turn error reporting to E_ALL for the time being. It looks like you probably have some output being sent to the browser from another included file. Often the output is easy to miss like an extra carriage return after your closing ?> tag. It is best to omit the closing tags on your included files as they aren't necessary and will prevent accidental output.

    If you are getting unseen output this means the headers have already been sent by the time the Location header is sent so it doesn't work.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度