<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cookies Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<atom:link href="https://atcodex.com/tag/cookies/feed/" rel="self" type="application/rss+xml" />
	<link>https://atcodex.com/tag/cookies/</link>
	<description>Achieve financial success without sacrificing well-being! AtCodex provides actionable strategies for managing money, time, and personal growth—all in one place.</description>
	<lastBuildDate>Sat, 28 Aug 2021 17:15:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>

<image>
	<url>https://atcodex.com/wp-content/uploads/2020/05/cropped-New-Project-1-32x32.png</url>
	<title>Cookies Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<link>https://atcodex.com/tag/cookies/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Create, Access and Delete Cookies in PHP</title>
		<link>https://atcodex.com/how-to/how-to-create-access-and-delete-cookies-in-php/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Tue, 14 Jul 2020 15:22:29 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Cookies]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=374</guid>

					<description><![CDATA[<p>Cookies are small text files that are created by the server and stored on the user computer in around 4KB files. Cookies are used to store some information when user &#8230; </p>
<p>The post <a href="https://atcodex.com/how-to/how-to-create-access-and-delete-cookies-in-php/">How to Create, Access and Delete Cookies in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Cookies are small text files that are created by the server and stored on the user computer in around 4KB files.  Cookies are used to store some information when user visit the website we set the cookies and next time when the user comes to the website we can retrieve the value of the cookie and manipulate the information or data for that particular user as per requirement, so cookies are very helpful in the website and we can use it in multiple scenarios. in this tutorial, we will let you know <strong>How to use cookies in PHP</strong> and<strong> How to Set Cookies, Get Cookies, Delete Cookies with PHP</strong>.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Uses of cookies</h3>



<p class="wp-block-paragraph">Cookies can be used for different purpose:</p>



<ul class="wp-block-list"><li>Store data like (font size, color,  element properties, theme, or anything about a particular user, etc.)</li><li>Track something like (page count, percentage, behavior, etc.)</li><li>Store username or numbers (to validate the user)</li></ul>



<h3 class="wp-block-heading">Set Cookie</h3>



<p class="wp-block-paragraph">We can create a cookie using PHP&nbsp;<em><strong>setcookie</strong>()</em>. This function needs mainly 3 parameters to create a cookie, name, value, and expiration time.</p>



<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="">$cookie_val = 'atcodex'; //variable 
setcookie("websiteName", $cookie_val, time()+3600); //cookie name, value &amp; time (1 hour)</pre>



<p class="wp-block-paragraph"><strong>Note:&nbsp;The setcookie() function must appear before the &lt;html&gt; tag.</strong></p>



<p class="wp-block-paragraph">If you run the code, it should create a cookie named &#8220;websiteName&#8221; with its value &#8220;atcodex&#8221; on the user computer,  cookies expire time has set 1 hour so after an hour it will automatically be expired. In the same way we can set multiple cookies according to our requirements in website. </p>



<p class="wp-block-paragraph">You can set more advanced parameters, which include path, domain, secure, httponly. setcookie(NAME, VALUE, EXPIRE, PATH, DOMAIN, SECURE, HTTPONLY)</p>



<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="">
$cookie_val = 'atcodex'; //variable 
setcookie("websiteName", $cookie_val, time()+3600, '/', 'atcodex.com', true, true);
</pre>



<p class="wp-block-paragraph">We have set  &#8216;/&#8217; in the path which means that cookies will be available in the entire domain. You can set cookies in &#8216;/folderName&#8217; and then the cookie will available only in that folder and its sub-directories.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Retrieve Cookie Value</h3>



<p class="wp-block-paragraph">After setting the cookie value we can easily retrieve the cookie&#8217;s value on any page within the website using &nbsp;$_COOKIE . The below code will show the cookie&#8217;s value in the browser.</p>



<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="">
echo 'Your Website Name Is ' . $_COOKIE['websiteName'];
</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Once cookies stored in the browser, it remain in the borswer  till the expiration date or user deliberatly clear it in broswer setting. But if you want to delete cookies you do with just simple code shown in belwo example.</p>



<h3 class="wp-block-heading">Delete a Cookie</h3>



<p class="wp-block-paragraph">To delete the stored cookie, just set the expiration date to negative.</p>



<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="">setcookie("websiteName", "", time()-3600); //expiration time set to one hour ago</pre>



<p class="wp-block-paragraph">When you run this code, it will set the cookie to before time which triggers the browser cookie removal mechanism, which instantly removes cookie from the browser.</p>



<h2 class="wp-block-heading">Updating Cookie in PHP</h2>



<p class="wp-block-paragraph">To update/modify a cookie value, simply just set its value again. like if we want to update the &#8216;<strong>websiteName</strong>&#8216;, just change the name parameter using <strong><code>setcookie()</code>&nbsp;</strong>method again.</p>



<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="">$cookie_val = 'atcodex2'; //variable 
setcookie("websiteName", $cookie_val, time()+3600); //cookie name, value &amp; time (1 hour)</pre>



<h2 class="wp-block-heading">Delete all cookies php</h2>



<p class="wp-block-paragraph">if we want to delete all the cookies at one go then just run the below code,&nbsp;it will unset all of the cookies on your domain</p>



<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="">// unset cookies all at one go
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-3600);
        setcookie($name, '', time()-3600, '/');
    }
}</pre>



<h2 class="wp-block-heading"><strong>How to store Array and JSON in Cookie</strong></h2>



<p class="wp-block-paragraph">Sometimes we need to store array in a cookie,  for that, we need to define all the value in the array and just use json_encode to encode that array in JSON  and store in a cookie. </p>



<p class="wp-block-paragraph">Store JSON value in <strong>Cookie</strong></p>



<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="">$arrayData=array(
    'beginner',
    'Inertmediate',
    'Expert'    
);
setcookie('cookieData', json_encode($arrayData));
</pre>



<p class="wp-block-paragraph">Store array value in <strong>Cookie</strong></p>



<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="">setcookie ("atcodex[name]", $name);
setcookie ("atcodex[city]", $city);
setcookie ("atcodex[state]", $state);
setcookie ("atcodex[zip]", $zip);</pre>



<h2 class="wp-block-heading"><strong>How to Retrieve <strong>Array and JSON</strong></strong> <strong>from Cookie</strong></h2>



<p class="wp-block-paragraph">Get value of stored JSON</p>



<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="">$arrayData = json_decode($_COOKIE['cookieData']);</pre>



<p class="wp-block-paragraph">Get value of  stored  array </p>



<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="">$_COOKIE['atcodex']['name']
$_COOKIE['atcodex']['city']
$_COOKIE['atcodex']['state']
$_COOKIE['atcodex']['zip']</pre>
<p>The post <a href="https://atcodex.com/how-to/how-to-create-access-and-delete-cookies-in-php/">How to Create, Access and Delete Cookies in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
