Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setcookie() vs 'Cannot modify header ' problem

    I think I have an easy problem but cannot get over it somehow...
    I get the classical:

    Warning: Cannot modify header information - headers already sent by (output started at /wn/show_item_list.php:6) in /wn/scripts/db2.php on line 25

    my code is as follows:

    for - show_item_list.php -

    <?php include("db2.php");?>
    <html>
    <head>
    <title>Order Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .boxes {...
    ....
    ...html stuff

    for - db2.php -

    <?php
    // This page contains the connection routine for the
    // database as well as getting the ID of the cart, etc
    session_start();
    $connection=mysql_connect("host", "dbuser", "dbpass")
    or die(mysql_error());
    mysql_select_db("DB", $connection);

    function GetCartId()
    {
    // This function will generate an encrypted string and
    // will set it as a cookie using set_cookie. This will
    // also be used as the cookieId field in the cart table

    if(isset($_COOKIE["CartID"]))
    {
    return $_COOKIE["CartID"];
    }
    else
    {
    // There is no cookie set. We will set the cookie
    // and return the value of the users session ID
    setcookie("CartID", session_id(), time() + ((3600 * 24) * 30));
    return session_id();
    }
    }
    ?>

    The db2.php is at the top of the show_item_list.php - so I do not send anything (html code, or echo's ) to the browser - Im new so I might have missed something?

    Thanks,

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your outputing data bfeore cookie is set

    the cook must be set before this is sent to browser

    <html>
    <head>
    <title>Order Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .boxes {...
    ....
    ...html stuff

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean then - that the setcookie() in the include file db2.php is ran after the html stuff below EVEN that it is at the top of the code?


    <?php include("db2.php");?>
    <html>
    <head>
    <title>Order Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--

    thanks...

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no sorry thought it was one script

    is there any white space a top of script

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,043
    Thanks
    10
    Thanked 93 Times in 91 Posts
    GetCartId() is a function and ignored until called ... where are you calling that function ? chnaces are after you have output some HTML

    less likely is that your script is generating an error before the function is called , e.g. if your mysql_connect() failed ,output would be created, same for any error or notice that your code generates , if it generates the error before the cookie is set you get the above problem.
    Last edited by firepages; 03-25-2004 at 01:01 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yep Firepages, I think I got it now. I call the GetCartId function way down when I output the sql select stuff - and that wont work! So i need to call it on top and put the value from getcartId to a variable - that shoul probably do it!

    Ive also heard that using ob_start() could get rid of these types of errors - but dont know what that statement may effect.

    Thanks a lot...

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,043
    Thanks
    10
    Thanked 93 Times in 91 Posts
    Hi , yes ob_start() at the top of the page will supress any content until flush()ed either explicitly in your script or implicitly when the script execution ends.

    regardless its better to find out exactly what the problem is first (as you have done) and try and work around it rather than use what would otherwise be a hack.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •