Use PHP functions in JavaScript

JavaScript setcookie

Send a cookie

1
2
3
4
56
7
8
9
1011
12
13
14
function setcookie (name, value, expires, path, domain, secure) {
    // Send a cookie  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/setcookie    // +   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);
}
external links: original PHP docs | raw js source

Examples

Running

1
setcookie('author_name', 'Kevin van Zonneveld');

Should return

1
true

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have setcookie goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
8 Jan '09 Permalink

q  @ Brett Zamir: With so many lines shared, I've made setcookie depend on setrawcookie.

Gravatar
Brett Zamir
8 Jan '09 Permalink

q  Here's setrawcookie() (just removed encodeURIComponent())...

[CODE="Javascript"]
setrawcookie('author_name', 'Kevin van Zonneveld');
alert(document.cookie); // author_name=Kevin van Zonneveld
setcookie('author_name', 'Kevin van Zonneveld');
alert(document.cookie); // author_name=Kevin%20van%20Zonneveld

function setrawcookie(name, value, expires, path, domain, secure) {
// http://kevin.vanzonneveld.net
// * example 1: setrawcookie('author_name', 'Kevin van Zonneveld');
// * returns 1: true
expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
var r = [name + "=" + value], s, i;
for(i in s = {expires: expires, path: path, domain: domain}){
s[i] && r.push(i + "=" + s[i]);
}
return secure && r.push("secure"), document.cookie = r.join(";"), true;
}[/CODE]

Gravatar
Kevin van Zonneveld
9 Nov '08 Permalink

q  @ Onno Marsman: Fixed

Gravatar
Onno Marsman
7 Nov '08 Permalink

q  The encodeURI function does not encode characters like & and = . I think encodeURIComponent should be used instead.

Gravatar
Kevin van Zonneveld
3 Nov '08 Permalink

q  @ Andreas: Thank you, fixed!

Gravatar
Andreas
28 Oct '08 Permalink

q  Usage of escape() is out-dated (isn't it?) and got some other problems (e.g. some UTF8 stuff). I think encodeURI() would be a better solution here.

However, thx for this project!

Gravatar
Kevin van Zonneveld
17 Jul '08 Permalink

q  @ space_marine: I haven't tried that with Jonas' code so far. Can you share your findings?

Gravatar
space_marine
23 Jun '08 Permalink

q  Can this function set array cookies?

[CODE="php"]
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo "$name : $value <br />\n";
}
}
?>
[/CODE]

Output:

three : cookiethree
two : cookietwo
one : cookieone


Contribute a New function

More functions

In this category

inet_ntop
inet_pton
ip2long
long2ip
» setcookie
setrawcookie

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !