Click to See Complete Forum and Search --> : set cookie via HTML header


jeanluca
07-20-2009, 06:06 AM
Hi All

I was wondering how to set a cookie through the header of an HTML page. Normally PHP does this for me, but how would you do this yourself!?

I guess you have to put some sort of meta tag in the head of your HTML file like

<html>
<head>
<meta name="Set-Cookie" content="PREF=8634GPYE4S; expires=Wed, 20-Jul-2011 11:00:07 GMT; path=/; domain=.mydomain.com">


Cheers
LuCa

Charles
07-20-2009, 06:20 AM
No, it has to be done by the server or by JavaScript and the JavaScript method is quite iffy. The server method is iffy also but not quite as iffy.

jeanluca
07-20-2009, 08:46 AM
if you run the following script:
#! /usr/bin/perl
#

use CGI qw/:standard/;

my $query = new CGI ;
my $cookie = "PREF=ID=c5f962b6752ce0d3:TM=1248094016:LM=1248094016:S=Fhbrnbm5Wuz-RCby; expires=Wed, 20-Jul-2011 12:46:56 GMT; path=/; domain=.google.nl" ;
print $query->header(-cookie=>$cookie);

print "<html></html>"

the output is
Set-Cookie: PREF=ID=c5f962b6752ce0d3:TM=1248094016:LM=1248094016:S=Fhbrnbm5Wuz-RCby; expires=Wed, 20-Jul-2011 12:46:56 GMT; path=/; domain=.google.nl
Date: Mon, 20 Jul 2009 14:42:27 GMT
Content-Type: text/html; charset=ISO-8859-1
<html></html>

If I copy-past this into a file the browser doesn't understand it verywel. Any suggestions why?

Charles
07-20-2009, 09:17 AM
In a successful attempt to make things as complicated as possible each HTML page has three different heads. As a part of the communication between browsers and the servers each file is pre-pended with a secret HTTP header that contains information about the file. Browsers can also request just this information so they can decide to use the version already in the cache. Then when the file is sent it is broken up into separate packets that are each routed separately. Each packet has a header that describes where it is trying to go and where it fits into the final document. And then there is the HTML HEAD element.

You want to tinker with the HTTP header using the HTML head and that won't work unless the server is set up to allow you to do that. You can either use some kind of server side scripting or, if and only if the server is set up to do this, you could use:<meta http-equiv="Set-Cookie" content="PREF=ID=c5f962b6752ce0d3:TM=1248094016:LM=1248094016:S=Fhbrnbm5Wuz-RCby; expires=Wed, 20-Jul-2011 12:46:56 GMT; path=/; domain=.google.nl">It is also possible that some browser might pay attention to that.

jeanluca
07-20-2009, 10:14 AM
ok, thnx a lot for explaining it!!

cheers