There is a ready-made example.

$db = mysql_connect ("localhost","login","password"); mysql_select_db ("info",$db); mysql_query('SET NAMES utf8');; $uid = $_GET['uid']; $name=$_GET['uid']; if ($_REQUEST['hash']==md5('3965622'.$uid.'sdpfkjpowjif09w8ejf')) { //доверяем вконтактику, и далее полагаем, что пользователь действительно авторизован там //для учетных записей пользователей я решил выделить логины вида vk-******** $result = mysql_query("SELECT id, random, password FROM tracker_users WHERE username = 'vk-$uid'"); setcookie('uid',''); setcookie('pass',''); if (mysql_num_rows($result)) { //пользователь авторизован, просто пересоздадим куки $user = mysql_fetch_assoc($result); mysql_query("UPDATE tracker_users SET name = '$name' WHERE username = 'vk-$uid' LIMIT 1"); setcookie('pass',md5($user['random'].$user['password'].$user['random'])); setcookie('uid',$user['id']); } else { //добавим запись в таблицу пользователей $random = mt_rand(100000,999999); $pwd = $uid . 'sdpfkjpowjif09w8ejf'; $pid=md5(uniqid(rand(),true)); mysql_query("INSERT INTO tracker_users (username, name, password, random, id_level, email, style, language, flag, joined, lastconnect, pid, time_offset) VALUES ('vk-$uid', '$name', '" . md5($pwd) . "', $random, 3, '', 5, 7, 0, NOW(), NOW(),'$pid', '0')"); //вставили строчку, теперь создадим куки и перебросим на другую страницу setcookie('pass',md5($random.md5($pwd).$random)); setcookie('uid',mysql_insert_id()); } header("Location: /index.php"); } 

There are such errors?

 Warning: Cannot modify header information - headers already sent by (output started in on line 30, 31, 33` 
  • What line error appears? Line number - ReinRaus
  • What do you have in the db.php file? Is there any chance that there is a message that the connection to the database has been established? - mountpoint
  • In lines 12, 13, 29, 30, 32 - Radik Kamalov
  • one
    1. Resave this file and db.php without BOM. 2. If in db.php there is output to the browser (echo, vardump), then put the data in a variable, and after setting all the cookies make an output to this variable. - ReinRaus
  • one
    Understood. The problem was that an error popped up. And the conclusion of the error is also a conclusion. - Radik Kamalov

2 answers 2

Not at the beginning of the document on the bill - the wrong statement. setcookie actually modifies the document title. accordingly setcookie cannot be executed when all the headers have already been sent and the "body of the document" has begun. Those. any web document consists of a set of headers (at the beginning of the document, so they are called so) and after they are output - the body begins

Example number 1:

 <?php setcookie('uid',1231); echo '1'; ?> 

This code will execute successfully.

Example number 2:

 <?php echo '1'; setcookie('uid',1231); ?> 

In this case, we get "headers already sent", because “1” has already been sent to the body of the document, which means that we can no longer change / add any headers in the document, which php is swearing at.

In your case, something initiates a conclusion (could it be a banal space before the opening <? Php or after the closing?> Or some function wants to say something about the error).

It is possible to make the sequence in Example 2 also not give an error — for example, through output buffering, but this is a separate topic.

  • one
    Even it became interesting, what prevents me from setting cookies without executing session_start ? ) Mixed everything ... - Indifferent
  • one
    Yes, it is generally from another opera, progluchilo. - Vlad R

setcookie() can be used strictly before the first output to the page, be it HTML markup or PHP output. You can place any PHP code before installing the cookies, but with the condition that it will not display anything on the page.