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条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失