require perl script for random html

Discuss Programming

require perl script for random html

Postby Calum » Tue May 25, 2004 4:25 pm

hello all,

well i would like (and remember i know nothing about perl here) a perl script (yes, perl, that's what the server has on it, so no "php is best" please! :) ) which will serve a random front page selected from a directory full of possible front pages. So for example, say i have a directory at ~/www/index.html.d/ and it has 20 html pages in it, every time the site is visited, the index page will call one of the 20 files at random.

also it would be good if i can find one which will display random text messages as part of the web page, so say i have a section of the page where i want "message of the day" or whatever to be displayed, every time the page is called, that space on the page will contain a random phrase pulled from a list, stored as a text file, obviously with some sort of delineation between messages, or i suppose i could have a directory again with each message in its own file.

Now of course i wouldn't want anybody to write these as i am sure they have been written many times, but i thought probably somebody would know if thses scripts are out there somewhere for download off of somebody's site, and if so, whereare they?
any ideas? and thanks in advance...reu
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby cdhgold » Tue May 25, 2004 6:25 pm

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
User avatar
cdhgold
administrator
administrator
 
Posts: 382
Joined: Tue Mar 18, 2003 6:11 pm
Location: Texas

Postby Void Main » Tue May 25, 2004 6:26 pm

Here's a quick hack to spit out a random *.html file in a specified directory. Note, I didn't really put much thought into it so there is no error checking and I didn't take security into consideration. Should do what you want though:

randhtml.cgi:
Code: Select all
#!/usr/bin/perl -w
                                                                               
#############################################
# Another ugly sample by Void Main
# Spit out a random *.html file from a dir
#############################################
                                                                               
my $HTMLdir = "/var/www/index.html.d";
                                                                               
opendir(HDIR, $HTMLdir) || die("Cannot open directory!");
my @HTMLfiles = grep /\.html/, readdir HDIR;
closedir(HDIR);
                                                                               
srand;
$i = rand @HTMLfiles;
                                                                               
print "Content-type: text/html\n\n";
open(HFILE,"<$HTMLdir/$HTMLfiles[$i]");
while (<HFILE>) { print; }
close(HFILE);
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Tue May 25, 2004 7:08 pm

That is actully a very good example Void, LOL. I have written and used an almost exact copy myself about a year ago. The only basic difference was the variable and array names.

Oh by the way the use of "my" in your variable declerations is not neccessary unless you "use strict;" which all good perl programmers should do. :)
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Tue May 25, 2004 7:22 pm

Yes, using "my" is actually good to localize the variables, however, as you say they are not necessary, and since there are no subroutines/functions localizing really doesn't matter. It's just habit as small programs usually turn into larger programs. I should have actually added the "use strict;" and it would have been a better example. :)

Here, better example:

Code: Select all
#!/usr/bin/perl -w
 
#############################################
# Another ugly sample by Void Main v0.00001a
# Spit out a random *.html file from a dir
#############################################
 
use strict;
 
my $HTMLdir = "/var/www/index.html.d";
 
opendir(HDIR, $HTMLdir) || die("Cannot open directory!");
my @HTMLfiles = grep /\.html/, readdir HDIR;
closedir(HDIR);
 
srand;
my $i = rand @HTMLfiles;
 
print "Content-type: text/html\n\n";
open(HFILE,"<$HTMLdir/$HTMLfiles[$i]");
while (<HFILE>) { print; }
close(HFILE);


:)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Wed May 26, 2004 9:52 am

I messed around a little bit with this example due to the use of grep in Voids's example. There is nothing wrong with this usage if you are sure your app is running on a *nix platform however if you want to cover all bases you need a platform inspecific application.

Here is one example of an app this will run on any OS platform.

Code: Select all
#!/usr/bin/perl -w

#############################################
# An even uglier example than Void's, but is
# platform independant. :)
#############################################

use strict;
# I used single ticks ('') in the following variable decleration
# to eliminate the possible interpolation factor of a file path.
# Be sure to change this value to your real file path.
my $HTMLdir = '.';

opendir(HDIR, $HTMLdir) || die "Cannot open $HTMLdir!: $!";
my @HTMLfiles = readdir(HDIR);
closedir(HDIR);

sub pick {
   my $i      = rand @HTMLfiles;
   my $target = "$HTMLdir/$HTMLfiles[$i]";

   if ($target =~ /\.htm/i) { #<-- This matches .htm or .html
      print "Content-type: text/html\n\n";
      open(HFILE, "<$target");
      flock(HFILE, 2); while (<HFILE>) { print; }
      close(HFILE);
   }else{
      pick();
   }
}
pick();


I added a bit more functionality to this example in case you have more than just htm or html files in this directory. You still want your app to perform what it was designed to do and not give up if it finds a non-html file so I added some recursion. I also added the file locking function out of habit and good filehandle handling practices. If you have a busy site there is a large possibility that your application might try to access the same randomly chosen file twice at the same time. This of course would cause errors and possibly some unhappy web surfers. :)

Edit:
Dang spelling mistakes.
Last edited by ZiaTioN on Wed May 26, 2004 10:58 am, edited 2 times in total.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Wed May 26, 2004 10:27 am

I didn't use a system call for "grep", I used the Perl built in "grep" function. This should be platform independent.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Wed May 26, 2004 10:54 am

Ahh LOL.. I totally missed that. I saw grep and assumed backticks. Yes you are right this should be plattform independant.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Calum » Wed May 26, 2004 12:17 pm

guys this is excellent!

i really appreciate this script, but much more i appreciate your explaining what is going on! this will give me a working thing to shoehorn into my site and it will also allow me to modify it when i want to.

For information, the files will be only html, or as i say text, and the platform is a remotely hosted NetBSD machine, which i have no control over the administration of.

Incidentally i posted this over at the olf FMS site out of habit and have so far had NO replies.

thanks again guys!
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Wed May 26, 2004 1:12 pm

That's because they are still in the "hate monger" stage over there. I have moved beyond that into the "they (M$) no longer exist in my world monger" stage. :)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Wed May 26, 2004 4:01 pm

Calum wrote:Incidentally i posted this over at the olf FMS site out of habit and have so far had NO replies.


Because it is gay!
All just annoying teens saying (and i'm about to use a JimmyJames-ism)...

yaw yaw yaw yaw yaw yaw yaw


But I say no more, this forum is about love not hate!
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Calum » Thu May 27, 2004 3:58 pm

so easy!

it works it works!

thanks again guys, i spent a good while editing my html pages and moving them about a bit to accomodate the change (since they all have links to the former index page and that's the page i am replacing blah blah blah, but installing the script was simplicity itself.

example of it working perfectly here:

http://www.polytheism.org.uk/ (refresh a few times, isn't it great! :) )
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Calum » Sat May 29, 2004 9:16 am

you knew this would happen...

is there a way to get it to allow me to specify which page i want it to serve?

say i want to give somebody a url for the site, but i want them to see a particular first page when they go there, i'd like to just be able to say http://polytheism.org.uk/cgi-bin/random.cgi#faq.html or http://polytheism.org.uk/cgi-bin/random.cgi#about.html if you see what i am getting at...
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Mon May 31, 2004 6:46 pm

You have two options. You could make the /var/www/index.html.d directory accessible through the web server in which case you could just point them to the actual page, or you could add to your code so you can add a parameter that will force a specific page:

Code: Select all
#!/usr/bin/perl -w
                                                                               
#############################################
# Another ugly sample by Void Main v0.00001b
# 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);


You would call it by "http://www.yoursite.com/rand.cgi?page=yourpage.html".
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 9:39 am

Yeah I was going to say you would have to use "?" instead of "#" LOL.. :)

CGI is awesome for this sort of thing.

Edit:
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. :)
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron