Page 1 of 1

PHP Header() error

Posted: Fri Jun 26, 2009 11:41 pm
by samiullah_php
Hi,

As i am developing a small CRUD(create,read,update,delete) application. In this application the following errors persists.

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\WishList\index.php:3) in C:\xampp\htdocs\WishList\index.php on line 12
 
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\WishList\index.php:3) in C:\xampp\htdocs\WishList\index.php on line 14
Can anyone provide solution for this one.

Re: PHP Header() error

Posted: Sat Jun 27, 2009 12:24 am
by requinix
The "solution" is to rearrange your code so you aren't calling session_start after output has started.

Code: Select all

// bad:
echo "output";
session_start();
 
// good:
session_start();
echo "output";

Re: PHP Header() error

Posted: Sat Jun 27, 2009 12:45 am
by samiullah_php
I wrote the code like that. Ok could you please see the code. I am sending.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<?php
 
require_once("Includes/db.php");
$logonSuccess = true;
 
//Verifying the User's Credentials
if ($_SERVER["REQUEST_METHOD"] == "POST"){
 
    if (WishDB::getInstance()->verify_wisher_credentials($_POST["user"], $_POST["userpassword"]) == 1) {
        session_start();
        $_SESSION["user"] = $_POST["user"];
        header('Location: editWishList.php');
    } else {
        $logonSuccess = false;
    }
}
 
?>
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="icon" href="Images/favicon.gif" type="image/gif"/>
        <title>Wish List Application</title>
    </head>
    <body>
    <center>
        <form action="wishlist.php" method="GET" name="wishList">
        <font color="black" size="4" face="verdana">Show wish list of: </font><input type="text" name="user"/>
        <input type="submit" value="Go!"/>
        </form>
        
 
        <form name="logon" action="index.php" method="POST" >
        <table border="1" bgcolor="#FFFFCC">
        
            <tr>
            <td><font color="black" face="verdana" size="3"><b>Username:</b></font>
            </td>
            <td>
                <input type="text" name="user">
            </td>
            </tr>
 
            <tr>
            <td><font color="black" face="verdana" size="3"><b>Password:</b></font></td>
            <td>
                <input type="password" name="userpassword">
            </td>
            </tr>
 
            <?php
 
            if(!$logonSuccess)
            echo '<font color="red" face="verdana" size="2"><b>Invalid Username and/or Password</b></font>';
 
            ?>
 
            <tr>
            <th colspan=2><input type="submit" value="Edit My WishList"</th>
            </tr>
        
        </table>
        <br>
 <font color="black" face="verdana" size="2">Still don't have a wish list?!</font><a href="createNewWisher.php"><font color="purple" face="verdana" size="3"><b>Create now</b></font></a>
       </form>
 
   </center>
    
</body>
</html>
 
When i run this the above said errors persists. Please further clarification required. Thanks for your immediate response.

Re: PHP Header() error

Posted: Sat Jun 27, 2009 1:29 am
by requinix

Code: Select all

1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Output began right there. You have to call session_start before that.

Re: PHP Header() error

Posted: Sat Jun 27, 2009 2:03 am
by samiullah_php
I did it but the following error still persists.

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\WishList\index.php:7) in C:\xampp\htdocs\WishList\index.php on line 18

Re: PHP Header() error

Posted: Sat Jun 27, 2009 11:47 am
by McInfo
Post your updated script.

Edit: This post was recovered from search engine cache.

Re: PHP Header() error

Posted: Sat Jun 27, 2009 1:26 pm
by a94060
There is a conflict between session_start() and your header() statement. The headers are already sent by your session_start(), which is why the header() is giving a problem. JS redirect should fix the problem

Re: PHP Header() error

Posted: Sat Jun 27, 2009 2:49 pm
by McInfo
a94060 wrote:There is a conflict between session_start() and your header() statement.
Not true. session_start() and header() are compatible.

Try this.

Code: Select all

<?php
session_start();
if (!isset($_SESSION['test'])) {
    $_SESSION['test'] = 'hello';
    header('Location: '.basename(__FILE__));
    exit;
} else {
    echo 'Test: '.$_SESSION['test'];
}
?>
Because the output on the first run is
Test: hello
the script obviously runs twice, which means that header() is called successfully after session_start().

Edit: This post was recovered from search engine cache.

Re: PHP Header() error

Posted: Sat Jun 27, 2009 9:57 pm
by a94060
im sorry. I am still rusty. Havent programmed in a loong time, so i am trying to get back into it by reading on forums and attempting to help. Sorry if i led you in the wrong direction