Gossamer Forum
Home : General : Perl Programming :

How do I do this?

Quote Reply
How do I do this?
Typically, when you take in request for a script, it goes like

script.pl?something=something

or

script.pl?something

Is their a way to take it in like

script.pl/something

So, you would basically replace ? with /

I know this is possible, but I can't find out where this info is.

Thanks Smile
Quote Reply
Re: How do I do this? In reply to
One more step,

What I would like to do this this.

script.pl/data/data2

Is their a form parse sub routine that I can use to accept data and data2 so that I can use them while using / instead of ?

Thanks Smile
Quote Reply
Re: How do I do this? In reply to
Try this code
Code:
#Create list
@info = "$ENV{'PATH_INFO'}";

#Split List
foreach $info (@info)
{
#Split list into sections divided by a /
($slash, $script_name, $data, $data2) = split(/\//,$info);
}
$data is assigned the value of the frist data in script.pl/data/data2.
$data2 is assigned the value of data2 in script.pl/data/data2.

I hope this helps.

-Matt
Quote Reply
Re: How do I do this? In reply to
I would also like to know this.
For example when you search the www.download.com's search engine, it will return:
Code:
http://download.com/pc/list/1,339,0-a-0-0-e-1,00.html?tag=st.dl..srch.list&search=FREEWARE&queryType=quicksearch

How do they do that?
What are the advantages? Disadvantages?

------------------
find.virtualave.net
Quote Reply
Re: How do I do this? In reply to
Gentlemen,

Parsing the PATH_INFO (afaik, you're better off using $ENV{'REQUEST_URI'}) is all well and good, but you probably won't get that far, because the webserver will try and serve a file from the requested directory.

Using inbuco's example of "script.pl/data/data2", most webservers will treat "script.pl" as a directory, and try and serve an index file from the last directory in the requested uri, in this case "data2".

You need to tell the webserver that everything under the directory that script.pl is in should be handled by the script.pl program.

On Apache, the easiest way to go about this is to use the invaluable mod_rewrite module, like this:

RewriteEngine on
RewriteRule ^sites/3.* /cgi/sites/3/index.cgi [T=application/x-httpd-cgi]

In this example, I placed an .htaccess file in the document root directory of my site, and this little rewrite tells the webserver that any requests for "/sites/3" are to be handled by the script "index.cgi" in the directory "/cgi/sites/3". The final bit of the rewrite rule tells the webserver that this is a cgi script.

This script then parses the REQUEST_URI environment variable and delivers the appropriate content. This is particularly handy for delivering automated dynamic navigation tools and pages from a database, while still letting the search engines index the site.

Unfortunately, I can't give you a URL to visit this example because I've removed the site, but I can tell you it works *very* well.

Finally, to Pasha, C|Net and their associated sites use all that data in the query_string to deliver dynamic content in much the same way, only *much* more complicated. AFAIK, nearly all of their content is dynamically delivered, which is why they use weird filenames aswell. They don't just parse the query_string, they parse the filenames too.

The advantages are obvious, it gives better delivered content, and can be tailored to users if needs be, and the whole site can be changed completely in a matter of minutes. The disadvantages are not so obvious, but the main one is performance. Because so much content is delived dynamically, it places a higher load on the server. But there's little doubt that C|Net and other large sites use scripting languages that are precompiled into the server, such as ASP or PHP3.

I've been using PHP3 for a short while now, and much as I love Perl, PHP3 can be a hell of a lot easier to use for some things, in some cases skipping two or three steps to do the same job. Granted, Perl is ideally suited to some applications, but using the example above, it's a hell of a lot easier to build a dynamic site like this in php3 that it is with Perl, and in benchmark tests it's faster too. Plus, once you get the hang of it, working with SQL databases is childs play, again speeding everything up.

My advice is to keep an eye on PHP4. The first beta version has just been released, complete with the Zend engine. ASP is a little puddy-tat compared to this...

Hope this helps,
adam

[This message has been edited by dahamsta (edited July 29, 1999).]
Quote Reply
Re: How do I do this? In reply to
Ok, here is the deal.

So, if I use the following .htaccess
RewriteEngine on
RewriteRule ^sites/3.* /home/htdocs/whatever/directory/index.cgi [T=application/x-httpd-cgi]

and place a file called index.cgi in that directory

that every time I call

http://whatever/directory/data1/data2

that it will be called with the index.cgi file?

Becuase that's exactly what we are looking for BUT, everytime I try it, I get a 500 error with the htaccess file in the directory.

Or is their another way to do this.

Becuase, we would really like to do this

http://whatevery/directory/data1/data2

With directory really being the directory with the index.cgi file and everything called from that directory will be processed under the script.

Thanks for the help Smile
Quote Reply
Re: How do I do this? In reply to
What is the difference between using

$ENV{'REQUEST_URI'}

and

$ENV{'PATH_INFO'}

I am still not clear on that one.

Quote Reply
Re: How do I do this? In reply to
First of all, using request_uri is, as I said, as far as I know. Anytime I've seen something like this done, it's been with request_uri, and it works for me, so I'm happy. But maybe I'm wrong! Smile Alex would be the man to explain the difference rather than me, I still consider myself an amateur. With the speed the web is developing, I think I always will be!

Anyway, before you go any further, I'm talking about Apache here, so if you're running another webserver, this (probably) won't apply. Next, check to see if mod_rewrite is compiled into the server. If you don't know how to do that, email your server administrator and ask him. Also check to see if you're allowed to use .htaccess files, because some sysadmins turn them off.

Also, I would advise you to have a look at the official documentation on mod_rewrite at www.apache.org/docs/mod/mod_rewrite.html and the invaluable guide written by Ralf Engelschall, the writer of mod_rewrite at www.engelschall.com/pw/apache/rewriteguide/ , because it's one of the more complicated of the modules. It's also one of the most powerful, or "bloody handy" in dahamsta speak. And Ralf has put some nice examples on that webpage.

Ok, so for simplicity's sake, I'll use your example:

http://whatever/directory/data1/data2

So you want everything under /directory/ to be processed by index.cgi right? Well, I'm guessing that the reason that you're getting an error is because either a) you're setting up a loop with mod_rewrite (it's trying to rewrite to /directory/, trying to rewrite to /directory/ etc etc) or b) there's a problem with the script itself.

The easiest way to go about it is to create a .htaccess file in your document root directory, with something like this:

RewriteEngine on
RewriteRule ^directory.* /cgi-bin/script.cgi [T=application/x-httpd-cgi]

And stick your script into your cgi-bin. You see, it doesn't *matter* where you keep the actual script, because the user will never know that the page is actually being processed by it, to them it will look like they're viewing a regular file in a regular directory. The directory itself doesn't even have to exist, matter of fact I wouldn't recommend you create one first day, because you'll just confuse the issue!

So a simple script to test this would be like:

Code:
#!/usr/bin/perl

# This'll report errors back to the browser.
use CGI::Carp qw/fatalsToBrowser/;

# Now split the uri into an array.
@dirs = split("/",$ENV{REQUEST_URI});

# And splice it to remove the first two elements.
# Remember that there's a forward slash at the start too,
# so there will be an empty entry in the first element.
# The second element is of course /directory/, which we
# don't need.
splice(@dirs,0,2);

# Print a header
print "Content-type: text/html\n\n";

# Now print out each element in the array.
# You should get back each dir requested
# under /directory/
foreach $dir (@dirs) {
print "$dir<br>";
}

exit;

So to use this in a useful manner, you would then assign the data you got back from the directory names into your own variables. Like in a shopping cart, if your request_uri was /directory/item1/ you could use that like:

$item = $dirs[0];

And "item1" would now be in $item. And you could use that anyway you wanted then, maybe to pull details from a database, or to parse a file with that products details.

I know the whole thing is a bit complicated at the start, but when you get used to it, you'll realise how invaluable it is, particularly on large sites, or in product catalogues and the like. Set it up to interface with a powerful program like Links or dbMan, or better still, an SQL database, and you've got a very powerful tool in your hand. So to speak. Smile

Cheers,
adam

[This message has been edited by dahamsta (edited July 29, 1999).]
Quote Reply
Re: How do I do this? In reply to
Still having problems,

I am running Apache 1.2.4,

I created a directory called directory and put the following script in the directory named index.cgi

#!/usr/bin/perl
# This'll report errors back to the browser.
use CGI::Carp qw/fatalsToBrowser/;
# Now split the uri into an array.
@dirs = split("/",$ENV{REQUEST_URI});
# And splice it to remove the first two elements.
# Remember that there's a forward slash at the start too,
# so there will be an empty entry in the first element.
# The second element is of course /directory/, which we
# don't need.
splice(@dirs,0,2);
# Print a header
print "Content-type: text/html\n\n";
# Now print out each element in the array.
# You should get back each dir requested
# under /directory/
foreach $dir (@dirs) {
print "$dir<br>";
}
exit;


I created an .htaccess file with the following
RewriteEngine on
RewriteRule ^directory.* /directory/index.cgi [T=application/x-httpd-cgi]

And still get a 500 error across the board.

I know the script works and the .htaccess option works.

This makes no sense, HELP!!!
Quote Reply
Re: How do I do this? In reply to
################################
I would also like to know this.
For example when you search the www.download.com's search engine, it will return:

code:
--------------------------------------------------------------------------------

http://download.com/pc/list/1,339,0-a-0-0-e-1,00.html?tag=st.dl..srch.list&search=FREEWARE&queryType=quicksearch

--------------------------------------------------------------------------------

How do they do that?
What are the advantages? Disadvantages?

####################################

This is done using Server Side Includes which contain a call to a cgi script. Any name=value pairs tacked on to the URL of the html/shtml file will be passed to the embedded cgi script that is called in that page as if you were calling it directly. For example, if you visit my site at http://www.pro-stuff.net and click on any link under the "Downloads" section, this is actually calling an shtml file with a bunch of parameters tacked on. The shtml file contains three lines:

1. An include which inserts my header.
2. Call to the cgi script which generates the tables for the downloadable files.
3. An inclide which inserts my footer.

Basically I use SSI for templates so I can change the look and feel of my site by only editing 2 files.

To take that a step further, the header and footer files are also shtml which include SSI calls to insert my banner code and other dynamic content. In short, Links is pretty much the only significant section of my site that contains static html. But when it's built, it uses the headers and footers too. Smile

Quote Reply
Re: How do I do this? In reply to
 
Quote:
I created a directory called directory and put the following script in the directory named index.cgi

You're not reading my posting inbuco! I said, put the .htaccess file in your document root, which is lowermost directory on your site that can be reached from the Internet.

Also, you have to realise how illogical is is to place the cgi script in /directory/. With the RewriteRule you have, you're telling the server to rewrite every under /directory/ to /direcotry/index.cgi. Obviously, that's not gonna work, because it's just going to keep looping around. In fact that process could be still running on the server, so you should delete /directory/ altogether.

Try reading my posting again.

adam