title: Add URL Parameter to Cookie
body: <p>I am setting up an affiliate link area for my site and need to be able to store a URL query string into a cookie that expires in a 7 days. </p>
  
  <p>Users will click on a link like: <a href="http://examplesite.com?afil=randomvalue" rel="nofollow noreferrer">http://examplesite.com?afil=randomvalue</a>.</p>
  
  <p>I want to pull the key 'afil' and the value 'randomvalue' and put it in a cookie.</p>
  
  <p>This will be used later down the road in another area of the site and I can't keep it in the URL. </p>
  
  <p>I found this post: <a href="https://stackoverflow.com/questions/29804219/set-a-cookie-based-on-url-parameter">Set a Cookie based on url Parameter</a></p>
  
  <p>But it doesn't seem to do what I want or set an expiration date. I am not sure if it is best to use php or javascript for this but I was planing on using php to pull the data later.</p>
  
  <p>UPDATE:</p>
  
  <p>I added this code to my dev site here: <a href="http://inventivewebdesign.com/dev/about?afil=great" rel="nofollow noreferrer">http://inventivewebdesign.com/dev/about?afil=great</a></p>
  
  <pre><code>&lt;?php 
       $value=$_GET['afil']; //the value from the url
       setcookie("afilCookie", $value, strtotime( '+7 days' ));  /* expire in 7 days */
       echo "Cookie = " . $value;
  ?&gt;
  </code></pre>
  
  <p>As you can see, it does display the cookie value (I added it right at the end of my header.php file which puts it in the body). For some reason it doesn't add it to my cookies when I view them.</p>
  
  <p>Also, does this need to be made conditional for if there is no afil value?</p>