Re: Warning: session_start()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 18 May 2011 19:15, Nazish <nazish@xxxxxxx> wrote:
> Hi everyone,
>
> I recently uploaded my website files to a server. When I tried to log into
> my website, I received these error messages:
>
> *Warning*: session_start()
> [function.session-start<http://www.myparcoasis.com/function.session-start>]:
> Cannot send session cookie - headers already sent by (output started at
> /home2/myparcoa/public_html/index.php:10) in *
> /home2/myparcoa/public_html/includes/login_form.php* on line *33*
>
> *Warning*: session_start()
> [function.session-start<http://www.myparcoasis.com/function.session-start>]:
> Cannot send session cache limiter - headers already sent (output started at
> /home2/myparcoa/public_html/index.php:10) in *
> /home2/myparcoa/public_html/includes/login_form.php* on line *33*
>
> *Warning*: Cannot modify header information - headers already sent by
> (output started at /home2/myparcoa/public_html/index.php:10) in*
> /home2/myparcoa/public_html/includes/login_form.php* on line *36*
> *
> *
>
> The website worked fine on the Apache localhost server (I could log in), so
> I'm not sure what's wrong with the code which is creating the error on the
> online server. Any ideas? I've highlighted the two error lines (31 & 34).
> I'd appreciate any insight! Thnx!
>
>
> <!-----------------------------------------------------------------------
> Â Â Â Â Â ÂWHEN USER CLICKS 'ENTER' TO LOGIN
> <!------------------------------------------------------------------------>
> <?php
>
> $submit = ($_POST['submit']);
>
> if ($submit) Â// If user clicks the 'ENTER' button to login
> {
> Â Â// Connect to server and select database
> Â Âinclude ("includes/mysql_connect.inc");
> Â Âinclude ("includes/connect_res_directory.php");
>
> Â Â// define login variables
> Â Â$login = mysql_real_escape_string($_POST['login']);
> Â Â$password = mysql_real_escape_string($_POST['password']);
>
> Â Â$check_login = mysql_query("SELECT * FROM unit_info
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â WHERE login = '$login'
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â AND password = '$password'");
>
> Â Â$data = mysql_fetch_assoc($check_login);
>
> Â Â// Are all the fields filled?
> Â Âif($login && $password)
> Â Â{
> Â Â Â Â// If fields are entered, verify username and password in mysql
> database
> Â Â Â Âif (mysql_num_rows($check_login)) // If the login and password
> exists
> Â Â Â Â{
> Â Â Â Â Â Â//Login
> Â Â Â Â Â * session_start();*
> Â Â Â Â Â Â$_SESSION ['login'] = $data['login'];
>
> Â Â Â Â* Â Âheader ("Location: index_test.php"); // webpage for correct
> login*
>
> Â Â Â Â Â Âexit;
> Â Â Â Â}
> Â Â Â Âelse
> Â Â Â Â{
> Â Â Â Â Â Â// Invalid username/password
> Â Â Â Â Â Âecho "<div class='alert'>The username or password you entered is
> incorrect.</div>";
> Â Â Â Â}
> Â Â}
> Â Âelse
> Â Â Â Âecho "<div class='alert'>Please enter all the fields!</div>";
>
> }
>
> ?>
>

The session cookie must be sent prior to any output. Including, but
not limited to, comments, whitespace, HTML code, etc.

Even a blank line before the first <?php line will be enough to tell
the web server that all the headers have been sent and to start
sending data.

At that stage, the session cookie - which is sent as a header - cannot
be sent as all the headers have been sent.

Options.

1 - Use output buffering. This tells PHP to not output anything until
the end of the script. So echo's, print's, etc. don't send their data
directly. This allows out of order headers and body to be sent and the
server sorts it all out at the end. You can override output_buffering
at runtime, but if headers have been sent due to white space, it won't
help with the session cookie.
2 - Remove all white space. Personally, this is the route I would use.

Richard.
-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux