-3

可能重复:
PHP 已发送的标头

我有一个创建文件的功能。如果成功,我希望它将用户重定向到 X 页面.. 在这种情况下为 1.php.... 但它不起作用。PHP 脚本在顶部......所以从技术上讲它应该可以工作

如果我把它放在header ()createFile () 函数里面,它就可以工作,但如果我把它放在 if 语句里面,它就不行....

<?php 
    //DB Config File
    $dbFile = 'dbconfig.php';


    function createfile ($dbFile) {
            //Creates File and populates it.
            $fOpen = fopen($dbFile, 'w');

                $fString .= "<?php\n";
                $fString .= "// Database Constants\n";
                $fString .= "define(\"DB_SERVER\", \"$server\");\n";
                $fString .= "define(\"DB_USER\", \"$username\");\n";
                $fString .= "define(\"DB_PASS\", \"$password\");\n";
                $fString .= "define(\"DB_NAME\", \"$dbname\");\n";
                $fString .= "?>";

            fwrite($fOpen, $fString);
            fclose($fOpen);

    }

    if (isset($_POST['submit'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];
    $server = $_POST['server'];
    $dbname = $_POST['dbname'];

    try {
    $db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);

    if ($db) { //if succesful at connecting to the DB

    if (file_exists($dbFile)){
        if (is_readable($dbFile) && is_writable($dbFile)){ 

            //Creates File, populates it and redirects the user

        if (createfile($dbFile)) { 
        $host  = $_SERVER['HTTP_HOST'];
        $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extra = '1.php';
        header("Location: http://$host$uri/$extra");
        exit ();
                }


            } else { 

            $msg = "2The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission.";  }

        } else {

            //Creates File, populates it and redirects the user

        if (createfile($dbFile)) {
        $host  = $_SERVER['HTTP_HOST'];
        $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extra = '1.php';
        header("Location: http://$host$uri/$extra");
        exit ();
                }

            }


    }

    } catch (PDOException $e) { //Catchs error if can't connect to the db.
        $msg = 'Connection failed: ' . $e->getMessage();
    }


    }


    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    </head>

    <body>


    <form id="iForm" method="post" action="install.php">
    <label id="username" name="username">Username</label>
    <input id="username" name="username"/>
    <label id="password" name="password">Password</label>
    <input id="password" name="password" />
    <label id="server" name="server">Server</label>
    <input id="server" name="server"/>
    <label id="dbName" name="dbname">dbName</label>
    <input id="dbName" name="dbname"/>

    <input type="submit" name="submit" value="submit" />
    </form>
    <p id="error"><?php echo $msg ?></p>
    </body>
    </html>
4

2 回答 2

7

它不起作用,因为您已经通过在 header() 函数调用上方将该 HTML 内容输出到浏览器。

您需要将 HTML 输出到输出缓冲区,或者在将任何输出发送到浏览器之前将标头调用移动到脚本的开头附近。

在我看来,没有理由不能简单地将脚本中的初始 HTML 内容向下移动到大部分 PHP 代码之后的位置。

<?php 
//DB Config File
$dbFile = 'dbconfig.php';


function createfile ($dbFile) {
        //Creates File and populates it.
        $fOpen = fopen($dbFile, 'w');

            $fString .= "<?php\n";
            $fString .= "// Database Constants\n";
            $fString .= "define(\"DB_SERVER\", \"$server\");\n";
            $fString .= "define(\"DB_USER\", \"$username\");\n";
            $fString .= "define(\"DB_PASS\", \"$password\");\n";
            $fString .= "define(\"DB_NAME\", \"$dbname\");\n";
            $fString .= "?>";

        fwrite($fOpen, $fString);
        fclose($fOpen);

}

if (isset($_POST['submit'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$server = $_POST['server'];
$dbname = $_POST['dbname'];

try {
$db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);

if ($db) { //if succesful at connecting to the DB

if (file_exists($dbFile)){
    if (is_readable($dbFile) && is_writable($dbFile)){ 

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) { 
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = '1.php';
    header("Location: http://$host$uri/$extra");
    exit ();
            }


        } else { 

        $msg = "2The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission.";  }

    } else {

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) {
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = '1.php';
    header("Location: http://$host$uri/$extra");
    exit ();
            }

        }


}

} catch (PDOException $e) { //Catchs error if can't connect to the db.
    $msg = 'Connection failed: ' . $e->getMessage();
}


}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>

<body>
<form id="iForm" method="post" action="install.php">
<label id="username" name="username">Username</label>
<input id="username" name="username"/>
<label id="password" name="password">Password</label>
<input id="password" name="password" />
<label id="server" name="server">Server</label>
<input id="server" name="server"/>
<label id="dbName" name="dbname">dbName</label>
<input id="dbName" name="dbname"/>

<input type="submit" name="submit" value="submit" />
</form>
<p id="error"><?php echo $msg ?></p>
</body>
</html>
于 2012-08-01T15:32:32.790 回答
2

重定向必须在您向客户端发送任何数据之前发生

于 2012-08-01T15:33:01.827 回答