require perl script for random html

Discuss Programming

Postby Void Main » Tue Jun 01, 2004 10:07 am

ZiaTioN wrote:Void, just out of curiosity why do you use single ticks as your regexp delimiters? Is it simply personal preference? Some people say this is bad practice because there is no performance enhancements in using an other-than-default regexp delimter set and it just adds to reader confusion but I say who cares, use what you like. If you cannot read a little and understand what the "m" designator does then you are probably going to be confused anyway. :)


Ok, here ya go (requires a couple more chars):

Code: Select all
#!/usr/bin/perl -w
                                                                               
#############################################
# Another ugly sample by Void Main v0.00001c
# Spit out a random *.html file from a dir
#############################################
                                                                               
use strict;
use CGI;
my $cgi = new CGI;
                                                                               
my $HTMLdir = "/var/www/index.html.d";
my $HTMLfile = "";
                                                                               
if ($cgi->param('page')) {
                                                                               
  my $page = $cgi->param('page');
                                                                               
  # Very important security consideration
  # (don't allow leading '/' or './')
  if ($page !~ m/(^\/)|(\.\/)/) {
    $HTMLfile = "$HTMLdir/$page";
  }

}
                                                                               
if (! -f "$HTMLfile") {
                                                                               
  opendir(HDIR, $HTMLdir) || die("Cannot open directory!");
  my @HTMLfiles = grep /\.html/, readdir HDIR;
  closedir(HDIR);
                                                                               
  srand;
  my $i = rand @HTMLfiles;
  $HTMLfile = "$HTMLdir/$HTMLfiles[$i]";
                                                                               
}
                                                                               
print "Content-type: text/html\n\n";
open(HFILE,"<$HTMLfile");
while (<HFILE>) { print; }
close(HFILE);


I actually use the "/" normally but that regex was a copy/paste from another Perl program I use that someone else wrote (I don't like reinventing the wheel). The good thing about Perl is that there are 1000 ways to do everything. The bad thing about Perl is that there are 1000 ways to do everything.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Tue Jun 01, 2004 12:27 pm

thanks again void!

oddly though i tried it and it don't work! i think the reason is my directory structure on the site. It's hosted by somebody else (a company called xcalibre.co.uk) so i have no control over it really... the script is in /www.polytheism.org.uk/cgi-bin/ and so are all the html files, although there are copies in /www.polytheism.org.uk/public_html/ which are linked to from the html pages in cgi-bin since the publicly viewable static pages all have to be in the public_html directory. Obviously these paths are all from the perspective of me within my account on their server. so, in the old script, the variable $HTMLdir is set to ".", just like in the example, however i am thinking that possibly the new file would not allow "." to be a valid directory given the security checks you have put in there. is this the case do you think, or should i bark up another tree entirely?
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Tue Jun 01, 2004 1:13 pm

Actually using "." for $HTMLdir should be just fine. What you can't do is use a "./" or "../" or "/" in the "page" variable in your URL. Say you have two *.html files in the same directory the script resides in. If you call the script as you normall do (with no params) then it should display those two files randomly. Does it do that. Say one of the files is called "1.html" and that is the one you want displayed you would:

http://www.yoursite.com/rand.cgi?page=1.html

This does not work? Does it give an error? If so, what is the error? Can the script be run on the command line?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Tue Jun 01, 2004 4:39 pm

Check your error log and see what the error states. It is more than likely a permissions problem or as Void has stated a file path problem. The problem with the way you have it setup is that the script is run from the cgi-bin directory due to the nature of the executable script and the default "Script Alias" value in the Apache httpd.conf file.

Now when you run this script from the cgi-bin directory and you try to acces subdirectories of your htdocs (or any other dir name that is outside your cgi-bin dir) then you will probably run into permissions problem. If that is not the case you are probably trying to link to a file in your htdocs but the script is looking in cgi-bin for this .html file and not in your html directory.

Like I said if you look at your error logs you will be able to see what full file path the script is trying to use if your issue is not with permissions and is with file path.

What I do with a similar script is a add an entry in my httpd.conf include file as such:

<Directory /home/someuser/websites/perlskripts.com/htdocs>
Options ExecCGI
AddHandler cgi-script .cgi
AllowOverride Options FileInfo AuthConfig Limit
</Directory>


We use include files on our server so if your host does not you will have to find a valid place for this entry (more than likely it will be within your vhost section). What this does is makes your htdocs (or whatever your html dir name is) capable of executing executable files so your file path issues will cease to exist since all the linked files will be a subset of this file in in a subdirectory that is already in this directories file path.

I am sure all that sounded a little confusing so post back if something did not jive with you and I will see if I can explain it a different way. Again this may not be your problem, it could be as simple as file permissions but I do not think so since I have had direct issues with this exact setup before and the file path issue along with permissions was my problem.

One last thing is if you choose to go with the method I have (which is the only way to make this work as far as I know) you will have to pay attention to your DirectoryIndex section of your httpd.conf file. This tells the webserver the order to look for these files in. So in my example below if I have an "index.html" and an "index.cgi" in my htdocs directory Apache will read the index.html first and never reads the index.cgi. You will have to of course name your script "index.cgi" and then rename your index.html file or remove it all together so Apache does not find it and continues on to your cgi script.

DirectoryIndex index.html index.php index.cgi


index.cgi might not be the name allowed for executable index pages on your server. An easy way to find out if it is is give the following command to the system.

grep -n DirectoryIndex /etc/httpd/httpd.conf
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Tue Jun 01, 2004 5:19 pm

The thing is, he said it worked before I added the code for the param. Maybe they don't have the "CGI" module available. If he could run it on the command line it would give him an error, also check the error_log as you said. This would surely have the error message if there is one.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Wed Jun 02, 2004 10:37 am

True,

Just run a "perl -c scriptname.pl" at the shell prompt. This will check the syntax and module dependencies. If the module does not exist this will tell you.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Calum » Wed Jun 02, 2004 3:22 pm

ok, the html files are in the same dir (the cgi-bin one) as the script, also i am saying that the new script gives a 500 error when run with no parameters (eg, i visit the file using a browser and get a standard 500 error page), while the old one worked fine, both with the only editing being that i used "." as my $HTMLdir.
and
Code: Select all
-bash-2.05b$ /usr/bin/perl -c randomnew.cgi
randomnew.cgi syntax OK
-bash-2.05b$ ls -l randomnew.cgi
-rwxr-xr-x    1 calum9   telnet       1731 Jun  1 19:15 randomnew.cgi


if you really want, have a look at the actual 500 error at http://www.polytheism.org.uk/cgi-bin/randomnew.cgi however i think you can probably imagine it quite adequately...
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Wed Jun 02, 2004 3:29 pm

You'll have to find the "error_log" for the web server. The error message will be in there with an idea of what the problem is. What do you get when you just run it like this from the command line?:

./randomnew.cgi

The only thing that makes any possible sense to me is that the CGI module isn't available. The error_log would most definitely give an indication of where the problem is though.

EDIT: Just thought of something else. Try commenting out the line that prints out the "Content-type text/html". It might be that the CGI module already has sent this header and it is no longer needed but that shouldn't be the case. It works fine on my system.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Thu Jun 03, 2004 12:26 am

I have to agree, I just tried it on my server and it seems to work fine.

You can see it here:

http://www.perlskripts.com/temp-cgi/random.cgi

The above performs the random function.

http://www.perlskripts.com/temp-cgi/ran ... age=1.html

This one specifically calls the first html page.

http://www.perlskripts.com/temp-cgi/ran ... age=2.html

This specifically calls the second html page. Everything seems to be functioning like it should be. However I am not sure if the CGI module is absent from the server because the "-c" would have reported that.

Again a simple run from the command line or an error_log entry will guide you.

Here is an example of the output from the perl -c command if a module does not exist:

Code: Select all
# Just a snippet
use strict;
use CGI;
use Void::Main::Rocks;


and with this new addition to the script we get the following:

[someuser@perlskripts.com]$ perl -c random.cgi
Can't locate Void/Main/Rocks.pm in @INC (@INC contains: /usr/lib/perl5/5.8.3/i486-linux /usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/i486-linux /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl .) at random.cgi line 10.
BEGIN failed--compilation aborted at random.cgi line 10.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Calum » Sat Jun 05, 2004 10:49 am

Code: Select all
-bash-2.05b$ ./randomnew.cgi
Cannot open directory! at ./randomnew.cgi line 29.
-bash-2.05b$ tail -8 ../logs/error_log
[Sat Jun  5 13:49:42 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/robots.txt
[Sat Jun  5 13:49:42 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/missing.html
[Sat Jun  5 15:09:12 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/robots.txt
[Sat Jun  5 15:09:12 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/missing.html
[Sat Jun  5 16:25:33 2004] [error] [client 66.196.90.186] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/robots.txt
[Sat Jun  5 16:25:33 2004] [error] [client 66.196.90.186] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/missing.html
[Sat Jun  5 17:17:34 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/robots.txt
[Sat Jun  5 17:17:34 2004] [error] [client 65.54.164.39] File does not exist: /home/6041/calum9/www.polytheism.org.uk/public_html/missing.html


?? these errors are all from last week, and refer to me shifting the files around and changing the hyperlinks within.
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Sat Jun 05, 2004 5:31 pm

Hmmm, did you say you changed the "$HTMLdir" variable to "."? That should work, it does on mine. Try giving it the full path to the directory. Might want to paste in your source too just to make sure everything looks right.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Fri Dec 10, 2004 2:30 pm

cdhgold wrote:not sure about the random web page but here is a link that should give you the info on how to do the random quotes

http://www.techtv.com/screensavers/answ ... 08,00.html

Hope this helps
Chris


hi again all. i feel well dumb here, i've followed the instructions on the above page, and the path to the cgi-bin directory is correct, and i had a quick look over this page: http://tech.irt.org/articles/js166/ just to make sure, but for some reason, when i stick that directive on my web page, there's no output.

i am expecting there to be some output directly under the integrational polytheism image in the right hand column at www.polytheism.org.uk but if you see on the page, there's nothing. is this right? essentially, what have i done wrong? :oops:

from this brief look at the concept, i'd expect an error message to appear there rather than nothing. also, have a look at the source for the page, the directive appears there rather than any quote. incidentally the quotes.txt file is in the same directory as the qotd file and contains three lines of test quotes.
when i run the qotd program on the command line, this:
Code: Select all
-bash-2.05b$ perl -wT qotd
Content-type: text/html

"He is truly wise who's traveled far and knows the ways of the world." - anon. -Viking sayings, ca.800 AD



so, clearly it's working (although i don't see why it wants to print out this Content-type etc declaration), but why isn't it working *right*? :)
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Fri Dec 10, 2004 6:14 pm

I don't see an include anywhere in the source of your page to call the script. The "Content-Type" is being printed because it's in your code (in a "print" statemnet). If you don't want it remove it or comment it out. I'm having a little trouble following what you are trying to do though so actually seeing the exact files that you are talking about would probably help greatly.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Sat Dec 11, 2004 6:24 am

ok, here we go, i feel really dumb about this...

the aim of this game is to have a web page identical to the one i currently have, but i'd like to be able to have it display text somewhere on the page, which i specify, which shows random text, selected from some sort of database (in this case, a text file called quotes.txt) at that point on the page.

firstly, this is the bit making me feel dumb, from the techtv instructions:
First, embed an SSI directive into the HTML of your webpage like so:

<!--#include virtual="/cgi-bin/qotd"-->

This command tells the web server to call the program "qotd" in the cgi-bin directory, then stick the output of that program into that spot on the page.


and here are the files concerned:

-bash-2.05b$ pwd
/home/6041/calum9
-bash-2.05b$ ls -l www.polytheism.org.uk/cgi-bin/qotd
-rwxr-xr-x 1 calum9 telnet 218 Dec 10 19:57 www.polytheism.org.uk/cgi-bin/qotd
-bash-2.05b$ cat www.polytheism.org.uk/cgi-bin/qotd
#!/usr/bin/perl -wT
use strict;
my $quote;
open(QUOTES, "quotes.txt") or die "Oops! Can't find quote file: $!";
srand;
rand($.) < 1 && ($quote = $_) while <QUOTES>;
print "Content-type: text/html\n\n";
print $quote;

-bash-2.05b$ cat www.polytheism.org.uk/cgi-bin/quotes.txt
"you can read to gain knowledge, but you have to seek to gain wisdom," - Karen Follett, Llewellyn's 2004 Witches' Spell-a-Day Almanac
"Ever wonder why god-centered religions make a woman responsible for messing up the world?" - anon.
"He is truly wise who's traveled far and knows the ways of the world." - anon. -Viking sayings, ca.800 AD
-bash-2.05b$ cat www.polytheism.org.uk/public_html/index.html
<!DOCTYPE public "-//wapforum//dtd wml 1.2//en" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="author" content="Calum">
<meta name="Keywords"
content="spirituality,religion,god,pagan,wicca,gods,muslim,christian,worship,bla
sphemy,norse,eclectic"> <meta name="Description" content="Integrational
Polytheism - Make up your own rules..."> <link rev="made"
href="mailto:polytheism@linuxmail.org.REMOVETHISBIT"> <meta name="robots"
content="index, follow"><title>Polytheism.org.uk - Religions of the
World</title></head><body bgcolor="#c8d290">
<br>

<table align="center" width="90%" bgcolor="#e8f2b0">
<tr>
<td align="center"><a
href="http://www.polytheism.org.uk/index.html">Home</a></td><td
align="center"><a href="http://polytheism.org.uk/ip/index.html">Integrational
Polytheism</a></td><td align="center"><a
href="http://polytheism.org.uk/religions/index.html">World Religions</a></td><td
align="center"><a
href="http://polytheism.org.uk/essays/index.html">Philosophical
Essays</a></td><td align="center"><a
href="http://polytheism.org.uk/bbs/index.php" target="_blank">Discussion
Forum</a></td></tr></table><br><table align="center" width="90%"
cellpadding="10" bgcolor="#e8f2b0"><tr>
<td valign="top">

<h3>World Religions, considered with an open mind</h3>
<p>
Hello and welcome to Polytheism.org.uk. This website aims to spotlight various
religions of the world and hopefully to provide some interesting information
about religions you may not know about or may want to know more about. Please
use the quick hyperlinks at the top of each page to find each section of the
site.</p><p>This website sprung up from my own interest in world religions. This
interest has prompted me to search out literature, stories and other information
about various religions which I attempt to present in an easy to understand way
on the site. I like to think that the information here is accurate and correct,
so if you see any inaccuracies, i would appreciate it if you could send me an
email to <u><i>calum</i> <b>@</b> <i>polytheism.org.uk</i></u> so i can correct
the spurious information in question. The site is definitely not limited, by the
way, to smaller religions, although i confess to being more interested in them.
I hope to continue to include features about Christianity, Judaism and other
mainstream religions.</p><p>The other important function that this site provides
is to let you know about <a href="ip/index.html">Integrational Polytheism</a>,
or Inclusional Polytheism as it has been called. This is my own view of
religion. You could call it a sect, a cult, a way of life, a cultural position
or many other things, but basically it is a religious framework describing what
I actually believe myself. It is a little different from the other religions
featured here as it is a "new" religion and it is also non-exclusive, meaning
that is does not exclude belief in other religions (quite the opposite in
fact!).</p><p>There is also a <a href="bbs/index.php" target="_blank">public
discussion forum</a>, feel free to read and contribute to the various
discussions that are going on there. You're more than welcome to talk about
things other than religion in the forum as well, i might add.</p></td>
<td
width=300 valign="top"><img src="logo.jpg">
<p><!--#include virtual="../cgi-bin/qotd"--!>
</p>
</td></tr></table><a
HREF="http://www.monkeys.com/spammers-are-leeches/"> </a></body></html>




so the problem i suspect is that the instructions look like they are saying i should just plonk this wherever i want my quote to appear in the webpage:
<!--#include virtual="/cgi-bin/qotd"--> and that's what i have done, however it is clear to me that this is not the full story, since that'll just get treated like a comment.


it's going to be something embarrasing and obvious, i know it. it even works properly if you call the program using a browser, click here and see -> http://www.polytheism.org.uk/cgi-bin/qotd

edit: hey you know another interesting thing? if i comment out the line that produces that Content-type line, the above link doesn't work when visited with a browser, it just gives a 500 error.

again, probably obvious, but sometimes i have dumb days.
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Sat Dec 11, 2004 8:19 am

Don't feel dumb. What you are trying to accomplish in the way you are trying to accomplish it is beyond basic HTML. You are also trying to introduce SSI includes into your HTML which your web server has to be configured to allow (look up Apache and SSI (server side includes)). Also what you are including is a CGI perl script. Apache doesn't know what kind of output it is so you have to tell it by printing the "Content-Type:" line. You'll see the error in your Apache error log. Also, that call to your qotd program is not really a comment. That is a special tag for SSI. You actually can perform many different kinds of directives in what might look like comment sections.

Now, before I help you get your server configured properly to do SSI (which isn't all that difficult) I want to remind you that what you are actually trying to do by this exercise is create a dynamic web page. I don't think too many people these days use SSI for this. I would personally do the page in a single PHP script.

But, it's certainly not a bad idea to know how to use them so browse over these:

http://httpd.apache.org/docs-2.0/howto/ssi.html
http://httpd.apache.org/docs-2.0/mod/mod_include.html

To get you going there are two things you'll probably have to do. Your Apache config is probably set up to recognize "*.shtml" files as HTML capable of doing SSI so you'll either have to rename your *.html file to *.shtml, or add ".html" to the end of your "AddOutputFilter INCLUDES" directive, or use the XBitHack. The other thing is you'll have to make sure that in your "Directory" tag in your httpd.conf that relates to the location of your web site files you have to add "Includes" to the end of your "Options" tag.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

PreviousNext

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron