1

Only in my index page session won't start but if I go to another page the session would start or if I go to another page than back to the index page the session would start. The way I have it set up I'm using jQuery AJAX. If it goes through it should create a session than the page refreshes than the session start but it won't start that's the problem.

Top section of the index page:

ob_start;
error_reporting(0);
session_set_cookie_params(3600 * 24 * 7, '/', '.example.com');
session_start();

If I get rid of error_reporting(0) I get these errors

session_start() [function.session-start]: Cannot send session cookie - headers already session_start() [function.session-start]: Cannot send session cache limiter

I don't get those errors in other pages

7
  • yes on all my pages theirs session_start();
    – Juan
    Aug 8, 2013 at 5:45
  • My guess is that you have content over top your PHP, either whitespace, HTML, or a space before your <?php. You can't have anything on top of <?php such as <div>..., or <table>... etc. Check to see if you do. That is why you're getting the error message of "headers already sent". Show your full code. Aug 8, 2013 at 5:55
  • error_reporting(-1); // it will guide you Aug 8, 2013 at 6:01
  • thank you so much it works now. I had that issue for a long time and I never thought white space would effect it. I knew html codes would but not white space. Thank you for your time
    – Juan
    Aug 8, 2013 at 6:04
  • @Juan I will post it as an answer then. Aug 8, 2013 at 6:06

2 Answers 2

1

My guess is that you have content over top your PHP, either whitespace, HTML, or a space before your <?php. You can't have anything on top of <?php such as <div>..., or <table>... etc.

Check to see if you do. That is why you're getting the error message of "headers already sent".

0

Move the session functions above everything else. Also, ob_start is a function and must be invoked.

error_reporting() should always be set to -1 while you are working on the code. When it is uploaded to the server it should be set to 0. It is usually easier to handle it using the php.ini file.

session_set_cookie_params(3600 * 24 * 7, '/', '.example.com');
session_start();
error_reporting(-1);
ob_start();
// Do something
// Call whateter ob_* function you need

I suggest you always read the documentation. There is a lot of help to be had there. Be sure to read the comments as well.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.