Why don't include () allow setcookie to work?

I need to set cookies on my page, but it returns

Warning: Cannot modify header information - headers already sent by (output started at /home1/bsam/public_html/24kadr/index.php:1) in /home1/bsam/public_html/24kadr/basic_login.php on line 35 

in line 1 i have

 include 'basic_login.php'; 

but even if I remoove include, ir returns the same warning on session_start or mysql_connect. so I think that all functions use a header when called, but what can I do? at least I need to connect to the database, and right after that I need to use setcookie (). so what can i do? thanks

update the first lines of index.php

 <?include 'basic_login.php';include 'settings.php';?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> ........... 

in basic_login.php starts

 <? session_start(); include 'connect.php'; ini_set("session.bug_compat_42",1); ini_set("session.bug_compat_warn",0); 

..........................................

 setcookie("cookname", $_SESSION['username'], time()+60*60*24*30, "/"); setcookie("cookpass", $_SESSION['password'], time()+60*60*24*30, "/"); 

+4
source share
3 answers

You most likely have a space in /24kadr/index.php before opening <?php . Remove it and everything should be fine.

If there is no space, your PHP file may have been saved with the Byte Order Mark (BOM). In this case, save the file without it.

+6
source

You get this error if you output anything before setting the header / cookie. Either do this before you print anything, even empty lines, or use output buffering .

+2
source

If you are sure that your /24kadr/index.php file starts with

 <?php 

with more than one character, you may encounter the problem of "UTF-8 with specification"

If your editor is configured to save a file encoded "UTF-8 with BOM", it will add (and will not show) 2 special characters at the beginning of your file.

This may cause PHP to issue a warning.

I hope this helps Jerome WAGNER

0
source

Source: https://habr.com/ru/post/1306348/


All Articles