Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a webpage that will contain a poll. The way I tell if I should display the form to vote or the results is by checking to see if the user's IP address has been logged for this particular poll. However, I don't want to have my entire site run by a Perl program - having to generate code on every page hit is more computing power than I'd like to use.

Is there any way I can use something else - maybe SSI's "<!--#exec cmd=...>" or some other implementation to pass my existing poll %ENV? Particularly $ENV{REMOTE_ADDR}?

I've tried running something like this:

<!--#exec cmd="perl -e \"print $ENV{REMOTE_ADDR}\"" -->

But that doesn't capture %ENV. Does anyone have any ideas?

Thanks.

Replies are listed 'Best First'.
Re: Getting %ENV without CGI?
by Kanji (Parson) on Sep 18, 2002 at 04:58 UTC

    Actually, you don't need Perl at all...

    <!--#echo var="REMOTE_ADDR"-->

    But I have to wonder ... how are you tallying the results of the poll? If you're using a Perl CGI, why do you need to access the REMOTE_ADDR beforehand when it should be available to your script as $ENV{'REMOTE_ADDR'}? It seems redundant to me to use SSI at all ...

        --k.


Re: Getting %ENV without CGI?
by blokhead (Monsignor) on Sep 18, 2002 at 04:49 UTC
    When I use a server-parsed HTML file, the includes get all the proper %ENV variables one would expect from CGI (including REMOTE_ADDR). Either rename all of your HTML files to *.shtml, or add this line to httpd.conf (this is how I do it):
    AddHandler server-parsed .html .shtml
    In fact, I recall having standalone CGI perl scripts that eventually became includes in my HTML pages, and no modifications to the scripts were required for it to work properly. Of course, you won't be able to do POST forms through includes, only GET forms (as far as I know).

    blokhead

Re: Getting %ENV without CGI?
by Zaxo (Archbishop) on Sep 18, 2002 at 04:58 UTC

    Where do you keep your poll data? Do you want SSI to maintain the data? What do you plan to do about clients on dynamic IPs?

    If speed is the issue, think about mod_perl. If you want maintainability of static pages, think about one of the templating systems from CPAN.

    Btw, I believe merlyn has some columns on these issues. ;-)

    After Compline,
    Zaxo

Re: Getting %ENV without CGI?
by BigLug (Chaplain) on Sep 18, 2002 at 06:15 UTC
    If you're looking to stop people voting more than once then you don't need SSI. The poll appears on everyone's browser and when they click on an option they're passed to a perl script right? If so, then its just the perl script that needs to access the REMOTE_ADDR. If they've voted before then you politely tell them where to go. If you've not seen them before you count the vote.

    The only reason I can see for using SSI would be if you want to stop people who have voted from seeing the poll a second time. If this is what you're trying to do then SSI should have all the ENV vars available anyway, just use the script name in your SSI ('script.pl'). If you tell the SSI to execute 'perl script.pl' then it will be run in shell and will not have the ENV vars you need.

Re: Getting %ENV without CGI?
by alien_life_form (Pilgrim) on Sep 18, 2002 at 06:53 UTC
    Greetings,

    Surely I do not need to point out that - if I understand correctly what you are I doing - you are basically allowing one single vote per proxy? (e.g AOL is allowed 20/30 votes under your schema).

    If I were you, I would look into cookies...
    Cheers,
    alf


    You can't have everything: where would you put it?
      I have experience of making a vote script myself, I log the IPs to stop people voting twice from the same IP, but like alien_life_form says, they can still vote... I have cookies as well, but they just disable them, or go through their cookie files and clear them out.

      What's a girl to do? It's not even as if the vote is a life or death thing... some people just cheat for the fun of it :(

        Hey, to get around cheating, there's always the totalitarian method of voting..only give them one option :D
Re: Getting %ENV without CGI?
by Aristotle (Chancellor) on Sep 18, 2002 at 18:12 UTC

    An SSI exec produces no less (in fact, a bit more) CPU load than a CGI script.

    You can actually do this purely in SSI, but it is going to be a very, very ugly hack:

    <!--#include virtual="/poll.shtml" --> /poll.shtml:
    <!--#if expr="\"$REMOTE_ADDR\" = /1\.2\.3\.4|5\.6\.7\.8|9\.10\.11\.12/ + --> <h1>Poll RESULTS go here</h1> <!--#else --> <h1>Poll FORM goes here</h1> <!--#endif -->

    Then the vote CGI would have to rewrite the file, updating that regular expression along with the poll results every time someone casts a vote..

    Well, it works - but as I said, that's rather very ugly a way to go about this.

    Makeshifts last the longest.