Development Dynamic pages via CGI

Discussion in 'Software' started by OneSeventeen, 2 May 2002.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Location:
    Albuquerque, NM
    Posts:
    3,454
    Likes Received:
    2
    I'm working on a site for the human resources department at UNM, because their original one isn't so hot..., but I want to make dynamic pages instead of wasting my time writing pages over & over. Right now I'm using server side includes, and just want help on making a script so I can create links to piece the pages together on the fly (as opposed to having a file index.htm that uses index-c.htm for its content)
    I think that RandLand did this, or something similar on his site..

    help? :sigh:

    edit:
    oh, and if you were curious:
    http://www.unm.edu/~cjadams/hrprop2/
    (tables are used because our school supports outdated netscape browsers...)
     
    Last edited: 2 May 2002
  2. Randland

    Randland What's a Dremel?

    Joined:
    5 Feb 2002
    Location:
    Eau Claire, WI
    Posts:
    191
    Likes Received:
    0
    Yup, I did do something similar on my site, but not quite the same. Please don't laugh at me because I probably did this all weird and non standard-like, but it was the first thing I ever did with Perl. Ok, here goes.

    First I decided what layout I wanted my page to be in. Because I haven't learned CSS yet this included alot of tables, but things were split up into basically componants. I used:

    Page Header
    Sidebox Header
    Sidebox Divider
    Sidebox Footer
    Page Divider
    Story Header
    Story Footer
    Titled Story Header
    Titled Story Divider
    Titled Story Footer
    Page Footer

    This enabled me to create my own markup language to tell my script what to do. First I made a file called "menu" and in it I put all of the stuff that will fit in the sideboxes. The format was as follows:

    Code:
    <sidebox>
    <title> Insert Title Here </title>
    Insert links, cool stuff, whatever here, this is the body of the sidebox
    </sidebox>
    Then you could add as many of those as you wanted, and all pages would load from that menu file.

    I did the same thing with the content, but specified the file name with the "Page" value that is passed in. This tells the script what page layout to go look at for the stories and such.

    Code:
    <story>
    This is just a plain old story without a title
    </story>
    
    <story>
    <title> Title </title>
    This is a story with a built in title
    </story>
    
    Then my homebrewed script just layed down the pieces in order, parsed the menu and page files and added them. It was really simple, and that is why I am sure I could have done it better. Hope that helps.

    By the way, I am planning on changing this all to CSS and PHP, but for the time being, stop by and tell me what you think of the layout and the looks. http://karpenske.com/display.cgi?page=home:D
     
    Last edited: 2 May 2002
  3. Randland

    Randland What's a Dremel?

    Joined:
    5 Feb 2002
    Location:
    Eau Claire, WI
    Posts:
    191
    Likes Received:
    0
    If anyone wants I can post my script, it is well documented.
     
  4. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Location:
    Albuquerque, NM
    Posts:
    3,454
    Likes Received:
    2
    Anyone Wants!

    I really want to get to know scripting, and at work is the only place I have access to a CGI bin.
    I'm about to start paying $$ for hosting.. I'm thinking of powweb, which is what you use, isn't it? (sorry, I know this belongs in the other thread, but I'm lazy..):p

    But yeah, post the script as soon as you get the chance!
    hehe /me freeloads
     
  5. Randland

    Randland What's a Dremel?

    Joined:
    5 Feb 2002
    Location:
    Eau Claire, WI
    Posts:
    191
    Likes Received:
    0
    Yup, Powweb is what I use, and I like it alot. Anyway, here you go:

    Code:
    #!/usr/bin/perl
    
    # Import the query string from the bowser, and store the page requested
    # to the $page scalar
    use CGI;
    $query = new CGI;
    $page = $query->param('page');
    
    if( !$page )
    {
      $page = 'Home';
    }
    
    print $query->header;
    
    # Store the page template location to the $template scalar
    $template = 'PageTemplate';
    
    # Store the layout information to the $layout scalar so that it can be parsed
    open (LAYOUT, "< $page/layout")
      or die "Can't open menu";
    @layout = <LAYOUT>;
    $layout = join'', @layout;
    
    # Store the menu layout to the $menu scalar so that it can be parsed
    open (MENU, "< menu")
      or die "Can't open menu";
    @menu = <MENU>;
    $menu = join'', @menu;
    
    # Print the page header
    &printFile("$template/PageHeader");
    
    # Print the menu information, search for each instance of the sidebox tags
    while ($menu =~ /<sidebox>(.*?)<\/sidebox>/sg)
    {
      # Store the text found between the tags to the $sidebox scalar
      $sidebox = $1;
      
      # Print the sidebox header information
      &printFile("$template/SideboxHeader");
      
      # Find the title to the sidebox
      if ($sidebox =~ /<title>(.*)<\/title>/s)
      {
        # Print it
        print $1;
      }
    
      # Print the dividing line between the title and the content
      &printFile("$template/SideboxDivider");
    
      # Find the content for the sidebox
      if ($sidebox =~ /<body>(.*)<\/body>/s)
      {
        # Print it
        print $1;
      }
    
      # Print the footer for the sidebox
      &printFile("$template/SideboxFooter");
    }
    
    # Print the dividing line between the sidebar and the story section
    &printFile("$template/PageDivider");
    
    # Print the story information found between the story tags
    while ($layout =~ /<story>(.*?)<\/story>/sg)
    {
      $story = $1;
      
      # Find out if the story requires a title bar
      if ($story =~ /<title>(.*)<\/title>/s)
      {
        # Print the titled story header
        &printFile("$template/TStoryHeader");
    
        # Print the titled story title
        print $1;
    
        # Print the dividing line between the title and the content
        &printFile("$template/TStoryDivider");
    
        # Find the content between the body tags
        if ($story =~ /<body>(.*)<\/body>/s)
        {
          # Print it 
          print $1;
        }
    
        # Print the titled story footer
        &printFile("$template/TStoryFooter");
      }
      # Or
      else
      {
        # Print the story header
        &printFile("$template/StoryHeader");
        # Print the story content
        print $1;
        # Print the story footer
        &printFile("$template/StoryFooter");
      }
         
    }
    
    &printFile("$template/StoryHeader");
    print "<center>You are visitor number ";
    &printFile("count");
    print " to visit this site</center>";
    &printFile("$template/StoryFooter");
    
    open(IPLOG, ">> iplog")
      or die "Can't open iplog";
    @iplog = <IPLOG>;
    close IPLOG;
    $iplog = join'',@iplog;
    $newIP = 1;
    if($iplog =~ /$ENV{'REMOTE_ADDR'}/sg)
    {
      $newIP = 0;
    }
    if($iplog =~ /<IP>(.*?)<\/IP>/sg)
    {
      if($1 eq "$ENV{'REMOTE_ADDR'}")
      {
        $newIP = 0;
      }
    }
    open(COUNT, "< count")
      or die "Can't open count";
    $count = <COUNT>;
    if ($newIP = 1)
    {
      $count++;
      open(IPLOG, ">> iplog")
        or die "Can't open iplog";
      print IPLOG "<IP>";
      print IPLOG "$ENV{'REMOTE_ADDR'}";
      print IPLOG "</IP>\n";
      close IPLOG;
    }
    close COUNT;
    open(COUNT, "> count")
     or die "Can't open count";
    print COUNT $count;
    close COUNT;
    
    # Print the page footer
    &printFile("$template/PageFooter");
    
    # Subroutine to print a file to the HTML document
    sub printFile()
    {
      # For each of filenames passed
      foreach $file (@_)
      {
        # Open the file
        open (FILE, "< $file")
          or die "Can't open $file";
        
        # Print the file to the document
        while (<FILE>)
        {
          print;
        }
        
        # Close the file so that another one can be opened
        close FILE;
      }
    }
    Hope that helps ;)
     
    Last edited: 2 May 2002

Share This Page