Need help with cookies problem please!
Ok i set up an HTML page to set up cookie and delete cookie. I would like to set up another button to get cookie. The following code is what i have. Lets say if someone sends me a cookie i want to be able to type in a variable and click get cookie and have it display the name of the cookie and its contents. Any help would be much appreciated, Thank you
<HTML>
<HEAD>
<SCRIPT LANGUAGE="Javascript">
<!--
function getexpirydate( nodays){
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}
function getcookie(cookiename) {
var cookiestring=""+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1 || cookiename=="") return "";
var index2=cookiestring.indexOf(';',index1);
if (index2==-1) index2=cookiestring.length;
return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,duration){
cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
document.cookie=cookiestring;
if(!getcookie(name)){
return false;
}
else{
return true;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<script>
<!--
if(getcookie("visitorname")){
document.write("Welcome "+getcookie("visitorname"));
}
//-->
</script>
<FORM METHOD="GET">
name: <INPUT TYPE="text" NAME="myname" SIZE="10" MAXLENGTH="20">
<INPUT TYPE="submit" NAME="submit1" VALUE="Set Cookie" onclick="setcookie('visitorname',document.forms[0].myname.value,2);">
<INPUT TYPE="submit" NAME="submit2" VALUE="Delete Cookie" onclick="setcookie('visitorname',document.forms[0].myname.value,-1);">
</FORM>
</BODY>
</HTML>
Re: Need help with cookies problem
Re: Need help with cookies problem please!
Does anyone know how to use the variable to retrieve all the cookie contents to be displayed on the screen? I've look all over but nothing shows me how to read the contents and print them. If anyone can help it would be much appreciated. Thanks.
Re: Need help with cookies problem please!
Assuming this is something you are wanting to do server side the method you would use depends on what language you are wanting to use. For example, php has "$_COOKIE" as an environmental variable while perl provides "$ENV{HTTP_COOKIE}". As I've generally refrained from MS servers I don't remember off-hand how to access the cookie with their products.
Re: Need help with cookies problem please!
Yeah im not too sure on that....i dont know if there was away like on how i set the cookie where the user is able to type in his name and you can set the cookie...If you open the cookie you see the users name along with other information. I was wondering if you could have button that could read that information and display it on the screen along where it says Welcome: users name.
Re: Need help with cookies problem please!
Once you set the cookie you can access it using the methods I outlined as it is passed back to the server which created it on subsequent connections. Again the method used to set the cookie depends on how you are serving the page..php has one method, asp another, perl (or cgi) another while plain html requires scripting.
If you are using plain html take a look at http://javascript.internet.com and see if you can't find some cookie scripts there.
Re: Need help with cookies problem please!
Yeah, I'm just using plain HTML. Thanks for you help. Ill look through the link you posted. Thanks again.