简体   繁体   中英

PHP. session_start() [function.session-start]: Cannot send session cookie error

Im having problem in my web where it works perfectly on my local server which is a WAMPSERVER i can run my website completely but after adding to the web server it give me error saying

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/multisl/public_html/udrivetest/index.php:2) in /home/multisl/public_html/udrivetest/init.php on line 4

init.php

  <?php

ob_start();
session_start();

   mysql_connect("localhost","***","***");
   mysql_select_db('****');

   include 'function/user.func.php';
   include 'function/car.func.php';
   include 'function/image.func.php';

   ?>

header1.php

<head>
  <title></title>
  <link href="css/style.css" rel="stylesheet" type="text/css">

<style type="text/css">
    <!--
    .style4 {
        font-size: 16px;
        font-weight: bold;
    }
.style5 {
    font-size: 12px
}

.a {
    color:#000000;
    text-decoration:none;
}
-->
</style>

</head>
<body>

 <div style="margin-left:10px;">U Drive.lk</div>

<div class="style5" style="text-align:right; padding:10px;">
        Contact Us | Disclaimer | Site Map</div>
</div>
<div>
    <div style="background-image:url(images/Car3.png); border:#FFFFFF; border-style:solid; border-width:2px; border-top:none; border-bottom:none; width:920px; height:450px;  margin-left:auto; margin-right:auto;" align="center">
        <br/>

        <img src="images/Car6.png" width="920px" height="135px" />
        <div style="margin-top:-22px; height:22px; margin-left:55px;" align="left"><a  class="a">Home</a></div>
        <div style="margin-top:-22px;  height:22px; margin-left:200px;" align="left"><a  class="a">Gallery</a></div>
        <div style="margin-top:-22px;  height:22px; margin-left:350px;" align="left"><a class="a">Leasing</a></div>
        <div style="margin-top:-22px;  height:22px; margin-left:490px;" align="left"><a  class="a">Testimonial</a></div>
        <div style="margin-top:-22px;  height:22px; margin-left:640px;" align="left"><a  class="a">Forum</a></div>
        <div style="margin-top:-22px;  height:22px; margin-left:790px;" align="left"><a class="a">FAQ</a></div>
    <div style="margin-left:auto; margin-right:auto; margin-top:20px;" >
        <script type="text/javascript">
            AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','920','height','135','src','Flash/Flash Header2','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','Flash/Flash Header2' ); //end AC code
        </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="920" height="135">
            <param name="movie" value="Flash/Flash Header2.swf" />
            <param name="quality" value="high" />
            <embed src="Flash/Flash Header2.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="920" height="135"></embed>
        </object></noscript>
    </div>

    <div align="left">
        <table width="100%" border="0" style="margin-top:10px;">
            <tr>
                <td width="20%" valign="top" ><div align="left"><img src="images/Buy_men.png" /> <img src="images/Sell_men.png" /> <img src="images/Rent_men.png" /></div>
                  <div style="color:#FFFFFF;margin-top:-90px; width:38px; margin-left:10px;"><a href="displayVehicle.php" style="color:#FFFFFF;">Buy</a></div>
                    <div style="color:#FFFFFF;margin-top:-18px; width:38px; margin-left:65px;"><a href="submitCar.php" style="color:#FFFFFF;">Sell</a></div>
                    <div style="color:#FFFFFF;margin-top:-20px; width:38px; margin-left:115px;"><a style="color:#FFFFFF;">Rent</a></div></td>
                <td width="38%"><fieldset style="background-color:#cccccc">
                        <span class="style4">Quick Search</span>

                    </fieldset></td>
                <td width="42%"><fieldset style="background-color:#cccccc">
                        <span class="style4">Login</span>
                     <?php include_once('login.php'); ?>
                    </fieldset></td>
            </tr>
        </table>
    </div>

</div>
</div>

<div style="margin-left:auto; margin-right:auto; width:920px;">

login.php

  <?php
    if (logged_in()) {
    echo '<br/> <a href="personal_ViewPage.php"> View User Profile </a> <br/><a   href="logout.php">Log Out</a>';
    } else {
    ?>
    <form action="" method="post">
        <table width="100%" border="0" style="margin-top:-5px;" >
            <tr>
                <td><div align="left">User Name :</div></td>
                <td><input type="text" name="Quick_Name" size="25"  /></td>
            </tr>
            <tr>
                <td><div align="left">Password :</div></td>
                <td> <input type="text" name="PassWord" size="25"  />&nbsp;<input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
    <a href="forgotPassword.php">Forgot your password </a>/ <a href="user_Register.php">Register User</a>
    <?php
}
if (isset($_POST['Quick_Name'], $_POST['PassWord'])) {
    $forgotEmail = $_POST['Quick_Name'];
    $login_password = $_POST['PassWord'];

    if (empty($forgotEmail) || empty($login_password)) {
        $errors[] = 'Email and Password required';
    } else {
        $login = login($forgotEmail, $login_password);

        if ($login === false) {
            $errors[] = 'Unable to Log you in';
        }
    }

    if (!empty($errors)) {
        foreach ($errors as $error) {
            echo $error, '<br/>';
        }
    } else {
        $_SESSION['user_id'] = $login;
        header('Location:index.php');
        exit();
    }
     }
    ?>

index.php

 <?php include 'init.php'; include 'widgets/header1.php'; ?>

 <div>
     </div>
</div>

</body>
</html>

Please Can any one help me out to solve this problem or give me a clue what I am doing here?

Thanks in Advance

This is most likely that there exists some whitespace that is sent before the session_start command, try removing the closing PHP tag (?>) and report back the results. By doing this you remove the possibility of it outputting any invalid whitespace.

In PHP, headers get sent as soon as the page starts to print something - which can occur unintentionally due to whitespace.

Based on your error message (output started at /home/multisl/public_html/udrivetest/index.php:2)

I am guessing this line of index.php:

<?php include 'init.php'; include 'widgets/header1.php'; ?>

appears on line 2 of the file? But you cannot have a line of whitespace above it, it needs to be line 1 since one of the includes is dealing with headers (cookie for session).

Otherwise the blank line 1 is "printing something" - a new line - which sends headers probably before you are intending.

Basically, check all files for not having blank lines above opening tags <?php and don't have blank lines below closing tags ?> .

There should have some blank space sending back to client. It is a good practise to leave php closing tag ?> if your last line of code is a php code.

Make sure that you dont leave any blank space before and after the php opening tag and php closing tag

You can prevent any output to send to client by using object buffering with ob_start() . put the ob_start at the very beginning of your php code can save you time.

I would like to consider removing ob_start() from init.php to index.php.

thanks

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