PHP Header() error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
samiullah_php
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 11:38 pm

PHP Header() error

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Header() error

Post 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";
samiullah_php
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 11:38 pm

Re: PHP Header() error

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Header() error

Post 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.
samiullah_php
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 11:38 pm

Re: PHP Header() error

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Header() error

Post by McInfo »

Post your updated script.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:37 pm, edited 1 time in total.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: PHP Header() error

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Header() error

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 12:38 pm, edited 1 time in total.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: PHP Header() error

Post 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
Post Reply