Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Incorrect URL given.

Posted on 2007-08-01
13
Medium Priority
?
789 Views
Last Modified: 2013-12-25
Hi guys

I have a problem with the following script.

The search form is searcher.html and is:

==============================================================  searcher.html
<form action="/cgi-bin/searching.cgi" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>

============================================================== searching.cgi
#!c:/perl/bin/perl.exe           
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
      
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
       return if ($_ =~ /^\./);                        
       return unless ($_ =~ /\.txt/i);    
       stat $File::Find::name;
       return if -d;
       return unless -r;
                                     
       open(FILE, "< $File::Find::name") or return;
      my $string = <FILE>;
      close (FILE);
                                    
      return unless ($string =~ /\Q$query\E/i);
                                    
      my $page_title = $_;            
       if ($string =~ /<title>(.*?)<\/title>/is)      {
           $page_title = $1;
       }
       print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n"; #Here we output our link.
      },
        '/wamp/'
        );
                    
        print "</ol>\n";
      print end_html();

================================================================

What happens is that the script returns the files with the search criteria in correctly. For example,
If i enter on the form 'test' for the text to find.
The problem is when I click on one of thos results, it goes to the wrong url.

If one of the results is listed as:

test.txt

And I click on this result in the browser, it takes me to:

http://localhost/wamp/www/hi/test.txt

which give me 'Web page unavailable".

The correct path should be:

http://localhost/hi/test.txt

in which case, it is displayed correctly.


------------------------------------------------------
My document root is c:\wamp\www
Search.html is in c:\wamp\www
Searching.cgi is in c:\wamp\apache2\cgi-bin

Any help appreciated.
0
Comment
Question by:Simon336697
  • 8
  • 4
13 Comments
 
LVL 8

Assisted Solution

by:Perl_Diver
Perl_Diver earned 680 total points
ID: 19614369

    * $File::Find::dir is the current directory name,
    * $_ is the current filename within that directory
    * $File::Find::name is the complete pathname to the file.

instead of:

print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n"; #Here we output our link.

you could:

print "<li><a href=\"http://localhost/hi/$_\">$page_title</a></li>\n"; #Here we output our link.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19615821
Hi Perl Diver!

Thank you for that!
It seems to work, and returns entries in sub directories, but I dont know how to get the right href when clicking on a result, where the result is located in a subdirectory. It still points to the current directory.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19615942
my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
print "<li><a href=\"$target\">$page_title</a></li>\n";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Simon336697
ID: 19615970
It seems like if I do the following..

       print "<li><a href=\"http://localhost/$_\">$page_title</a></li>\n"; #Here we output our link.
      },
        'c:\\wamp\\www\\'
        );

It returns results in all folders starting at c:\wamp\www, and subdirectories.

However the output in the web page for the results, if I click on a file returned that is in a subdirectory eg. c:\wamp\www\hi\hi\testing.txt

The link says it is in http://localhost/testing.txt,
instead of what it should be, which is:
http://localhost/hi/hi/testing.txt

Im getting really confused here.
Either Im just a bit thick or this is tricky or both.

It will return only entries that are in the c:\wamp\www\hi folder, but not subfolders.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19616028
Hi ahoff!
Thank you mate for this.
I tried your change...

my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
print "<li><a href=\"$target\">$page_title</a></li>\n";
      },
     '/wamp/www/'
        );
                   
        print "</ol>\n";
      print end_html();

===================================
Results are returned as follows..

no.txt
nnn.txt
test.txt
ol.txt

If I hover over any of the above, they point to:

http://localhost/cgi-bin/hi/nnn.txt                (should be http://localhost/hi/nnn.txt)
http://localhost/cgi-bin/hi/god/ol.txt            (should be http://localhost/hi/god/ol.txt)
http://localhost/cgi-bin/no.txt                     (should be http://localhost//no.txt 

The location of the cgi script being run here on disk is c:\wamp\apache2\cgi-bin.
Somehow it looks like its putting in the current location of where the script is being run from??
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19616071
my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
$target =~ s#/cgi-bin##;
print "<li><a href=\"$target\">$page_title</a></li>\n";


# to be improved in many ways
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19616105
Thanks ahoff for all your help.

I think this question is just really messy, and I might just allocate points now.
There has to be an easier way to do this I think :>)
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1320 total points
ID: 19616448
($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
print "<li><a href=\"$target\">$page_title</a></li>\n";

# depends on what you mean by "easier": easier to understand, easier to read, easier to execute, ...
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19620200
ahoff!
Youre the best......it WORKS!!

Here is the code that does it...thank you.

================================================== search.html
<form action="/cgi-bin/searchcgi.cgi" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>
=================================================== search.cgi
#!c:/perl/bin/perl.exe          
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
     
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
       return if ($_ =~ /^\./);                        
       return unless ($_ =~ /\.txt/i || /\.xls/i);    
       stat $File::Find::name;
       return if -d;
       return unless -r;
                                     
       open(FILE, "< $File::Find::name") or return;
      my $string = <FILE>;
      close (FILE);
                                   
      return unless ($string =~ /\Q$query\E/i);
                                   
      my $page_title = $_;            
       if ($string =~ /<title>(.*?)<\/title>/is)      {
           $page_title = $1;
       }
       
       my $target=$File::Find::name;

($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
#print "<li><a href=\"$target\">$page_title</a></li>\n";
print "<li><a href=\"$target\">$page_title</a></li> $File::Find::dir\n";
      },
        '/wamp/www/'
        );
                   
        print "</ol>\n";
      print end_html();
============================================================ End of file.
Usage example:
http://localhost/search.html
In the text box, enter "test" to search for...Submit Query..

For the query test, these results were found:

nnn.txt /wamp/www
no.txt /wamp/www
test.txt /wamp/www
nnn.txt /wamp/www/hi
test.txt /wamp/www/hi
New Microsoft Excel Worksheet.xls /wamp/www/hi/New Folder/New Folder/yeppppppy
nnn.txt /wamp/www/hi/gothere

Thanks so much ahoff!
So in the below, you set the filename path to:
($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
Are you substituting on the right hand side?

0
 
LVL 1

Author Comment

by:Simon336697
ID: 19620315
Sorry in the above, in the search.html,
<form action="/cgi-bin/search.cgi" method="post">

Last question:
Is there any way to slightly modify this, so that instead of returning the files that contain the query string entered in the text box in the form, it returns the FILENAMES that have the text entered in the query string as part of their filename.
So, for example:

Search (form)                                       Results (these files have the search as part of their filename)
Search for "test" --------------------->   test.txt
                                                              thisisatest.xls
                                                              justtesting.doc
                                                             testit.txt
At the moment,
Search (form)                                       Results (these files have the search IN the file itself)
Search for "test" --------------------->   nn.txt
                                                             ooo.txt
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19622933
> Are you substituting on the right hand side?
yes (using the left), then assign to the left

don't understand you last question, but it's either a simple print somewhere, or please open a new question with the code not working
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19627633
Sorry ahoff!
Ill post another question.
And thankU for your help on this one :>)
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19627636
ThankU Perl Diver and ahoff...you guys are the best.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question