• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
session nightmare

#1
[eluser]rubber.drummer[/eluser]
hello all, i have this big problem with session and cookies both CI's own and PHP native I have tried everything in the forums but now is more than a week since i cannot get this to work, the strangest thing is that this problem only occurs on the server where the deployed app should be, because in local machines it works great we have tried in 2 different hosting with CI. Let me explain i have made a simple controller/view with the use of a native session, the controller's code is:
Code:
<?php
class Sessiontest extends Controller{
    
    function Sessiontest(){
        parent::Controller();
    }
    
    function index(){
        $this -> load -> view('sessiontest');
    }
}
?>
and the view is
Code:
<?php
     session_start();
    if(isset($_SESSION['views'])){
        $_SESSION['views'] = $_SESSION['views']+1;
        echo "these value comes from a session and it increments in every page load: <b>". $_SESSION['views'] . "</b>";
    }    
    else {  // set the session if it does not exist
        $_SESSION['views']=1;
        echo "these value comes from a session and it increments in every page load: <b>". $_SESSION['views'] . "</b>";
    }
?&gt;
the error i get is:
Code:
A PHP Error was encountered
Severity: Warning

Message: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /homepages/23/d236258025/htdocs/index.php:1)

Filename: views/sessiontest.php

Line Number: 2
Now if i run this code outside the CI folder that is only the view file, it works great.
Now lets use CI's own session, I commented all the code lines in the view and leave it blank and in the controller i only add this line of code
Code:
$this->load->library('session');
i get this error:
Code:
A PHP Error was encountered
Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /homepages/23/d236258025/htdocs/index.php:1)

Filename: libraries/Session.php

Line Number: 295
I don't know why this happens since I am only loading the library, I'm not even in the part where i create a session?
I must mention that I already looked for blank spaces before/after the php tags as in other cases users said and i get the same problem with cookies. Please any help would be very appreciated. Thank you, you can take a look at the error in here and here witch are in 2 different hosting providers.

#2
[eluser]webthink[/eluser]
Loading the library does create the session. So effectively your trying to send the header twice. You should decide if you want to use a session handler or not and stick to one methodology.

#3
[eluser]rubber.drummer[/eluser]
Thank you for the response, I am sure that I am only using one methodology at a time, since in my local machine works fine both approaches, if I use CI's session it crashes just by importing the library $this->load->library('session'); in the controller. I'm also sure I don't have it load in the autoload array in config.

#4
[eluser]gtech[/eluser]
I know you get a headers already sent error when you have white spaces outside &lt;?php ?&gt; tags and when you try to echo content before sending headers.

The session is set by sending header content to the browser.

White spaces will get sent to the browser if out side the php tags which will result in html headers being already set.

echo and print_r will also have the same effect.

#5
[eluser]Rick Jolly[/eluser]
Headers, including session cookies, must be sent to the browser before any other output. Check for any output, including echo/print statements, before the session library is loaded.

Edit: gtech beat me.

#6
[eluser]rubber.drummer[/eluser]
Hello all, and thank you for your replies.. I must tell you that I've solved the problem by installing a fresh copy of CI in the server and then playing around with the "welcome" controller/view that comes by default I added some CI's session's and it worked, don't know what was wrong before but it worked fine and then I uploaded my app's files one at a time to see if I get any error but i didn't.

#7
[eluser]datasponge[/eluser]
I was experiencing the same issue in regard to the 'Message: session_start()' error.

The fix for me was to remove the white spaces at the end of the controller file after the '?&gt;' tag. This was a valuable lesson learned, and very annoying to solve. Thank you to those who posted above on this fix.

#8
[eluser]einstein[/eluser]
The session should be started befor any code, if not you get an header error like in your case.
Remember is called MVC (Model View Controller) but your controller is loaded befor your view, so you allready have some code in form of variables in your page (controller) and when you try session_start from your view you get the headers error.

Anyway i am not 100% sure about this but i could say 95%.

Thank you


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.