简体   繁体   中英

Warning: session_start() not working

Seems like I am having a problem with session_start on the code below:

Error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /usr/home/web.com/web/index.php:1) in /usr/home/web.com/web/index.php on line 10

Code:

<?php
if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) )
{
header( 'Location: http://www.web.com/ie/index.php' ) ;
} 
?>
<?php
session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
$success="<p>Gracias ".htmlspecialchars($_SESSION['name'])."! Estaremos en contacto en menos de 24h!</p>";
$css='<style type="text/css">.demo-form{display:none;}.thanks{display:block;}</style>';
unset($_SESSION['sent']);
}
?>
<?=isset($css)?$css:""?>

Working on another inner page, where uses the "same" code, seems like the error is not displaying (code below):

<?php
session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
$success="<p>Gracias ".htmlspecialchars($_SESSION['name'])."! We'll be in touch asap!</p>";
$css='<style type="text/css">.demo-form{display:none;}.thanks{display:block;}</style>';

unset($_SESSION['sent']);
}
?>
<?=isset($css)?$css:""?>

So as you can see on the second example is just the same code without this part, but deleting it on the first example does not solve the problem:

   <?php
    if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) )
    {
    header( 'Location: http://www.web.com/ie/index.php' ) ;
    } 
    ?>

When submiting the form, it redirects using header('Location: index.php#5'); but I must let this part as it is.

Where could be the problem? Maybe I am missing something?

UPDATED CODE AND ANSWER (still not working)

CODE:

<?php session_start(); if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) ) { header( 'Location: http://www.web.com/ie/index.php' ) ; } session_name("fancyform"); ?>
<?php
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
$success="<p>Gracias ".htmlspecialchars($_SESSION['name'])."! Estaremos en contacto en menos de 24h!</p>";
$css='<style type="text/css">.demo-form{display:none;}.thanks{display:block;}</style>';
unset($_SESSION['sent']);
}
?>
<?=isset($css)?$css:""?>

Error:

**<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /usr/home/web.com/web/index.php:1) in <b>/usr/home/web.com/web/index.php</b> on line <b>1</b><br />
<br />
<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /usr/home/web.com/web/index.php:1) in <b>/usr/home/web.com/web/index.php</b> on line <b>1</b><br />**

LAST EDIT (Solved)

I just took an old backup and seems like it is working now. I dont know what was exactly the point to make it work now, but anyway it could help someone:

CODE:

**<?php

if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) )
{
header( 'Location: http://www.web.com/ie/index.php' ) ;
} 
?>
<?php
session_name("fancyform");
session_start();


$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];


if(isset($_SESSION['sent']))
{
$success="<p>Gracias ".htmlspecialchars($_SESSION['name'])."! We'll be in touch asap!</p>";
$css='<style type="text/css">.demo-form{display:none;}.thanks{display:block;}</style>';
unset($_SESSION['sent']);
}
?>
<?=isset($css)?$css:""?>**

When session_start() is executed it sends several headers as documentation says - http://de3.php.net/function.session-start

Therefore it has to be executed before your PHP script produces any output and of course it can't be used together with header('Location: index.php#5'); because redirection forbids any other headers (including session related headers like Cookies). I disagree with other answer saying that it has to be the first line of the script - here is an example from documentation which negates these statements http://de3.php.net/manual/en/function.session-cache-limiter.php

Remember that even space or linefeed before <?php produces output disallowing headers, so I believe the problem is in:

?>
<?php

The linefeed between these tags produces output. Closing php code just to reopen it in next line makes no sense anyway. Make sure that there is nothing before <?php and don't close it with ?> before you execute start_session() under any conditions.

You should conditionally execute either session_start() or header('Location: index.php#5'); and not both at the same time.

If you are still experiencing problems, you can turn on all warnings in php.ini and you should see some useful info about the real reason for the problem. http://de3.php.net/manual/en/configuration.file.php Remember that any output from the script forbids sending any headers afterwards.

If that won't reveal anything interesting, you should manually send request to your website with telnet for example and then you will see everything including offending headers. There are also extensions for Firefox to inspect headers and server response details but I prefer telnet .

After @Phil Perry's comment I have to add this information here. Even if you don't see the leading characters in your editor, saving a file in UTF-8 might produce BOM. Please check configuration of your editor.

Put this at the start

<?php
   ob_start();
   session_start();


This is how it should look -

<?php
ob_start();
session_start();

if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) )
{
   header( 'Location: http://www.web.com/ie/index.php' ) ;
} 

$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
   ...

Explanation:

For access the session variables within your script on any page, you have to start your session above it session_start() .

Also session_name() is deprecated in recent PHP versions. Use $_SESSION to GET or SET your session variables.

"Headers already sent" means that your PHP script already sent the HTTP headers, and as such it can't make modifications to them now.

Check that you don't send ANY content before calling session_start. Better yet, just make session_start the first thing you do in your PHP file (so put it at the absolute beginning, before all HTML etc).

You must have to use session_start at the begining of the start.

session_name("fancyform");
session_start();

Replace these two lines.

session_start();

//And then write starting comment

if ( preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT']) )
{
   header( 'Location: http://www.web.com/ie/index.php' ) ;
} 

    session_name("fancyform");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM