Mình có thắc mắc là tại sao mình đặt setcookie dưới thẻ html head nhưn vẩn không lổi?
Theo như document thì nó bảo
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
http://php.net/manual/en/function.setcookie.php
Ví dụ như:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test cookie</title>
<link rel="stylesheet" href="">
</head>
<body>
<?php //ở đây này
echo 'Cookie test<br>';
if(isset($_COOKIE['language'])){
echo 'is seted<br>';
}
if(setcookie('language','vi'))
{
echo "true";
}
?>
</body>
</html>
Thì không báo lổi? Tại sao?
(Tiếng anh mình hơi kém… Với lại mình chả biết làm sao để nó lênh màu code lung linh, thành thật xin lỗi).
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?