phpjs
Version:
81 lines (68 loc) • 2.52 kB
Markdown
layout: page
title: "JavaScript setrawcookie function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/setrawcookie:510
- /functions/view/setrawcookie
- /functions/view/510
- /functions/setrawcookie:510
- /functions/510
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's setrawcookie
{% codeblock network/setrawcookie.js lang:js https://raw.github.com/kvz/phpjs/master/functions/network/setrawcookie.js raw on github %}
function setrawcookie (name, value, expires, path, domain, secure) {
// From: http://phpjs.org/functions
// + original by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + derived from: setcookie
// + input by: Michael
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: setrawcookie('author_name', 'Kevin van Zonneveld');
// * returns 1: true
if (typeof expires === 'string' && (/^\d+$/).test(expires)) {
expires = parseInt(expires, 10);
}
if (expires instanceof Date) {
expires = expires.toGMTString();
} else if (typeof expires === 'number') {
expires = (new Date(expires * 1e3)).toGMTString();
}
var r = [name + '=' + value],
s = {},
i = '';
s = {
expires: expires,
path: path,
domain: domain
};
for (i in s) {
if (s.hasOwnProperty(i)) { // Exclude items on Object.prototype
s[i] && r.push(i + '=' + s[i]);
}
}
return secure && r.push('secure'), this.window.document.cookie = r.join(";"), true;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/network/setrawcookie.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/setrawcookie.js)
### Example 1
This code
{% codeblock lang:js example %}
setrawcookie('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 %}