Link to home
Start Free TrialLog in
Avatar of davidcanfield
davidcanfield

asked on

Setting cookie when static page loads /SSI?

I want to be able to set a cookie using a Perl script when a static page loads.

I've created a simple script that takes care of setting and reading the cookies (this calls subprocedures in a library that actually does the work):

#-----------------------
require 'pei_pgs.pl';

    $cid = time * int(rand(100000));
    if (&GetCookies('id')) {
            $visit = "old";
     $id = $Cookies{'id'};
    }else{
     &SetCookies('id',$cid);
     $visit = "new";
     $id = $cid;
    }
#test the results
print "Cookie $id, visit $visit";
#--------------------------------

It works just fine on the dynamic pages that I build at runtime.

However, we have a couple of dozen static pages on the site, and I want to be able to call the same script to set and read cookies when users access them.  I know I can set/read the cookies using JavaScript, but I wanted to be able to use the same Perl script on all of the pages.

I've been trying to do this using SSI:

<!--#exec cgi="../cgi-bin/setcookie.cgi"-->

I can read the cookie if one already exists, this part works just fine.  The problem is that I can't set a cookie using the script.  I know the script is executing OK, because it also does some log file updates (not shown here) and these are working fine.  It returns the value for $id and $visit, and it acts like the cookie got sent, but the browser never recieves it.  I'm assuming this is because I'm calling the script from one directory, and the page lives in a different one? Maybe the cookie just doesn't know where to go? Setting the cookie's path variable doesn't help, because this only tells the cookie where it is valid.

So the page lives in:
<home>/test_pages/cookie_test.shtml

But the script that is calling the cookie is in:
<home>/cgi-bin/setcookie.cgi

It seems like there should be a simple way to do this, and that I'm just missing something brainless. Maybe I'm barking up the wrong tree trying to use SSI to do this, but it seems like this would be a common issue.

Any help would be appreciated.

David
ASKER CERTIFIED SOLUTION
Avatar of shirjeel
shirjeel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davidcanfield
davidcanfield

ASKER

Shirjeel;

Hmmm...didn't know you could do that with Perl scripts(cool).

I did this:

<script language="perl/perlscript" src = "../cgi-bin/name-of-script.cgi"></script>

I placed it in the body of the html, and it worked (after I placed the content header in the script).

Thanks for your help!

David