Session and cookies problem [solved]

Hello,

I have a problem with my site that worked fine until recently: cookies are not saved in any browser I’ve tested, that includes session cookies.

To isolate and identify the problem, I created a php file making sure there is no output before “session_start ();” or “setcookie ();”, but the problem continues (it seems that the server itself was sending some output before running the php file, I can not think of another reason)

The operation of the file can be seen at http://diariobebe.site50.net/probarsesion.php

This is the file code:

<?php
	$bufer = (setcookie('prueba','lista'))?'Cookie establecida correctamente':'No se pudo establecer la cookie';
	session_start();
	$bufer.= '<br/>'.$_COOKIE['prueba'];
	echo 'SID = '.session_id();
	echo '<hr/>$_SESSION = ';
	print_r($_SESSION);
	$_SESSION['validar'] = "OK";
	echo '<hr/>'.$bufer;
	echo '<hr/><a href="">Recargar</a><hr/>';
?>

What is the problem and how can I solve it?

Hi @maurohost!

You had to add php_flag output_buffering on to your .htaccess

I have fixed the problem for you :wink:

That’s great! Thanks.

But I did not understand why I needed to control the output using a buffer, in my code there was no output before starting session or setting cookies; Does the http server add some output automatically before running my script? … Given the solution I suppose yes, but I would like to know which one and why

But I did not understand why I needed to control the output using a buffer, in my code there was no output

session_start(); should be placed in top of the script because it’s using cookies to identify the clients. Therefore using setcookie() before session_start() might interfere.

Does the http server add some output automatically before running my script?

No, it doesn’t :wink:

$bufer = (setcookie(‘prueba’,‘lista’))?‘Cookie establecida correctamente’:‘No se pudo establecer la cookie’;

I don’t know if it only sets the value to $bufer or it also outputs it. I have never tested it in such way.

Je je, I had already tested with session start before set cookie or any other code and the result was the same; until today I had the suspicion that it was a specific problem of cookies and I added set cookie to prove it. That’s why I sent the query.

Either way the buffer solved the problem, thanks again.

You’re more than welcome :blush: