Results 1 to 6 of 6

Thread: [RESOLVED] White Spaces Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    108

    Resolved [RESOLVED] White Spaces Error

    Can anyone help me with this error?

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\guestbook.php:1) in C:\xampp\htdocs\guestbook.php on line 1

    I've eliminated all tabs, spaces before and after my line 1, but that error message still displays.

    Code:
    <TITLE>Comments</TITLE><?php session_start();
    if(!isset($_SESSION['nname']))
    {
    $_SESSION['nname'] = 'Anonymous';
    }
    if (!isset($_GET['command']))
    {
    echo "Welcome " . $_SESSION['nname'] . "<br>";
    ?>
    <a href ="?command=add">Add Post</a>
    <a href ="?command=display">Display Post</a>
    <a href ="?command=login">Set Nickname</a>
    <?php 
    }
    else {
    if ($_GET['command'] == 'add'){
    add_comment();
    } 
    elseif ($_GET['command'] == 'adding'){
    adding();
    } 
    elseif ($_GET['command'] == 'display'){
    display_comments();
    } 
    elseif ($_GET['command'] == 'login'){
    set_name();
    } 
    elseif($_GET['command']=='logging'){
    logging();
    }
    }
    function add_comment() 
    {
    ?>
    <form action = "?command=adding" method = POST>
    <textarea rows= 5 name = "comment">Insert your comments here...</textarea>
    <input type = "submit" value = "Post">
    </form>
    <?php 
    }
    function adding() {
    $con1 = mysql_connect("localhost", "root", "password");
    mysql_select_db("guestbook",$con1);
    mysql_query("INSERT INTO comments (userid, comment, when_posted) VALUES (".$_SESSION['nname'].",".$_POST['comment'].", Now()" );
    ?>
    <br>
    <a href = "?command=display">View Comments</a>
    <?php 
    mysql_close($con1);
    }
    function set_name() {
    ?>
    <form action = "?command=logging" method = POST>
    Set your nickname: <input type = "text" name = "nname">
    <br><input type = "submit" value = "Set">
    </form>
    <?php 
    }
    function display_comments() {
    $con = mysql_connect("localhost", "root", "password");
    mysql_select_db("guestbook",$con);
    $results = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    echo "<TABLE BORDER = 1>";
    while($row = mysql_fetch_array($results))
    {
    echo "<TR>";
    echo "<TD BGCOLOR = PINK>" . $row['userid'] . "</TD>";
    echo "<TD BGCOLOR = YELLOW>" . $row['comment'] . "</TD>";
    echo "<TD BGCOLOR = SKYBLUE>" . $row['when_posted'] . "</TD>";
    echo "</TR>";
    }
    echo "</TABLE>";
    mysql_close($con);
    }
    function logging() {
    $_SESSION['nname'] = $_POST['nname'];
    echo "Hello " . $_SESSION['nname'];
    ?>
    <a href = "guestbook.php">Back</a>
    <?php 
    }
    ?>

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: White Spaces Error

    Your problem is the <title> tag - you cannot modify session variables (and some other things) after a page's HTTP headers have been sent. Outputting any HTML forces them to be sent. Move the tag down, below any PHP that modifies a session var.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    108

    Re: White Spaces Error

    That's it. Thanks! One more problem though... Is there something wrong with my query (in the adding() function)... It doesn't save into my database.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    108

    Re: White Spaces Error

    Even with the correct code (the one above is wrong),

    Code:
    mysql_query("INSERT INTO comments (userid, comment, when_posted) VALUES ('".$_SESSION['nname']."','".$_POST['comment']."', Now()" );
    it still doesn't save.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    108

    Re: White Spaces Error

    I get it. Second parenthesis after Now(). This thread is RESOLVED.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] White Spaces Error

    Some advice:

    Your code structure is very hard to follow. It seems to be a muddle of logic and presentation. (Why are the database calls in the middle of the HTML? Why are you using echo to emit the output?)

    As a general rule you should perform all logic (such as authentication and fetching data) before worrying about presentation. In a short script this can be accomplished simply, as in the following template:
    PHP Code:
    <?php
      
    # Open session
      
    session_start();

      
    # Open database connection
      
    $dbh mysql_connect(...);
      
    #...

      # Fetch data into a variable (say $comments)
      #...
    ?>
    <html>
      <!-- Define HTML markup -->

      <!-- Output the data fetched:  -->
      <?php foreach ($comments as $comment): ?>
        <h2><?php echo $comment['title'?></h2>
        <p><?php echo $comment['comment'?></p>
      <?php endforeach; ?>

      <!-- Any other markup -->
    </html>
    This type of structure is much easier to follow, easier to maintain, and scales well if your website or web application develops beyond its original level of complexity.

Posting Permissions

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



Click Here to Expand Forum to Full Width