Click to See Complete Forum and Search --> : [RESOLVED] can't open file


yssirhc
04-03-2009, 12:27 PM
I wrote a simple login program & for some reason, it gets stuck at opening the file that contains the passwords. I checked the file's permissions and everyone has read, write, and execute privileges. I also tried adding "<" to the statement and changing the slashes to backslashes, but just got the same message.
Is there something wrong with this code? ....at least I know that my errorPage function works ;)

#! /usr/bin/perl
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use diagnostics;

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

### get datastring: ######################################
if ($ENV{"REQUEST_METHOD"} eq "POST")
{
read(STDIN, $datastring, $ENV{"CONTENT_LENGTH"});
}
elsif (exists $ENV{"REQUEST_METHOD"})
{
$datastring = $ENV{"QUERY_STRING"};
}
else
{
print "Offline execution detected.\n";
print "Please enter some data.\n";
$datastring = <>;
chomp $datastring;
print "== data accepted == HTML output follows ==\n\n";
}
### finished getting data ##################################

### splits form data into name/value pairs: ##################
$datastring =~s/%0D%0A/\n/g; #line breaks in text areas

@pairs = split(/&/, $datastring);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

$name =~tr/+/ /;
$name =~s/%([\da-fA-F]{2})/pack("C",hex($1))/eg;
$value =~tr/+/ /;
$value =~s/%([\da-fA-F]{2})/pack("C",hex($1))/eg;

if (exists $formHash{$name})
{
$formHash{$name} = $formHash{$name}.";".$value; #multiple select menus
}
else
{
$formHash{$name} = $value;
}
}
### done decoding ######################################

%access = ();

$submittedUser = $formHash{"user"};
$submittedPassword = $formHash{"password"};

#open password file
open(USER, "../data/bugUsers.txt") or &errorPage("Unable to open user/password file.");

#read file & store data in hash
while ($line = <USER>)
{
chomp $line; #remove newline character
if ($line ne "") #precaution - skip if line is blank
{
($user, $password) = split(/:/, $line, 2);
$access{$user} = $password;
}
}
close(USER);

#verify userID and password
if (exists $access{"$submittedUser"})
{
if ($submittedPassword eq $access{$submittedUser}) #success!
{
&redirect;
}
else #password wrong
{
&printHeader;
&invalidPassword;
&printLogin;
}
}
else #user not listed in file
{
&printHeader;
&invalidUser;
&printLogin;
}

Sixtease
04-04-2009, 03:32 AM
Examining $! (error message) could shed some light on the cause.
open(USER, "../data/bugUsers.txt") or &errorPage("Unable to open user/password file: $!");

yssirhc
04-06-2009, 10:49 AM
well now it also says "No such file or directory"

but it DOES EXIST!

I even changed it so that the file would be in the same folder as the cgi and it still can't find it!

Sixtease
04-06-2009, 10:53 AM
OK, try to print $ENV{PWD} -- i.e. check what's the working directory of the process. You may need to use an absolute address or adapt the relative address to $ENV{PWD}.

yssirhc
04-06-2009, 11:09 AM
I added print "$ENV{PWD}";
and I get an error saying that this is an uninitialized value. Is this the problem? How do I initialize it?

Sixtease
04-06-2009, 11:30 AM
It means that you don't have PWD among environment variables and thus you can't use relative addresses -- the script has actually no working directory. Open the file with an absolute address, that should do the trick.

yssirhc
04-06-2009, 12:04 PM
ok, I tried listing the file as the URL, which I can navigate fine to in my browser, & I tried listing the file starting with the drive letter. Both times it tells me that it's an invalid argument.

Is there something I need to set on the server?

winracer
04-06-2009, 01:28 PM
did you try changing the code


#open password file
open(USER, "../data/bugUsers.txt") or &errorPage("Unable to open user/password file.");



to


#open password file
open(USER, "/absolutePATH/data/bugUsers.txt") or &errorPage("Unable to open user/password file.");



where absolutePATH is your absolute path to the file. something like /home/something/data/bugUsers.txt or could be something different.

winracer
04-06-2009, 01:50 PM
if you are not sure of your absolute path then copy this to a file call it test.pl



#!/usr/bin/perl


print "Content-type: text/html\n\n";
print "<html>\n" ;
print "\n" ;
print "<head>\n" ;
print "<title>$ENV{'SERVER_NAME'} Server Path Test Script</title>\n" ;
print "</head>\n" ;
print "\n" ;

print "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#000000\" vlink=\"#000000\" alink=\"#000000\">\n" ;
print "\n" ;

print "<center><font face=Verdana size=4><b><br>\n" ;
print "Server Path Test Help Script</b></center>\n" ;

print "<font face=Verdana size=4><b><br><br>\n" ;
print "Path and URL information:</b><br><br>\n" ;
print "Your path to Perl is correct<br>\n" ;
print "Your server is running Perl version: $]<br>\n" ;
print "Your domain name is: $ENV{'SERVER_NAME'}<br>\n" ;
print "The complete system absolute PATH to this script is: $ENV{'SCRIPT_FILENAME'}<br>\n" ;
print "The URL of this script is: http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}<br>\n" ;

print "</body>\n" ;
print "</html>\n" ;






Upload this script into the directory you are running your script
CHMOD the test.pl 755
Run the script by typing the URL in your browser
You should now see your absolute PATH and URL information


or you can use this path test

http://support.microsoft.com/kb/245225

yssirhc
04-06-2009, 02:43 PM
When I ran that test script, there was nothing listed after "The complete system absolute PATH to this script is:" It was just blank.

The Perl path is right. I'm running version 5.01.
The domain & URL listed are both correct.

But there is no absolute path listed :confused:

winracer
04-06-2009, 02:59 PM
? try this one. I have used it in the past and look at Document Root:


name it pathtest.pl




#!/usr/bin/perl

#use CGI::Carp "fatalsToBrowser";


### *** IMPORTANT FOR UNIX *** ###

### *** Make sure the path to PERL above is correct *** ###

#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#
#
# Program is copyright by Ranson's Scripts all rights reserved
# Email - support@rlaj.com
#
# Program Name ------- pathtest.pl
# Program Version ---- 2.0
# Configuration File - none
#
#
#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#


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

$Perl_Version = "Perl Version $]";

if (-d "C:/" || $^O eq "MSWin32") {
$windows = 1;
$platform = "WINDOWS";

} elsif (-e "/etc") {
$unix = 1;
$platform = "UNIX";

} else {
$unix = 1;
$platform = "UNKNOWN PLATFORM";
}

foreach $key (keys(%ENV)) {
if ($key =~ /cookie/i) { $key = "" }
$My_Key .= "<TR><TD>$key<TD> $ENV{$key}</TD></TR>\n";
if ($ENV{$key} =~ /\w:\\.+\\pathtest.pl/ && $windows eq "1") { $path = "$ENV{$key}"; }
}
if ($0 =~ /(.*)\/pathtest.pl/) { $path = "$1"; }
elsif ($ENV{'SCRIPT_FILENAME'} =~ /pathtest.pl/) { $path = "$ENV{'SCRIPT_FILENAME'}"; }
if ($ENV{'DOCUMENT_ROOT'} ne "") { $document_root = "<B>Document Root:</B> <FONT COLOR=FF0000>$ENV{'DOCUMENT_ROOT'}</FONT>"; }

$path =~ s/pathtest\.pl//;
$path =~ s/\\$//;
$path =~ s/\/$//;


if ($platform eq "UNIX") {
$Sendmail = `whereis sendmail`;
}

@Sendmail = split(/\s+/, $Sendmail);

foreach $place(@Sendmail) {
if($place !~ /\./) {
push(@SendMail, "$place \&nbsp\; ");
}
}

if ($platform eq "UNIX") {
$Path_to_Sendmail = "<B>sendmail</B> = <FONT COLOR=F04E00 FACE=arial>@SendMail</FONT><P>";
}


### *** :) *** ###



print <<"EOT";

<HTML>
<HEAD>
<TITLE>Path Test by RLAJ.COM</TITLE>
</HEAD>
<BODY BGCOLOR=FFFFFF TEXT=000000>
<TABLE BORDER=0 BGCOLOR="#FFFFFF" CELLPADDING=2 CELLSPACING=0 Width=100%>
<TR>
<TD><B><FONT FACE="Arial" COLOR="000080" SIZE="+3">&nbsp; &nbsp; Path and Environment Test</FONT></B></TD>
<TD Align=right><FONT SIZE="+1"><A HREF="http://www.rlaj.com">Ranson's Scripts</A></FONT><BR><A HREF="mailto:Support\@rlaj.com">Support\@rlaj.com</A></TD>
</TR>
</TABLE>
<P>
<TABLE BORDER=0 BGCOLOR="000080" CELLPADDING=20 CELLSPACING=0>
<TR><TD>
<CENTER>
<TABLE BORDER=0 BGCOLOR="#FFFFFF" CELLPADDING=9 CELLSPACING=0>
<TR><TD>
<BR>
<B>The operating system is:</B> <FONT COLOR=#F04E00 FACE="arial">$platform</FONT> &nbsp; $Perl_Version
<P>
<B>The path for this file is:</B> <FONT COLOR=#F04E00 FACE="arial">$path</FONT>
<P>
$document_root
<P>
$Path_to_Sendmail
</TD></TR>
</TABLE>
</CENTER>
</TD></TR>
</TABLE>
<BR><BR><BR>
<B><FONT FACE="Arial" COLOR="000080" SIZE="+1">Environment Variables</FONT></B>
<BR><BR>
<TABLE BORDER=0 BGCOLOR="#FFFFFF" CELLPADDING=2 CELLSPACING=0 Width=100%>
$My_Key
</TABLE>

EOT

print "<BR><BR><BR><BR></body></html>";


exit;

### *** *** ###



I downloaded this one at http://www.rlaj.com/scripts/free_perl_cgi_scripts.html

yssirhc
04-06-2009, 03:27 PM
Document root was not listed either. :eek:

The absolute path is supposed to be everything after the domain name though, right? so if the file is at www.abc.com/application/data/users.txt then the absolute path would be /application/data/users.txt.

I tried putting it in that way, and instead of the invalid argument I was getting before I just get the same old "no such file or directory" again.

winracer
04-06-2009, 03:53 PM
Document root was not listed either. :eek:

The absolute path is supposed to be everything after the domain name though, right? so if the file is at www.abc.com/application/data/users.txt then the absolute path would be /application/data/users.txt.

I tried putting it in that way, and instead of the invalid argument I was getting before I just get the same old "no such file or directory" again.




the absolute path is the path on the server, NOT the url. are you using a hosting company? or do have your own server?

you can try this php script


<?php
$path = getcwd();
echo " Your Absolute Path is: ";
echo $path;
?>


Absolute vs. Relative Server Paths


Absolute Paths

Paths are used ON THE SERVER. Usually inside a script. The absolute path is the "full path". It is the path that contains the top directory (usually home) - the username - the root directory (usually public_html) - the sub directory (or file name) For example
/home/username/public_html/cgi-bin/test.cgi
This is the absolute path to the text.cgi file in the cgi-bin directory

BTW: /home/username/ would put you in the root of your account. An section that is NOT accessible via WWW (browser) - this is where mail files, password files, etc (other protected files) are stored on the server. You seldom need this path since you are working with web pages and need path info to the public_html (www) directory ( your root directory FOR the web)

Example of other useful path information shows the path to shared files on the server. For example the path to perl on the server:
/usr/bin/perl
Path info of this type are supplied by the host when you need it for scripts. In most cases, the cPanel account displays this info for the client. Along with path to 'date' 'sendmail' etc that are required by many scripts.

Relative Path

Relative Path may be a misnomer. It is actually like the relative url i.e. it is normally 'relative' to the public_html directory (which is the root directory for the web site / not the root of the server). However, it could be relative to the upper directories (home). But in actually use, the relative path is seldom used - even in scripts. And if it is required, the script writer may NOT use the name - relative path. It may be referred to as a non-absolute path, a direct path, a path to a file, etc. Bottom line - is that scripts that refer to the relative path - really mean the path to files below the www root (usually public_html). So it is usually the same as the relative URL. And in actual practice it's not 'usually called' a relative path. It's more likely to be referenced as the 'path to files' (or sumsuch :)


Relative vs. Absolute URL

Relative URL

The most useful URL path is the relative path! The relative URL points to a file or directory in relation to the present file or directory (folder). Relative URL's help in web site maintenance. It is easy to move a file from one directory (folder) to another, or a web site from one domain name to another. And you don't have to worry about updating the link(s) or the src (img) path(s).

Keep It Simple Stupid (KISS): If you are loading a file / image / etc. that is in the same directory as the source file, just use the file name. If the html file is in the top directory and need to link to an image in that directory, the relative URL would be <img src="top.gif" />. This is the very basic - simple example of a relative path because the file and directory are both in same place. In other words, no path info is required if the files are in the same directory. (very convenient and easy to manage).

For next level: To access a sub-directory below the top directory - do NOT use the preceding slashes. For example, if you Home page is the index.html and the logo (ibdhost.gif) image is in the images directory, use:
<img src="images/ibdhost.gif" /> (note the omission of the slash :)

For more details - Here is example on a web server (where the web root is public_html)

Root (public_html)
.......index.html
.......top.gif

....images
......ibdhost.gif

....help
......path
........index.php (this page)

For this page, the current page is the index.php file inside the path directory - inside the help directory. Therefore, the relative path to this page is
/help/path/index.php

Then to load the ibdhost.gif image (top left of this page), the relative path to the image is
../../images/ibdhost.gif
Which means the ibdhost.gif image is in a directory two levels up from this index.php file - then down into the 'images' directory.

The two dots .. instruct the server to move up one directory. Therefore two sets of ../../ moves up two levels to the root directory (public_html) - then opens the images directory and loads the ibdhost.gif file.

Of course, if the image had been in the help directory the relative path would be
../help/imagename.jpg

Relative path is really easy to understand just by reviewing the basic definition:
A relative URL points to a file or directory in relation to the present file or directory.

Absolute URL

An absolute URL is the URL most people already understand. It is the complete path including the domain - file name. Example: http://www.ibdhost.com/images/logo.gif specifies an image file (logo.gif) located in the images directory, for the www.ibdhost.com domain. This type of URL is what your must use when you want to link (or load) a file that is on another server.

Another example is the absolute URL of this page (which is also the Address / Location of the file) = http://www.ibdhost.com/help/path/index.php




I found the information here.
http://www.ibdhost.com/help/path/

yssirhc
04-06-2009, 04:27 PM
So the absolute path is really just MORE than the URL - going up further. I'm using my own server. Is there a way I can set it up to use relative addresses?

I'm beginning to think it's currently set up in an untraditional way since neither of those scripts are listing an absolute path when I run them. PHP isn't downloaded on that server, so I can't run that one.

Perl is on the C drive. When I type the domain name by itself, I'm accessing E:\wwwRoot\default.htm and the application I'm trying to run is at E:\webApps\bugsdead2.0\cgi

winracer
04-06-2009, 04:35 PM
I would try


#open password file
open(USER, E:\wwwRoot\data\bugUsers.txt") or &errorPage("Unable to open user/password file.");


or


#open password file
open(USER, E:\webApps\bugsdead2.0\cgi\data\bugUsers.txt") or &errorPage("Unable to open user/password file.");




or maybe


#open password file
open(USER, /wwwRoot/data/bugUsers.txt") or &errorPage("Unable to open user/password file.");




or something like that. hope this helps

yssirhc
04-07-2009, 09:49 AM
I had tried using the drive letter originally and it was giving me an invalid argument error. That last one worked though:

open(USER, /wwwRoot/data/bugUsers.txt") or &errorPage("Unable to open user/password file.");

And then I was able to change it to:

open(USER, /webApps/bugzdead2.0/data/bugUsers.txt");

so that the file would be where I wanted it.
:)
thanks to both of you for all of your help!