--- id: php-cookies title: "PHP Cookies" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["setcookie()", "$_COOKIE", "PHP μΏ ν‚€"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "cookies"] raw_sources: ["https://www.w3schools.com/php/php_cookies.asp"] applied_in: [] github_commit: "" --- # [[PHP Cookies]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `setcookie()` MUST run before any HTML output, including before the `` tag β€” this is a hard requirement (not a style preference) because cookies are transmitted as part of the HTTP header, which must be sent before the response body; this is the same constraint sessions have. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Cookie** β€” a small text file the server stores in the browser, resent with every subsequent HTTP request. [S1] - **`setcookie(name, value, expire, path, domain, secure, httponly)`** β€” only `name` is required; all others optional. [S1] - **Must precede any output** β€” `setcookie()` must be called before `` or any other output, since cookies are part of the HTTP header. [S1] - **`$_COOKIE`** β€” superglobal for reading cookie values. [S1] - **Deleting a cookie** β€” call `setcookie()` again with an expiration time in the PAST. [S1] - **Auto URL-encoding** β€” cookie values are automatically URL-encoded on send and decoded on receipt (use `setrawcookie()` to opt out). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Create + retrieve: `setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); ... if(isset($_COOKIE[$cookie_name])) { echo $_COOKIE[$cookie_name]; }`. [S1] - Modify: re-calling `setcookie()` with a new value overwrites the old one. [S1] - Delete: `setcookie("username", "", time() - 3600); // expiration in the past`. [S1] - Check if cookies enabled: `setcookie("test_cookie", "test", time() + 3600, '/'); ... if(count($_COOKIE) > 0) { echo "Cookies are enabled."; }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **좜λ ₯ μ „ 호좜 ν•„μˆ˜**: setcookie()λŠ” HTTP ν—€λ”μ˜ μΌλΆ€μ΄λ―€λ‘œ λ°˜λ“œμ‹œ νƒœκ·Έλ₯Ό ν¬ν•¨ν•œ μ–΄λ–€ 좜λ ₯보닀도 λ¨Όμ € ν˜ΈμΆœλ˜μ–΄μ•Ό ν•œλ‹€λŠ” 점이 두 번 강쑰됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 30일 만료 μΏ ν‚€λ‘œ μ‚¬μš©μž 이름을 μ €μž₯ν•˜λŠ” 것이 둜그인 μƒνƒœ μœ μ§€μ˜ λŒ€ν‘œ μ‹€μ „ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Creating and checking a cookie (PHP): ```php ?> ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP File Upload]], [[PHP Sessions]], [[PHP Superglobals]] - **μ°Έμ‘° λ§₯락:** ν΄λΌμ΄μ–ΈνŠΈ μΈ‘ 데이터 μ €μž₯ β€” μ„Έμ…˜(Sessions) μ±•ν„°λ‘œ 직접 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Cookies β€” https://www.w3schools.com/php/php_cookies.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Cookies" page (Astra wiki-curation, P-Reinforce v3.1 format).