{"id":5391,"date":"2020-01-31T15:31:02","date_gmt":"2020-01-31T10:01:02","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5391"},"modified":"2020-02-04T10:47:01","modified_gmt":"2020-02-04T05:17:01","slug":"how-to-setup-cookie-in-php","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-setup-cookie-in-php\/","title":{"rendered":"HOW TO SETUP COOKIE IN PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">HOW TO SETUP COOKIE IN PHP<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Date : 31\/01\/2019<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-31-at-16.14.36.png\" alt=\"\" class=\"wp-image-5397\" width=\"587\" height=\"194\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-31-at-16.14.36.png 476w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-31-at-16.14.36-300x99.png 300w\" sizes=\"(max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">     A cookie is a small file, that stores in the user computer, used to keep track of information, such as username or else, the site can retrieve when the user visits the website next time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Setting up cookie using PHP<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>setcookie()<\/strong> method is used to set up the cookie in PHP. we should call setcookie() method top of the script, before adding any functionality in the script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax<\/strong>:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>setcookie(name,value,expire,path,domain,secure)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">name =&gt; Name of the cookie<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">value =&gt; value of the cookie, <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">expire =&gt; specifies when the cookie is expired. after the mentioned time the cookie will inaccessible, the default value 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path =&gt;  specifies the path of the server. if we set &#8220;\/&#8221; the cookie is available in the entire domain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">domain =&gt; specifies the domain of the cookie(eg. www.mydomainname.com)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">secure =&gt; It will represent the cookie will be transmitted over secure https protocols. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$cookie_name = \"user\";\n$cookie_value = \"michel\";\nsetcookie($cookie_name, $cookie_value, time() + (86400 * 30), \"\/\"); \n?><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verify cookie<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">$_COOKIE  variable is used to retrieve the cookie.  <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$cookie_name = \"user\";\nif(!isset($_COOKIE[$cookie_name])) {\n    echo \"Cookie not exists\";\n} else {\n    echo \"Cookie exists\";\n    \n}\n?><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you for using pheonixsolutions. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you like it, share it with your friends.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HOW TO SETUP COOKIE IN PHP Date : 31\/01\/2019 Introduction A cookie is a small file, that stores in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[748,273,749],"class_list":["post-5391","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-cookie","tag-php","tag-setcookie","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1oX","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5391"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5391\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}