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

Perl coding newbie here, i need to include several html pages in a perl script with a password authentification at the front, i have the password functions down, but how do keep the links in the fake HTML printed by the script based in the script, so i dont have people able to goto the content i dont want them to see. this way i can keep all the pages in the one script and have the links goto the database tag and print my html code? when i came to this site, i think this is what i was thinking about. thanks for any help

Replies are listed 'Best First'.
Re: HTML printing
by dvergin (Monsignor) on Feb 17, 2001 at 06:27 UTC
    Here's an approach: add a query string to your url that specifies which page to display. Something like:   http://my.server.com/multipage.pl?page=1 Then, in your perl script, you grab this value and use it to decide which page to display. The query string can be anything you want: "?display=CoolStuff" would work fine. Just make sure you don't include any spaces. (There's a way to do that but don't worry about it now.)

    Two ways to get the query string value. You can get the whole query string with    my $qstr = $ENV{QUERY_STRING}; and then parse it yourself to get the page reference.

    Better would be to

    use CGI my $cgi = CGI::new() or die "Can't load new CGI"; my $page = $cgi->param('page');
    Then, as before, branch to or select the routine to print the specific page. You can use heredocs or normal print statements, or a mixed combination of the two, whatever does the job.

    In printing your various HTML pages, you can then include the <a....> links you need with the appropriate query strings for the other pages. Like this:    <a href="http://my.server.com/multipage.pl?page=2">Show page 2</a> You can also have subroutines to spit out portions of the HTML if you have repeated sections. And you can use conditionals to deliver pieces depending on whatever you want (username, requested page, etc.)

    If you want more query string values in one url in order to do cool things, just separate them with '&'. Update: Which of course, you must write as &amp; (see next post -- thanks merlin).

    I recommend you check out the CGI module. You may not feel you need it for this simple exercise, but this project would be a good way to get to know it for later. It is a powerful, versatile, and well-tested module.

    Also, if you want to know more about what is being passed from your browser and is capturable by Perl, here is a page that may help you.

    There is also some magic involving HTTP (not HTML) headers that you need to take care of. The CGI module can help with this. Or, at the very least, just be sure to    print "Content-type: text/html\n\n"; before anything else. (Note the double NewLine.)

    If you need more than this to get you going, just re-post. Good luck.

    dv

      If you want more query string values in one url in order to do cool things, just separate them with '&'.
      Which of course, you must write as &amp;, so when you send out the html, it looks like this:
      <a href="http://my.server.com/multipage.pl?page=2&amp;book=3">Show pag +e 2 of book 3</a>
      Please don't make the same mistake the monestary makes, which forces the browser to error-correct all the links, sometimes in surprising ways.

      And someday, I'll lean on his vroom-ness to fix this, but so far I'm just waiting for him to be enlightened on his own. {grin}

      -- Randal L. Schwartz, Perl hacker

        With great respect and appreciation for Randal... and not a little trepidation, let me quote two bits of tested perl code that each print '&' into an HTML page without making use of the '&amp' entity.

        The first example prints the '&' as displayed, plain text.
        The second example prints the '&' inside an HTML tag:

        print "Let's show a query string: ?key1=one&key2=two\n"; print "<a href='$my_url?key1=one&key2=two'>Click here</a>\n";
        The HTML '&amp' entity does have its use. A prime example of which is the fact that I had to type '&amp;amp' to get the third word in the previous sentence to appear correctly!

        Update: This is bad advice!! Read the next post for the correction. (This post left in place so that Randal's next post will make sense.)

Re: HTML printing
by deprecated (Priest) on Feb 17, 2001 at 05:40 UTC
    You can use heredocs:
    print <<'END_HTML'; <h1>this is a big piece of text sent to the browser</h1> END_HTML
    I posted something that used heredocs extensively not too long ago. The documentation is in perldoc perldata.

    I'm not quite sure this is what you wanted, but your post wasnt very clear.

    update

    : you could use heredocs like so:
    $array_of_pages[0] =<< 'LABEL'; # your html code goes here LABEL $array_of_pages[1] =<< 'LABEL; # your other html code goes here LABEL # ... and so on

    # directly from perldoc perldata... ($quote = <<'FINIS') =~ s/^\s+//gm; The Road goes ever on and on, down from the door where it began. FINIS
    this is what Quinn1981 seemed to be asking me in the CB.

    --
    i am not cool enough to have a signature.

      well, like if i used somesort of print method to give some html to the browser, i was wondering how i could have several pages worth of html docs in the perl script; and have them link to each other, kinda how this page works i think. i guess i just want html pages embedded in my script.