Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > What is wrong w/ my cgi code?

What is wrong w/ my cgi code?
Thread Tools
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
Apr 14, 2003, 07:07 PM
 
I want to put a cookie on my web page so i know when people have visited. Here is my page. The sentence at the bottom shows up so I know the page is running.

What is wrong w/ my cookie line?

#!/usr/bin/perl -w

print "Set-Cookie: Visited=1; path=/cgi-bin/; domain=.mydomain.com;\n";
print "Content-type:text/html\n\n";

print "<P>A visiting cookie has been sent to the users browser.</P>\n";
(Last edited by poulh; Apr 14, 2003 at 09:58 PM. )
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Apr 16, 2003, 02:02 PM
 
Is that the extent of your .cgi?

I can see two potential issues. One is the lack of HTTP headers.

All HTTP responses should begin with a header line, typically "HTTP/1.1 200 OK", which lets the client know the HTTP version of the response.

The other is the inclusion of the semicolon at the end of the Set-Cookie line. The HTTP spec does not allow for the semicolon, but some browsers handle it gracefully (i.e. ignore it), while others don't.
Gods don't kill people - people with Gods kill people.
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
Apr 16, 2003, 03:28 PM
 
I think it was not having an expiration date. I was looking in Camino's cookie file which only got written out on quitting. However by not having an epiration date the cookie expired at the end of the session so it would never get written out.

Adding the date worked and I took off the ending semi-colon.

However I did not need to add the HTTP/1.1 line.

Thanks for your help!
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 16, 2003, 08:38 PM
 
You do know that there are all sorts of libraries to do this stuff for you, right? You can do "setHeader()" instead of writing out headers yourself.

Writing CGIs like this is a good way to understand the underlying technology, but it's a terrible way to write production applications today.

Then again, any CGI is probably a bad idea today. Go with an application server like Zope/Plone/Tomcat/JBoss/WebLogic/etc
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
Apr 16, 2003, 10:54 PM
 
Yep... I know about the headers. I just wanted to see if I could do it "by hand" just so I know what those headers are really doing behind the scenes.
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
Apr 16, 2003, 11:01 PM
 
Ugh, another problem...

My cookie appears (as in I get it back thru $ENV{"HTTP_COOKIE"} ).

It works in every Gecko-Browser, IE for Win/Mac, Omniweb,

But not Safari!!!! Is this something that isn't finished yet or does safari do it differently. Here is my complete code...

#!/usr/bin/perl -w

sub SetCookie
{
$cookieName = $_[0];
$cookieValue = $_[1];
$theTime = $_[2];

@array = gmtime($theTime);
$day = (qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday))[$array[6]];
$month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$array[4]];
$year = $array[5] + 1900;
$timeString = "$day, $array[3]-$month-$year $array[2]:$array[1]:$array[0]";
print "Set-Cookie: $cookieName=$cookieValue; path=/cgi-bin/; expires=$timeString; domain=.my_domain.com\n";
}

$theTime = time;
$theTime = $theTime + 1800; #30 minutes

SetCookie ("Value", 1, $theTime);
SetCookie ("ID", $ENV{"UNIQUE_ID"}, $theTime);
print "Content-type:text/html\n\n";


print "<P>The counter cookie is /cgi-bin/ set to 1.</P>\n";
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2