SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session_start() [function.session-start] (What is this error?)

    Hey,

    I am getting an error on one of my website pages. If you take a look at this link:-

    http://www.judgethejob.com/vinny/join-us/

    You will i get this error:-

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /domains/judgethejob.com/http/vinny/join-us/index.php:11) in /domains/judgethejob.com/http/vinny/join-us/index.php on line 204
    Now, i have the following code and i have ob_start which i thought would prevent this error..?

    PHP Code:
             <?php
                session_start
    ();
                
    ob_start();
                
                if (isset(
    $_POST['btn-submit'])) 
                {     
                    include(
    "../conn.php");
                    
                    
    $emailcheck $_POST['txt_email'];
                    
                    
    $email_check mysql_query("SELECT email FROM tbl_members WHERE email = '$emailcheck'") or die(mysql_error());
                    
    $email_check2 mysql_num_rows($email_check);
                                    
                    if (
    $email_check2 != 0) {
                    echo 
    "<p>Sorry, the email '".$_POST['txt_email']."' is already in use. <a href='javascript:history.go(-1)'> Return</a> to amend.</p>";
                    }
                    else 
                    {
                    
    $insert "INSERT INTO tbl_members(username, password, email, gender, postcode, DOB, education, date_added) VALUES 
                    (
                                    // Insert here...
                    )"
    ;
                    
    $add_member mysql_query($insert);
                    
                    
    $from "ibrar@freemanholland.com";
                    
    $headers "From: $from";
                    
    $to $_POST['txt_email'];
                    
    $subject "This is a test";
                    
    $body "Thank you for registering with Judge the Job. 
                    One of our team will contact you within 24 hours about 
                    your registration to confirm your details. If ok we will 
                    make your account active and you will be able to place your 
                    offer immediately. 
                    
                    Meanwhile please keep your login details safe: E-mail: "
    .$_POST['txt_email']." Password: ".$_POST['txt_password']."";
                    
    //$body = "Thank you for registering on our website. Your login details are as follows:- Email Address: ".$_POST['txt_email']." and Username: ".$_POST['txt_username']."";
                    
                    
    if (mail($to$subject$body$headers)) {
                      echo(
    "<p>Many thanks for registering – one of our team will contact you within 24 hours about your registration to confirm your details. If ok we will make your account active and you will be able to place your offer immediately.</p>");
                     } else {
                      echo(
    "<p>Message delivery failed... please try again later, or contact the administrator.</p>");
                     }
                    }    
                }
    How can i prevent this?

    Regards

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to make sure that NOTHING is sent to the browser before session_start is called. Output buffering to prevent it is usually a hack to overcome dodgy programming, so try and avoid that.

    If your code snippet is anything to go by, you have whitespace before the opening PHP tag. That counts as sending something to the browser - therefore you can't start a session.

    If you're outputting HTML or whatever before parsing this script, that will also cause this error. A simple solution would be to start the session in the main file, one that's always included - e.g. if you have a header file which outputs HTML before scripts are run, put the session start at the top of that. It may be your database connection file; any file which is included in the majority of pages and is run before anything is output.

    Remove that space, see what happens.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    That worked i will keep that in mind for the future..

    Regards

  4. #4
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, nothing can be outputted to the browser before you do a session start or even a header redirect.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i had the same problem session ... nothing can be passed not even a single whitespace before start_session()

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In the way of background, what separates headers from content in an HTTP response is a single blank line. Once your script has any output, including whitespace, in order to print that output, PHP must send the blank line ending the headers. At that point, it's too late to start the session, since sessions are maintained through a cookie. Cookies are sent as HTTP headers, and any output after that blank line is interpreted by the browser as content rather than a header, so it can't be output at this point.


Bookmarks

Posting Permissions

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