Introduction to...LOGIN

Introduction to cookies

Cookies are data stored in the client's browser. We use cookies to track and store user data. Typically, cookies are returned from the server to the client via HTTP headers. Most web programs support the operation of cookies. Because cookies exist in the HTTP header, they must be set before other information is output, similar to the usage restrictions of the header function.

PHP sets Cookie through the setcookie function. Any Cookie sent back from the browser will be automatically stored in the global variable of $_COOKIE by PHP, so we can use $_COOKIE['key' ] to read a cookie value.

Cookies in PHP are very widely used and are often used to store users' login information, shopping carts, etc. When using a Session, Cookies are usually used to store session IDs to identify users. Cookies have a validity period. When the validity period expires, the cookie will be automatically deleted from the client. At the same time, for security control, Cookie can also set domain and path. We will explain them in detail in later chapters.


Next Section
<?php setcookie('test', time()); ob_start(); print_r($_COOKIE); $content = ob_get_contents(); $content = str_replace(" ", ' ', $content); ob_clean(); header("content-type:text/html; charset=utf-8"); echo '当前的Cookie为:<br>'; echo nl2br($content); ?>
submitReset Code
ChapterCourseware