Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Site Wide Cookie

Here's what I hope is a quick question...

I am trying to set a cookie that can be used sitewide. I'm creating a lead generation type site. I want users to fill out a form in order to access exclusive information. Once they fill out the form, they have access to the info.

I am dropping a cookie when they user submits the form so that they can just get straight to the content the next time they visit the site. The form they fill out is in the sidebar of every page on the site. When the user fills out the form on one page, they shouldn't see it on ANY page of the site.

Everything is working, except for the sitewide bit. I think the issue is in this bit of code:

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

But here's the full code below. THANKS SO MUCH!

<script type="text/javascript">
<!--
cookie_name="landasp"
expdays=365

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length; 
var clen = document.cookie.length;
var i = 0; 
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j); 
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function saving_cookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000*30)); //set for one month
Data="cooked"

set_cookie(cookie_name,Data,expdate)
}

function get_cookie_data(){
inf=get_cookie(cookie_name)
if(!inf){
document.getElementById("display1").style.display="block"
}
else{
document.getElementById("display2").style.display="block"
}
}

// -->
</script> 
like image 501
Ash Avatar asked Jan 26 '12 13:01

Ash


People also ask

How do I set a cookie for a whole domain?

To make the cookie available to the whole domain (including all subdomains of it), simply set the value to the domain name ( 'example.com' , in this case). Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

Can I set cookie for another path?

You can't access cookies from a different path - otherwise it would be a security hole. The only way I can think of is making /bar set a cookie whose path=/ so that all pages in / (including /foo ) could access it. Save this answer.

How do I put a cookie in a URL?

1. Add a cookie with the key/value from the URL query string (parameter). 2. Pull the cookie value to use as a variable in a link.


1 Answers

You should specify a site wide path, if the path is not given:

((path == null) ? "; path=/" : "; path=" + path) +

You can debug the cookies using Firebug, just have a look at the set cookies.

like image 101
Gregor Avatar answered Oct 05 '22 23:10

Gregor