Forum Moderators: coopster & phranque

Message Too Old, No Replies

CGI setting a session cookie

Simple script wanted to set session cookie for stats purposes

         

Martin_Sach

2:26 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



I'm reasonably web-literate but have no CGI experience. I am using Mach 5 FastStats which is a web log analyser. It can give me more statistics if I set a session cookie on users' browsers and I prefer to do this by a CGI script rather than Javascript if possible. I don't want to do anything with the cookie, nothing clever at all, but hopefully the web log will include the session information. Ive looked in lots of online libraries and sites for a CGI script that simply does this, and can't find one, so either it's impossible, I'm daft, or its so easy that nobody has thought to explain how to do it. The server is Zeus, by the way, not Apache.

Can anyone give advice please?

Thanks

mikeyb

11:21 am on Apr 18, 2005 (gmt 0)

10+ Year Member



Hi,

Setting a session cookie in CGI (perl) is easy enough:

#!/usr/bin/perl
use CGI qw(:standard);
print "Set-Cookie:name=value\n";

You must make sure the print "Set-Cookie... line runs before any "print header;" or "print "Content-type: text/html\n\n";" lines in your script.

Martin_Sach

10:06 am on Apr 20, 2005 (gmt 0)

10+ Year Member



Hi thanks for that help

I turned this into a short script setcookie.pl which is on the server. The complete content of the file is only the three lines:

#!/usr/bin/perl
use CGI qw(:standard);
print "Set-Cookie:name=ddil\n";

When I try to run this from a browser command line I get server error 500. I have set the permission to 705 (I also tried 777 but it made no difference).

Further basic questions I am trying to work out:

Do I need to run the script from every page that might be an entry page, if so what is the effect of people running the script several times as they move around the site?

How do I call the script from within an html page?

These are obviously very basic questions but I have found that most of the tutorials on the web seem to assume everyone knows this sort of thing!

Help is appreciated!
Martin

rocknbil

4:15 pm on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I try to run this from a browser command line I get server error 500.

That's because you stopped. :-) The cookie is a header. You need to follow it with a content-type, then some output.

#!/usr/bin/perl
use CGI qw(:standard);
print "Set-Cookie:name=ddil\n";
print "content-type: text/html\n\n"; ## Note TWO returns
print "the ddil cookie was set.";

You confused me a bit - command-line should work, but from a browser - yeah that will error. By the way the executable permissions are 755, not 705 or 777 (You may have meant that anyway.)

Do I need to run the script from every page that might be an entry page, if so what is the effect of people running the script several times as they move around the site?

If you want to set a cookie for each page, yes, but you might want to name them all differently. Should have no detrimental effect.

How do I call the script from within an html page?

Hmm you don't. You request the script, it returns an output. Done. If you want to set cookies from an HTML page, you should use Javascript.

OR

Set your links to a script for each page, although that's a convoluted way of doing things . . . .

<a href="cookiescript?page=index.html">Home</a>

#!/usr/bin/perl
use CGI qw(:standard);
print "Set-Cookie:name=ddil\n";
$query = new CGI;
$page = $query->param('page');
print "Location:$page\n\n"; ## Again, note two returns

Martin_Sach

9:24 am on Apr 21, 2005 (gmt 0)

10+ Year Member



Hi, thanks for that, but I don't feel much wiser!

I didn't put any output in because I don't want any - I only want to set a session cookie! I certainly dont want any text printed to the user's screen. But perhaps this doesnt actually print to the screen? Ive updated the setcookie.cgi script anyway, but it still doesnt seem to be doing anything.

I want to set one cookie, not one per page, it is one and one only. However I want it to be set by every page because people arrive at the site from search engines and land on any page, they may never visit the home page.

I dont understand the difference between calling a script and requesting a script. How to I provoke it into running, cause it to run, make it run....? I have tried putting in the header of an html page the following line:
<SCRIPT TYPE="application/x-perl" SRC="setcookie.pl"></SCRIPT>
but this doesnt seem to work. I don't really want to start the script from a page at all but somehow it has to run when a user session begins.

I dont think setting links to a script can be what I'm looking for since there is no promise at all that a user will click on any links.

I get the feeling that I'm barking up the wrong tree here because what I want to do is so simple, and thousands must have wanted to do it before, yet I am finding it so problematical. All I want to do is set a session cookie, one only, whenever a user looks at any page on the site, so that the session information can be recored in the log and analysed later for statistical reasons.

If anyone can suggest where I go next it will be appreciated....

rocknbil

3:43 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you could try this then.

<meta http-equiv="Set-Cookie" content="value=n;expires=date; path=url">

for a session, remove the expires. A valid GMT date causes the cookie to be persistent - that is, a session cookie will never show up in the cookies.txt of NN browsers or the cookies dir of IE.

<meta http-equiv="Set-Cookie" content="value=n;path=url">

Exemplified with your values:

<meta http-equiv="Set-Cookie" content="ddil=1;path=/">

Alternatively, look into Javascript cookies and set one using Javascript. In either case this goes on every page.

Martin_Sach

11:23 am on Apr 26, 2005 (gmt 0)

10+ Year Member



Thanks for that last suggestion - it's the simplest idea yet, I had not realised you could set a cookie in that way, and it works. I'm probably in the end going to have to use javascript but I'm testing them both on different pages at the moment.

Thanks to all those who have helped with this.