Possible Duplicate:
Headers already sent by PHP
I have a function to create a file. If successful, I want it to redirect the user to X page.. in this case 1.php.... but it's not working. The PHP script is on the top... so technicaly speaking it should work
It works if i put the header () inside the createFile () function but not if i put it inside the if statement....
<?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>
 
     
     
    