Current location: Hot Scripts Forums » Programming Languages » Perl » Need simple example of making a website with Perl


Need simple example of making a website with Perl

Reply
  #1 (permalink)  
Old 08-31-03, 07:24 AM
JohnElmore JohnElmore is offline
New Member
 
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Need simple example of making a website with Perl

Hi!

I tried searching all over the internet but didn't found an example how to make a simple site with perl.

I'm thinking of a simple script that makes the page structure (header, footer etc.)

I know that it's possible (like with php)

So if someone could show me an quick example or a link to a tutorial that talks about the subject i'd be happy
Reply With Quote
  #2 (permalink)  
Old 09-29-03, 12:27 AM
rob2132 rob2132 is offline
Newbie Coder
 
Join Date: Sep 2003
Location: USA
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by JohnElmore
Hi!

I tried searching all over the internet but didn't found an example how to make a simple site with perl.

I'm thinking of a simple script that makes the page structure (header, footer etc.)

I know that it's possible (like with php)

So if someone could show me an quick example or a link to a tutorial that talks about the subject i'd be happy :)
How do you mean making a site with Perl? What elements/data are you wanting to be dynamic or interactive? Are you talking about having different files that you can include in every page with simple short code to insert a page's header and footer to save time and make management of the pages you add easier without needing to update them all? If so, what structure do you want to use?
Reply With Quote
  #3 (permalink)  
Old 10-02-03, 05:03 PM
YUPAPA's Avatar
YUPAPA YUPAPA is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Antarctica
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Quote:
Originally Posted by JohnElmore
Hi!

I tried searching all over the internet but didn't found an example how to make a simple site with perl.

I'm thinking of a simple script that makes the page structure (header, footer etc.)

I know that it's possible (like with php)

So if someone could show me an quick example or a link to a tutorial that talks about the subject i'd be happy
A little my example:

Code:
#!/usr/bin/perl
use CGI;
use strict;

my $header_file		=	'/full/path/to/header_file';		# Path to Header
my $footer_file		=	'/full/path/to/footer_file';		# Path to Footer
my $content_dir		=	'/full/path/to/content_folder';		# Path to Web Pages
my $query			=	new CGI;
my $page			=	$query->param('page');

print "Content-Type: text/html\n\n";

print read_file($header_file);
print read_file($content_dir.'/'.$page);
print read_file($footer_file);

sub read_file {
	my $src_code;
	open(FILE,"<$_[0]");
		while(<FILE>) {
			$src_code .= $_;
		}
	close(FILE);
	return $src_code;
}
You call the script like this:
www.yourdomain.com/scriptname.pl?page=web_content.html

web_content.html should be stored under the $content_dir directory.
Reply With Quote
  #4 (permalink)  
Old 10-04-03, 09:20 PM
rob2132 rob2132 is offline
Newbie Coder
 
Join Date: Sep 2003
Location: USA
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
You may as well just print the contents. Also you should (*for security reasons*) not blindly accept the param("page") variable.

Otherwise someone could call:

http://www.yourdomain.com/scriptname.../../etc/passwd, for example.

Following that, not saving everything into a string just to print it out later by returning the new string--this is inefficient for large file content.

Checking the file open call wouldn't be a bad idea either.

Finally, since you're using the CGI module, you should just use print header() instead of print "Content-Type: text/html\n\n";.

Perhaps adding a valid file type to open, to be safe from people opening other files and printing out the contents (i.e., of configuration files or source code)!

Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard);
 
# Path to files to open:
my $header_file = '/full/path/to/header_file';    # Path to Header
my $footer_file = '/full/path/to/footer_file';    # Path to Footer
my $content_dir = '/full/path/to/content_folder'; # Path to Web Pages
 
# List of file extensions that are safe to open:
my @safe_files_ext = qw(
        htm
        html
        txt
);
 
# Get page to open (user specified):
my $query       = new CGI;
chomp(my $page  = $query->param('page') || '');
 
# Get rid of white space on file names passed, if any:
$page =~ s/\s+//g if ($page ne "");
 
# Print header (for CGI):
print header();
 
# Static file (0) or user specified (1):
read_file(0, $header_file);
read_file(1, $content_dir . '/' . $page) if ($page ne "");
read_file(0, $footer_file);
 
# Display file, if safe:
sub read_file {
        my ($in_page, $file) = @_;
        if ($in_page) { # If file is user specified.
                error("Invalid syntax") if ($file =~ /\.\./); # Disallow dir traversing.
                foreach my $file_ext (@safe_files_ext) { # Check each valid file extension:
                        # Stop and report error if in valid.
                        error("Invalid syntax for $file") if ($file !~ /\.$file_ext$/i);
                }
        }
        # Open and print file contents if we're okay.
        open(FILE, $file)       or error("Can't open $file $!");  # report error if can't open file.
                # Do more stuff here if you want.
                print while (<FILE>); # Print file line by line for best efficiency.
        close(FILE);
}
 
# Error subroutine to display error and exit.
sub error {
        my $reason = shift;
        print "Error: $reason\n";
        exit;
}
Anyway, just a quick suggestion--I've just typed this out and have not tested it (but you get the idea).

Last edited by rob2132; 10-04-03 at 09:23 PM.
Reply With Quote
  #5 (permalink)  
Old 10-04-03, 09:31 PM
YUPAPA's Avatar
YUPAPA YUPAPA is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Antarctica
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

That was just a quick example I showed... If he wants a fully secured version, I can code it 100% secure. I like to help and don't like juding ppl's code because that makes the person look bad~ Lastly thanks for correcting the code. I am leaving, Bye~

Last edited by YUPAPA; 10-04-03 at 09:35 PM.
Reply With Quote
  #6 (permalink)  
Old 10-04-03, 09:46 PM
rob2132 rob2132 is offline
Newbie Coder
 
Join Date: Sep 2003
Location: USA
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by YUPAPA
That was just a quick example I showed... If he wants a fully secured version, I can code it 100% secure. I like to help and don't like juding ppl's code because that makes the person look bad~ Lastly thanks for correcting the code. I am leaving, Bye~
If you think my post was meant to attack your code, you're mistaken--that was not my intention. It was simply suggestions to have it be more secure, bug free and efficient based on the code you posted as a solution ( I did so to help the OP, not to berate your code ). I didn't do so to make you look bad. I hope that's clear. I too, didn't go out of my way to code anything special or elaborate... I'm sure you code complete solutions and your quick post with the previous code doesn't reflect your skills.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl newbie has a problem... fowler23 Perl 4 03-24-10 05:14 PM
Help making a script. Arctic ASP 0 10-02-03 06:50 PM
I need a very simple calendar Giullare Script Requests 0 09-24-03 06:53 PM
Running Perl scripts under Microsoft IIS retrocom Perl 3 08-23-03 04:38 PM
running website from CD taher786 Script Requests 2 08-20-03 09:04 AM


All times are GMT -5. The time now is 04:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.