Pregunta

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

¿Fue útil?

Solución

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".

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top