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

Hello Monks,

I am having a problem getting an authentication script to work RFC: Proposed tutorial - simple login script using CGI::Application.  I would appreciate any help or suggestions.

I believe that I am having a problem with the my $self = shift. Because it seems as if the script cannot find it's path.  None of the templates can seem to find the parameters because the links don't work (only the tag names appear). The HTMLs appear (with bad links) but  /simple.pl  goes to a 500 Internal Server Error page. 

I changed the script from  use lib '/var/www/cgi-bin/WebApp/libs' to  $ENV{DOCUMENT_ROOT}/.  I also comment out lines 156-159 (as suggested) for the SSL. Otherwise everything in the script is the same. 

Replies are listed 'Best First'.
Re: Authentication Script Path Problems
by Corion (Patriarch) on Sep 11, 2010 at 21:59 UTC

    What does the web server error log say?

    Does the script work from the command line, when run as the same user that the webserver uses to run your script?

    Have a look at CGI::Carp.

    The line my $self = shift; has nothing to do with the script path (whatever you mean by that).

Re: Authentication Script Path Problems
by scorpio17 (Canon) on Sep 12, 2010 at 22:38 UTC

    Hi there!

    Thanks for trying to use my tutorial - I'm sorry you're having trouble getting it to work.

    There are no typos in the code I posted - it should work "as is". I think your problems are due to differences in your file names, directory structures, web server config, etc. I'll try to clarify as much as possible.

    For starters, I'm running Ubuntu linux (any debian based linux should be similar). If you're trying to run under windows... it *should* work, but I haven't tried it, and I'd recommend you switch to linux!

    For starters, my apache config file has the following:

    DocumentRoot /var/www/public_html/

    This means if you put a simple hello_world.html file inside /var/www/public_html you should be able to see it in your browser if you use the URL

    http://mydomain.com/hello_world.html

    You may have to "chmod 644 hello_world.html" to make it readable.

    My apache config file also has this in it:

    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    This means I can put scripts inside the /var/www/cgi-bin directory (for example, /var/www/cgi-bin/hello_worl.pl) and access it with the browser using a URL like:

    http://mydomain.com/cgi-bin/hello_world.pl

    You may have to "chmod 755 hello_world.pl" to make the script work properly.

    My apache config also has this:

    AddHandler cgi-script .pl

    If you don't have this, then your scripts may have to end in .cgi instead of .pl.

    I'd suggest creating simple "hello world" files and making sure that you can get at least that much working - else there's no use going any further.

    Assuming you can get that much to work, let's take a closer look at my tutorial:

    All paths are relative to a working directory of "/var/www/cgi-bin". For example, My WebApp directory is actually "/var/www/cgi-bin/WebApp". For simplicity, I didn't add the "/var/www/cgi-bin" to the front of everything, and in theory you could actually put it somewhere else - but you'd have to modify your apache config accordingly. The file structure under WebApp looks like this:

    WebApp
      | 
      |-simple.pl
      |-simple.ini
      |-libs
      |   |-MyLib
      |       |-Simple.pm
      |       |-Login.pm
      |-templates
          |-index.html
          |-default.html
          |-login_form.html
    

    All the directories should be chmod 755, all the files chmod 644 - except simple.pl, which should be chmod 755, and simple.ini, which should be chmod 600.

    I'm concerned that you didn't follow these instructions exactly as written because in your question you referenced "WebApp/Simple.pl" instead of "WebApp/simple.pl" - this stuff is case sensitive - so it will make a difference.

    I'm also concerned because in the info you posted it looked like you were putting files under "/home/username/public_html/cgi-bin" - if you want to change the file structure you really need to know what you're doing. For example, everywhere I have /var/www/cgi-bin, you'll have to change it to /home/username/public_html/cgi-bin, etc. By the way, putting your scripts under your document root is a bad idea, from a security point of view... but that's a whole different topic...

    You can check each part by using "perl -c" to check for syntax errors. It will also let you know if it can't find a library. So do something like this:

    cd /var/www/cgi-bin/WebApp/libs/MyLib perl -c Simple.pm perl -c Login.pm cd /var/www/cgi-bin/WebApp perl -c simple.pl

    You may need to install a module or two if you're missing something. If you install them using "sudo cpan" then everything should "just work". If you install modules into a custom locations, for example "~/libs", then you'll have to add a "use lib '~/libs';" to any .pl or .pm file that needs a module installed in the "~/libs" directory.

    Okay - this post has gotten pretty long, so I'm going to stop for now. Give these suggestions a try and let me know how it goes. If you're still getting errors, provide more info and I'll take another look.

      I got it working!

      The problem was the modules path.

      Thank you for your concise instructions both in the tutorial and here regarding my installation problems.

      I began anew. Since I had saved the scripts with the line numbers deleted I copied and pasted in the code into the appropriate files. When I checked I kept getting error messages. This was one of the last:

      Simple.pm

      Base class package "CGI::Application" is empty. (Perhaps you need to 'use' the module which defines that package first.) at /home/username/public_html/cgi-bin/WebApp/libs/MyLib/Login.pm line 4 BEGIN failed--compilation aborted at /home/username/public_html/cgi-bin/WebApp/libs/MyLib/Login.pm line 4. Compilation failed in require at (eval 1) line 3. ...propagated at /usr/lib/perl5/5.8.8/base.pm line 85. BEGIN failed--compilation aborted at Simple.pm line 5.

      Login.pm

      Base class package "CGI::Application" is empty. (Perhaps you need to 'use' the module which defines that package first.) at Login.pm line 4 BEGIN failed--compilation aborted at Login.pm line 4.

      simple.pl

      Base class package "CGI::Application" is empty. (Perhaps you need to 'use' the module which defines that package first.) at /home/username/public_html/cgi-bin/WebApp/libs/MyLib/Login.pm line 4 BEGIN failed--compilation aborted at /home/username/public_html/cgi-bin/WebApp/libs/MyLib/Login.pm line 4. Compilation failed in require at (eval 1) line 3. ...propagated at /usr/lib/perl5/5.8.8/base.pm line 85. BEGIN failed--compilation aborted at /home/username/public_html/cgi-bin/WebApp/libs/MyLib/Simple.pm line 5. Compilation failed in require at simple.pl line 5. BEGIN failed--compilation aborted at simple.pl line 5.

      After uninstalling and reinstalling the "CGI::Application" several times and changing the order of the 'use lib' lines. Then I looked for

      /usr/lib/perl5/5.8.8/base.pm line 85
      (which didn't exist). While doing that I Googled for similar error messages and solutions.

      I had begun my response to you when I thought that I would try to manually delete the CGI::Application file, using the file manager, before reinstalling. I couldn't find it and when I eventually did I discovered the CGI::Application in a different 'library'. So I tried the new library path and that's what worked.

      It seems that my host, which auto-installs all modules from cpan, separates the requested modules and the pre-installed modules. Maybe that's standard.

      Again, I appreciate your help.

        Great! I'm glad you finally got it figured out.

        As a follow up, here's something you might consider as you continue to develop your site: most of the modules used in my tutorial are "pure perl" - the only exception (I think) is DBI, but that usually comes preinstalled on most systems. For pure-perl stuff, I much prefer to have my own library. It's really nothing more than just another directory with perl files inside. So, I have a local development machine with cpan setup to install modules to a local ~/libs directory. Then, when I'm ready to "go live", I copy all my web files to my production host, including my ~/libs directory of perl modules (just tar/zip it all up). That way I know I have the exact same version of each module on both the development machine and the production machine. You have no way of knowing when your host might decide to update stuff on you without warning, and this can cause problems. It also makes it easier on you if you need to move the site to a new host, or install the web app on multiple hosts, etc.

        Good luck!

Re: Authentication Script Path Problems
by Anonymous Monk on Sep 12, 2010 at 07:59 UTC
    Here are the error messages for the different pages

    mydomain.com/cgi-bin/WebApp/Simple.pl
    Can't locate MyLib/Simple.pm in @INC (@INC contains: /home/username/public_html/cgi-bin/WebApp/libs /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at simple.pl line 5.
    mydomain.com/cgi-bin/WebApp/
    attempt to invoke directory as script: /home/username/public_html/cgi-bin/WebApp/

    Then I copied /libs/MyLib/ to /MyLib/ and moved mydomain.com/cgi-bin/WebApp/ to mydomain.com/WebApp/

    mydomain.com/WebApp/Simple.pl gives me a blank page and this error.
    File does not exist: /home/username/public_html/index2_files, referer: http://www.mydomain.com/
    Thanks for the info on "shift" I had looked through several tutorials and assumed the "stripping of the first part of an array" would be the root directory of the URL (www.mydomain.com/ from www.mydomain.com/blah/blah/)

      I think you will need to learn a lot about both, webservers and Perl, and also about how to install modules and how to load them.

      You haven't shown any code, so it's hard to suggest changes to your existing code.

      As a first tip, do not put any Perl modules below the website document root. Make a parallel directory, say /home/username/lib, and put your modules there. Then modify your CGI scripts to look (roughly) like this:

      #!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use CGI; use lib '/home/username/lib'; use MyLib::Simple; my $q = CGI->new(); print $q->header; print "Hello";
      The Carp error messages are as follows:
      "use" not allowed in expression at simple.pl line 7, at end of line syntax error at simple.pl line 7, near "use MyLib::Simple" Execution of simple.pl aborted due to compilation errors.
      Line 7 is use MyLib::Simple; I commented that out and I get this message.
      syntax error at simple.pl line 9, near "my " Execution of simple.pl aborted due to compilation errors.
      Line 9 is my $webapp = MyLib::Simple->new(

        My code works for me. How do you think I'll be able to tell you anything about your code that you don't show?

        The Perl error message you get tells me that you have a typo somewhere before line 7.

        I think now is the time to take a step back and start with a very simple example, like the one I posted. First get that very simple example to work before you try on your main script again.