{"id":475,"date":"2019-09-14T05:01:13","date_gmt":"2019-09-14T05:01:13","guid":{"rendered":"https:\/\/www.webhozz.com\/code\/?p=475"},"modified":"2019-09-14T05:40:15","modified_gmt":"2019-09-14T05:40:15","slug":"php-cookies","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/code\/php-cookies\/","title":{"rendered":"PHP : Cookies"},"content":{"rendered":"\n<p>Cookie adalah file teks kecil yang memungkinkan kalian menyimpan sejumlah kecil data (hampir 4KB) di komputer pengguna. Biasanya digunakan untuk melacak informasi seperti nama pengguna yang dapat diambil situs untuk mempersonalisasi halaman ketika pengguna mengunjungi situs web tersebut di kemudian hari.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Setting Cookie di PHP<\/h5>\n\n\n\n<p>Fungsi <strong><em>setcookie()<\/em><\/strong> digunakan untuk mengatur cookie di PHP. Pastikan kalian memanggil fungsi <strong><em>setcookie()<\/em><\/strong> sebelum output apa pun yang dihasilkan oleh skrip kalian karena jika tidak, cookie tidak akan ditetapkan oleh browser. Berikut sintaks dasar fungsi ini:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsetcookie(name, value, expire, path, domain, secure);\n<\/pre><\/div>\n\n\n<p>Parameter dari fungsi <em><strong>setcookie()<\/strong><\/em> memiliki arti sebagai berikut:<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>Parameter<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>name<\/code><\/td><td>Nama cookie<\/td><\/tr><tr><td><code>value<\/code><\/td><td>Nilai cookie. Jangan menyimpan informasi sensitif karena nilai ini disimpan di komputer pengguna.<\/td><\/tr><tr><td><code>expires<\/code><\/td><td>Tanggal kedaluwarsa dalam format UNIX timestamp. Setelah waktu ini, cookie tidak dapat diakses. Nilai standarnya adalah 0.<\/td><\/tr><tr><td><code>path<\/code><\/td><td>Tentukan path di server tempat cookie akan tersedia. Jika diatur ke \/, cookie akan tersedia di seluruh domain.<\/td><\/tr><tr><td><code>domain<\/code><\/td><td>Tentukan domain untuk tempat cookie mis. Www.example.com.<\/td><\/tr><tr><td><code>secure<\/code><\/td><td>Bagian ini, jika ada, menunjukkan bahwa cookie harus dikirim hanya jika ada koneksi HTTPS yang aman.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Berikut adalah contoh yang menggunakan fungsi <strong><em>setcookie()<\/em><\/strong> untuk membuat cookie bernama <strong><em>username<\/em><\/strong> dan menetapkan nilai <strong><em>John Carter<\/em><\/strong> untuk itu. Ini juga menentukan bahwa cookie akan kedaluwarsa setelah 30 hari <strong><em>(30 hari * 24 jam * 60 menit * 60 detik).<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/\/ Setting a cookie\nsetcookie(&quot;username&quot;, &quot;John Carter&quot;, time()+30*24*60*60);\n?&gt;\n<\/pre><\/div>\n\n\n<p><strong><em>Catatan:<\/em><\/strong> Semua argumen kecuali namanya opsional. Kalian juga dapat mengganti argumen dengan string kosong ( <strong><em>&#8220;&#8221;<\/em><\/strong> ) untuk melewati argumen itu, namun untuk melewatkan argumen kedaluwarsa gunakan nol ( <strong><em>0 <\/em><\/strong>) sebagai gantinya, karena merupakan bilangan bulat.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Mengakses Nilai Cookie<\/h5>\n\n\n\n<p>Variabel superglobal PHP <strong><em>$_COOKIE<\/em><\/strong> digunakan untuk mengambil nilai cookie. Biasanya array asosiatif yang berisi daftar semua nilai cookie yang dikirim oleh browser dalam permintaan saat ini, dikunci dengan nama cookie. Nilai cookie individual dapat diakses menggunakan notasi array standar, misalnya untuk menampilkan username cookie yang ditetapkan dalam contoh sebelumnya, Kalian dapat menggunakan kode berikut.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/\/ Accessing an individual cookie value\necho $_COOKIE&#x5B;&quot;username&quot;];\n?&gt;\n<\/pre><\/div>\n\n\n<p>Kode PHP dalam contoh di atas menghasilkan output berikut.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"787\" height=\"56\" src=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg\" alt=\"\" class=\"wp-image-477\" srcset=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg 787w, https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23-300x21.jpg 300w, https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23-768x55.jpg 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<p>Merupakan praktik yang baik untuk memeriksa apakah cookie disetel atau tidak sebelum mengakses nilainya. Untuk melakukan ini, kalian dapat menggunakan fungsi PHP <strong><em>isset()<\/em><\/strong>, seperti ini:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/\/ Verifying whether a cookie is set or not\nif(isset($_COOKIE&#x5B;&quot;username&quot;])){\n    echo &quot;Hi &quot; . $_COOKIE&#x5B;&quot;username&quot;];\n} else{\n    echo &quot;Welcome Guest!&quot;;\n}\n?&gt;\n<\/pre><\/div>\n\n\n<p>Kalian dapat menggunakan fungsi <strong><em>print_r()<\/em><\/strong> seperti <strong><em>print_r($_ COOKIE);<\/em><\/strong> untuk melihat struktur array asosiatif <strong>$_COOKIE<\/strong> ini, seperti array lainnya.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Menghapus Cookies<\/h5>\n\n\n\n<p>Kalian dapat menghapus cookie dengan memanggil fungsi <strong><em>setcookie()<\/em><\/strong> yang sama dengan nama cookie dan nilai apa pun (seperti string kosong) namun kali ini kalian perlu menetapkan tanggal kedaluwarsa di masa lalu, seperti yang ditunjukkan dalam contoh di bawah ini:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/\/ Deleting a cookie\nsetcookie(&quot;username&quot;, &quot;&quot;, time()-3600);\n?&gt;\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cookie adalah file teks kecil yang memungkinkan kalian menyimpan sejumlah kecil data (hampir 4KB) di komputer pengguna. Biasanya digunakan untuk melacak informasi seperti nama pengguna<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-475","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Belajar PHP : Cookies - WebHozz Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webhozz.com\/code\/php-cookies\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Belajar PHP : Cookies - WebHozz Code\" \/>\n<meta property=\"og:description\" content=\"Cookie adalah file teks kecil yang memungkinkan kalian menyimpan sejumlah kecil data (hampir 4KB) di komputer pengguna. Biasanya digunakan untuk melacak informasi seperti nama pengguna\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/code\/php-cookies\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Code\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-14T05:01:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-14T05:40:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"787\" \/>\n\t<meta property=\"og:image:height\" content=\"56\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"headline\":\"PHP : Cookies\",\"datePublished\":\"2019-09-14T05:01:13+00:00\",\"dateModified\":\"2019-09-14T05:40:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/\"},\"wordCount\":381,\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-23.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/\",\"name\":\"Belajar PHP : Cookies - WebHozz Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-23.jpg\",\"datePublished\":\"2019-09-14T05:01:13+00:00\",\"dateModified\":\"2019-09-14T05:40:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-23.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-23.jpg\",\"width\":787,\"height\":56},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-cookies\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP : Cookies\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\",\"name\":\"WebHozz Code\",\"description\":\"Tutorial Web &amp; Pemrograman Indonesia\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/author\\\/dody\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Belajar PHP : Cookies - WebHozz Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webhozz.com\/code\/php-cookies\/","og_locale":"en_US","og_type":"article","og_title":"Belajar PHP : Cookies - WebHozz Code","og_description":"Cookie adalah file teks kecil yang memungkinkan kalian menyimpan sejumlah kecil data (hampir 4KB) di komputer pengguna. Biasanya digunakan untuk melacak informasi seperti nama pengguna","og_url":"https:\/\/www.webhozz.com\/code\/php-cookies\/","og_site_name":"WebHozz Code","article_published_time":"2019-09-14T05:01:13+00:00","article_modified_time":"2019-09-14T05:40:15+00:00","og_image":[{"width":787,"height":56,"url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/"},"author":{"name":"admin","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"headline":"PHP : Cookies","datePublished":"2019-09-14T05:01:13+00:00","dateModified":"2019-09-14T05:40:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/"},"wordCount":381,"image":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg","articleSection":["PHP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/","url":"https:\/\/www.webhozz.com\/code\/php-cookies\/","name":"Belajar PHP : Cookies - WebHozz Code","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#primaryimage"},"image":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg","datePublished":"2019-09-14T05:01:13+00:00","dateModified":"2019-09-14T05:40:15+00:00","author":{"@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"breadcrumb":{"@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/code\/php-cookies\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#primaryimage","url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg","contentUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-23.jpg","width":787,"height":56},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/code\/php-cookies\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/code\/"},{"@type":"ListItem","position":2,"name":"PHP : Cookies"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/code\/#website","url":"https:\/\/www.webhozz.com\/code\/","name":"WebHozz Code","description":"Tutorial Web &amp; Pemrograman Indonesia","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.webhozz.com\/code\/author\/dody\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":4,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":481,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/475\/revisions\/481"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}