【问题标题】:PHP header ('Location: ..') Not working on server [duplicate]PHP标头('位置:..')在服务器上不起作用[重复]
【发布时间】:2015-10-02 05:11:25
【问题描述】:

我是 PHP 新手。 PHP 标头应该重定向到特定页面,但在我的情况下,PHP 重定向在 localhost 上工作正常,但是当我将相同的代码部署到服务器时,它在服务器中不起作用并抛出一个空白页面。我也尝试了以下建议:

这对我的情况没有帮助。

<?php

 include('../config/db.html'); 
 
if(!isset($_SESSION)){
    session_start();
}  
$msgsuccess = '';

if(!empty($_GET['msgsuccess'])){
    $msgsuccess = $_GET['msgsuccess'];
}
if(isset($_POST) && !empty($_POST)){
    if(!empty($_GET['url'])){ 
        $url = $_GET['url'];
    }
                        
    if(!empty($_POST['password'])){
        $pass = md5($_POST['password']);
    }
    if(!empty($_POST['username'])){
        $email = trim($_POST['username']);
    }
    $query = "select * from users where email = '$email' and password = '$pass'";
    $exec_query = mysql_query($query);
    $row = mysql_fetch_array($exec_query);
    if(!empty($row)){
        $_SESSION['adminfirstname'] = $row['firstname'];
        $_SESSION['adminID'] = $row['id'];
        $_SESSION['logged_in_admin'] = true;
        $host  = $_SERVER['HTTP_HOST'];
        $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extra = 'index.htm';
        header("Location: http://$host$uri/$extra");
         die();
    }
    else{
        $msgsuccess = 'INVALID CREDENTIALS!! PLEASE ENTER VALID EMAIL OR PASSWORD';
    }
}


?>

提前致谢。

【问题讨论】:

  • 你遇到了什么错误?
  • 空白页.. @Keerthi
  • &lt;?php error_reporting(E_ALL);ini_set('display_errors',1); 将此添加到您的页面顶部并检查确切的错误。
  • 您是否到达了重定向(在浏览器中查看)。也许这只是这段代码中的另一个错误。
  • 尝试回显echo 'http://'.$host.$uri.'/'.$extra;exit; 之类的网址,看看是否正确。如果是这样,请将其复制粘贴到地址栏中,看看你得到了什么

标签: php redirect


【解决方案1】:

尽量把php代码,放在页面的开头,这样之前就没有输出了,

    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'index.htm';
    $final_url = 'http://'.$host.$uri.'/'.$extra;
    header('Location:'.$final_url);

替换代码..

【讨论】:

  • 感谢回复,我不能把代码放在开头,因为我必须在DB中有数据时才重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
相关资源
最近更新 更多