---
layout: page
title: "JavaScript setcookie function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/setcookie:509
- /functions/view/setcookie
- /functions/view/509
- /functions/setcookie:509
- /functions/509
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's setcookie

{% codeblock network/setcookie.js lang:js https://raw.github.com/kvz/phpjs/master/functions/network/setcookie.js raw on github %}
function setcookie (name, value, expires, path, domain, secure) {
  // From: http://phpjs.org/functions
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // +   bugfixed by: Andreas
  // +   bugfixed by: Onno Marsman
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // -    depends on: setrawcookie
  // *     example 1: setcookie('author_name', 'Kevin van Zonneveld');
  // *     returns 1: true
  return this.setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure);
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/network/setcookie.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/network/setcookie.js)

### Example 1
This code
{% codeblock lang:js example %}
setcookie('author_name', 'Kevin van Zonneveld');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}


### Other PHP functions in the network extension
{% render_partial _includes/custom/network.html %}
