Here's the code how dimitar christoff generously posted. What I need to know is actually how to use it. Putting it in <a href="http://en.site.com/" title="English"><strong>English</strong> - continue</a> var Cookie = { // wrapper for working with cookies. set: function(c_name, value, options) { options = (typeof(options) === "undefined") ? { duration: null, // in days, if null, session only path: null // relative to domain, if null, default from URI } : options; if (options.duration !== null) { var exdate = new Date(); exdate.setDate(exdate.getDate()+options.duration); } document.cookie=c_name+ "=" +escape(value)+ ((options.duration===null) ? "" : ";expires="+exdate.toGMTString()) + ((options.path===null) ? "" : ";path="+options.path); }, get: function(c_name) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "="); if (c_start !== -1) { c_start = c_start + c_name.length+1; var c_end = document.cookie.indexOf(";",c_start); if (c_end === -1) c_end = document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return false; }, remove: function(c_name) { this.set(c_name, "", {duration: -1, path: "/"}); } }; // end Cookie wrapper PHP: To use it // set Cookie.set("language", "en", {path: "/", duration: 365}); // read it into a var var language = Cookie.get("language"); // remove it. Cookie.remove("language"); PHP:
what is the question? once you set it upon a click or whatever, you can always read it for example var language = Cookie.get("language"); // language => "en" always for any subsequent page.
// set Cookie.set("language", "en", {path: "/", duration: 365}); // read it into a var var language = Cookie.get("language"); // remove it. Cookie.remove("language"); PHP: How do I use for example // set Cookie.set("language", "en", {path: "/", duration: 365}) Do I put this in a hyperlink or anywhere in the page?Is this suppose to be in hyperlinks? Ex:<a href="something with cookies To remove it how do I put in a href? This is the format I'm going to have for the to language. <a href="http://en.mysite.com/" title="English"><strong>English</strong> - continue</a> Any help will be greatly appreciated.
just code a function. you can also use inline js like so: <a href="blah" onclick="javascript:cookie.set('language', this.getAttribute('title'))" > ... or something (would record english into your cookie) although this is a bad practice. somehow i don't think this matters a lot in your case
I am looking for a how to guide for cookies, because I own a site and would like to track my users. Or rather set up a referral system.
How do I use for example // set Cookie.set("language", "en", {path: "/", duration: 365}) Would it be :<a href="blah" onclick="javascript:cookie.set('language', {path: "/", duration: 365});" > What is this.getAttribute('title'))" > suppose to be?
right. basically the value you assign to the cookie is the second parameter here is a practical piece of code that will check if the user has visited before, assign 'visited' to true or false dependent of the result. if they have NOT visited previously, set them a cookie which will change subsequent visits to use 'visited = true' instead: var visited = Cookie.get("visited"); if (!visited) { // not visited, set the cookie // assign a cookie visited with value true || 1 that lasts 30 days: Cookie.set("visited", true, {duration: 30, path: "/"}); // you can now do something for new visitors. } else { // they have visited before, do whatever you do to returning users } Code (javascript): the code above is not event / click driven.