Cookies in PHP, how?

There is a problem, beaten up holes: no stored cookies. But it is very strange behavior.


In General, the first version of the engine retained some data to a file on the server, and the user gave cookies. While the HTML code was to code in PHP and set cookies succeeded:


...
<?php


...

// here is some code

setcookie ("auth", $auth);</code-->
br><br><br> BUT when I began to translate your "engine" in MySQL database, cookies stopped asking, and instead you receive this warning:<br><pre><code>PHP Warning: Cannot modify header information - headers already sent by (output started at /file path:12) in /path to the same file on line 275</code></pre><br><br><br> Line 12 — start of code (after the HTML, there is <?php), and line 275 function setcookie().<br--><br><br> The code itself has changed a bit, there was added a line:<br><pre><code class="php"> ... <?php include("./database.php"); // file is CLEAN, there is NO output at all, only the functions for the database at the beginning of the BOM, too, NO ... setcookie ("auth"); ?></code></pre><br><br><br> What could be the problem? Already the whole head broke on the table and the laptop, but I can't understand why when you save in files all rolled, and DB is not. br><br> Googled, followed all the advice (no BOM, the file database.php actually, output does not contain). br><br> Please, harrowdene help!
October 8th 19 at 02:00
4 answers
October 8th 19 at 02:02
Solution
Due to buffering, the data is not sent prior to the installation of cookies. And now more data PHP sends them before the setcookie.

Who does not believe that this is possible, execute the script:
Hello world!

<?php setcookie('test', 'test') ?>

I have this code working, the cookies are set (no notice).

Now this option:
<?php for ($i = 1; $i < 500; ++$i) echo 'Hello world!
'; ?>

<?php setcookie('test', 'test') ?>


Get:
Warning: Cannot modify header information - headers already sent


In any case, all headers must be sent before the data, and hope for the standard buffer is impossible. Use ob_start.
In the second version of the code (with loop) parser ate the br tag. - Tyshawn11 commented on October 8th 19 at 02:05
Hmm, but in General it is good to minimize a simple engine to one file? - orlo30 commented on October 8th 19 at 02:08
Hmm, but in General it is good to minimize a simple engine to one file?

Is it good? No :) But if the code is really very little, then why not. MVC fence is not necessary, but at least separate the html from php. All code must be executed to output the first byte. The most recent stage run passing variables to the template. - Tyshawn11 commented on October 8th 19 at 02:11
Hmm... so all the text variables to do... Thanks for the tip! We will see that as a Yes. - orlo30 commented on October 8th 19 at 02:14
At least
<?php
...
$name = 'World';
...
setcookie('key', 'value');
?>


Hello <?php echo $name ?>!
- Tyshawn11 commented on October 8th 19 at 02:17
October 8th 19 at 02:04
1) Learn the basics of the HTTP Protocol, in particular what response headers
2) Read about output buffering

The idea is that generally neither the first nor the second circuit should not work. Cookies should be placed prior to any output. Nothing to do with MySQL.
I know and understand how HTTP works and what the headers of the request and response.
But WHY the first scheme working properly? - Tyshawn11 commented on October 8th 19 at 02:07
Because you could be turned on output buffering in PHP, for example. Or warning displayed, but not visible on the page. - orlo30 commented on October 8th 19 at 02:10
October 8th 19 at 02:06
So you connect database.php after pegging begins. She appears before installing cookies.
Tried to, right in the beginning of the file. - Tyshawn11 commented on October 8th 19 at 02:09
Maybe a space or \n is outside the php tag is? - orlo30 commented on October 8th 19 at 02:12
October 8th 19 at 02:08
Cookies are passed in the headers is the HTTP standard. If you have started to the conclusion that the headers have already gone, and again they(headers) are not sent. Here's your problem. Start the output after the PHP operation.

Find more questions by tags PHP