PHP不重定向 - 没有错误?

时间:2015-01-25 16:48:45

标签: php redirect

我添加了将我重定向到另一个页面的代码段。 但它根本不会改变我的方向吗? 代码执行正常,没有空白页面或错误消息。我认为这很奇怪。 所以我不知道可能出现什么问题。

这是什么,以及如何解决它?

<?php
if(isset($_POST['ingameban']))
{
//recompose the banlist
$cache_ig = "banlist_cache.txt";
$fh = fopen($cache_ig, 'w') or die("can't open file");
$contents = $_POST['text'];
fwrite($fh, $contents);
$con=mysqli_connect("localhost","root","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM banlist");
while($row = mysqli_fetch_array($result)) {
$uid = $row['uid'];
$player = $row['Player'];
$admin = $row['Admin'];
$bannedon = $row['Bannedon'];
$unbanned = $row['Unbanned'];
$reason = $row['Reason'];
$pin_db = $row['PIN'];
$content = "\n";
fwrite($fh, $content);
$content = $uid;
fwrite($fh, $content);
$content = " # ";
fwrite($fh, $content);
$content = $admin;
fwrite($fh, $content);
$content = " banned ";
fwrite($fh, $content);
$content = $player;
fwrite($fh, $content);
$content = " on ";
fwrite($fh, $content);
$content = $bannedon;
fwrite($fh, $content);
$content = " reason ";
fwrite($fh, $content);
$content = $reason;
fwrite($fh, $content);
$content = " unbanned on ";
fwrite($fh, $content);
$content = $unbanned;
fwrite($fh, $content);
} 

fclose($fh);    
?>
<?php
header("Location: new-ban_upload.php");
die();
?>

除重定向外,代码执行正常。

header("Location: new-ban_upload.php");
    die();

谢谢

1 个答案:

答案 0 :(得分:1)

第一个显而易见的事情是你错过了一个结束}

- 代码格式是你的朋友,

<?php
if(isset($_POST['ingameban']))
{
//recompose the banlist
    $cache_ig = "banlist_cache.txt";
    $fh = fopen($cache_ig, 'w') or die("can't open file");
    $contents = $_POST['text'];
    fwrite($fh, $contents);
    $con=mysqli_connect("localhost","root","pass","db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM banlist");
    while($row = mysqli_fetch_array($result)) {
        $uid = $row['uid'];
        $player = $row['Player'];
        $admin = $row['Admin'];
        $bannedon = $row['Bannedon'];
        $unbanned = $row['Unbanned'];
        $reason = $row['Reason'];
        $pin_db = $row['PIN'];
        $content = "\n";
        fwrite($fh, $content);
        $content = $uid;
        fwrite($fh, $content);
        $content = " # ";
        fwrite($fh, $content);
        $content = $admin;
        fwrite($fh, $content);
        $content = " banned ";
        fwrite($fh, $content);
        $content = $player;
        fwrite($fh, $content);
        $content = " on ";
        fwrite($fh, $content);
        $content = $bannedon;
        fwrite($fh, $content);
        $content = " reason ";
        fwrite($fh, $content);
        $content = $reason;
        fwrite($fh, $content);
        $content = " unbanned on ";
        fwrite($fh, $content);
        $content = $unbanned;
        fwrite($fh, $content);
    } 

    fclose($fh);    
    header("Location: new-ban_upload.php");
    die();
    ?>

接下来就是你不需要这样做:

$uid = $row['uid'];
$player = $row['Player'];
$admin = $row['Admin'];
$bannedon = $row['Bannedon'];
$unbanned = $row['Unbanned'];
$reason = $row['Reason'];
$pin_db = $row['PIN'];
$content = "\n";
fwrite($fh, $content);
$content = $uid;
fwrite($fh, $content);
$content = " # ";
fwrite($fh, $content);
$content = $admin;
fwrite($fh, $content);
$content = " banned ";
fwrite($fh, $content);
$content = $player;
fwrite($fh, $content);
$content = " on ";
fwrite($fh, $content);
$content = $bannedon;
fwrite($fh, $content);
$content = " reason ";
fwrite($fh, $content);
$content = $reason;
fwrite($fh, $content);
$content = " unbanned on ";
fwrite($fh, $content);
$content = $unbanned;
fwrite($fh, $content);

你可以用以下内容替换所有内容:

$uid = $row['uid'];
$player = $row['Player'];
$admin = $row['Admin'];
$bannedon = $row['Bannedon'];
$unbanned = $row['Unbanned'];
$reason = $row['Reason'];
$pin_db = $row['PIN'];

$content = "\n" . "$content $uid # $admin banned $player on $bannedon reason $reason unbanned on $unbanned";
fwrite($fh, $content);

给出了:

<?php
if(isset($_POST['ingameban']))
{
//recompose the banlist
    $cache_ig = "banlist_cache.txt";
    $fh = fopen($cache_ig, 'w') or die("can't open file");
    $contents = $_POST['text'];
    fwrite($fh, $contents);
    $con=mysqli_connect("localhost","root","pass","db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM banlist");
    while($row = mysqli_fetch_array($result)) {

        $uid = $row['uid'];
        $player = $row['Player'];
        $admin = $row['Admin'];
        $bannedon = $row['Bannedon'];
        $unbanned = $row['Unbanned'];
        $reason = $row['Reason'];
        $pin_db = $row['PIN'];

        $content = "\n" . "$content $uid # $admin banned $player on $bannedon reason $reason unbanned on $unbanned";
        fwrite($fh, $content);
    } 

    fclose($fh);    
    header("Location: new-ban_upload.php");
    die();
}
?>

现在您可以进行调试:

1)如果页面上有任何内容,标题将不会重定向:您有一个回声,所以将其更改为die()

<?php
if(isset($_POST['ingameban']))
{
//recompose the banlist
    $cache_ig = "banlist_cache.txt";
    $fh = fopen($cache_ig, 'w') or die("can't open file");
    $contents = $_POST['text'];
    fwrite($fh, $contents);
    $con=mysqli_connect("localhost","root","pass","db");
    // Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $result = mysqli_query($con,"SELECT * FROM banlist");
    while($row = mysqli_fetch_array($result)) {

        $uid = $row['uid'];
        $player = $row['Player'];
        $admin = $row['Admin'];
        $bannedon = $row['Bannedon'];
        $unbanned = $row['Unbanned'];
        $reason = $row['Reason'];
        $pin_db = $row['PIN'];

        $content = "\n" . "$content $uid # $admin banned $player on $bannedon reason $reason unbanned on $unbanned";
        fwrite($fh, $content);
    } 

    fclose($fh);    
    header("Location: new-ban_upload.php");
    die();
}
?>

然后,您可以添加注释中的调试级别代码:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);

if(isset($_POST['ingameban']))
{
//recompose the banlist
    $cache_ig = "banlist_cache.txt";
    $fh = fopen($cache_ig, 'w') or die("can't open file");
    $contents = $_POST['text'];
    fwrite($fh, $contents);
    $con=mysqli_connect("localhost","root","pass","db");
    // Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $result = mysqli_query($con,"SELECT * FROM banlist");
    while($row = mysqli_fetch_array($result)) {

        $uid = $row['uid'];
        $player = $row['Player'];
        $admin = $row['Admin'];
        $bannedon = $row['Bannedon'];
        $unbanned = $row['Unbanned'];
        $reason = $row['Reason'];
        $pin_db = $row['PIN'];

        $content = "\n" . "$content $uid # $admin banned $player on $bannedon reason $reason unbanned on $unbanned";
        fwrite($fh, $content);
    } 

    fclose($fh);    
    header("Location: new-ban_upload.php");
    die();
}
?>

现在它应该告诉你什么是错的