Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Passing params to a php file via perl cgi script.

by chaskins (Sexton)
on Jan 20, 2002 at 01:47 UTC ( [id://140135]=perlquestion: print w/replies, xml ) Need Help??

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

Good evening brothers. I'm writing a cgi script in perl that calls other web pages. Theses web pages are infact php scripts that need to take parameters in order to display the correct information. Currently my script work fine when I don't have to pass any parameters and the pages are shown as expected. But when I try to pass some needed parameters these just get ignore, i.e. via a url http://www.asite.com/result.php?mode=n&color=blue&age=18

What I am trying is;

$content = get("http://www.asite.com/result.php?mode=n&color=blue&age= +18") print "$content";
The way I call this via my broswer is;
http://www.mysite.com/cgi-bin/my_perl.cgi?url=http://www.asite.com/res +ult.php?mode=n&color=blue&age=18

Which seems to call the 'result.php' script but does not pass any parameters to it.
Any ideas why?
Am I using the right code?
Chris

Replies are listed 'Best First'.
Re: Passing params to a php file via perl cgi script.
by dmmiller2k (Chaplain) on Jan 20, 2002 at 02:12 UTC

    I'll bet what's happening is that your cgi script is being called with the following parameters:

    • url=http://www.asite.com/result.php
    • mode=n
    • color=blue
    • age=18

    As a result, when you attempt to call your get() subroutine with the value of the 'url' parameter, you are only passing 'http://www.asite.com/result.php'.

    To avoid this, you should replace the non-alpha (e.g., ? and &) characters in your url to their encoded equivalents:

    • & => %26
    • / => %2F
    • : => %3A
    • = => %3D
    • ? => %3F
    • etc. (look up the rest)

    Which would mean calling your cgi script with the following as a 'url' parameter:

    http://www.mysite.com/cgi-bin/my_perl.cgi?url=http%3A%2F%2Fwww.asite.com%2Fresult.php%3Fmode%3Dn%26color%3Dblue%26age%3D18

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
      "etc. (look up the rest)"

      I couldn't resist:

      my %codes = map {chr($_) => sprintf("%%%x",$_)} (0..255); print map {"$_ => " . $codes{$_} . "\n"} qw(& / : = ?);
      But, URI::Escape is much better.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        URI::Escape is the way to go if you're comfortable using modules ...

        dmm

        If you GIVE a man a fish you feed him for a day
        But,
        TEACH him to fish and you feed him for a lifetime
Re: Passing params to a php file via perl cgi script.
by trs80 (Priest) on Jan 20, 2002 at 03:18 UTC
    This might be a URL issue not a language issue. In Perl you can
    use URI::Escape;
    If you have the module installed
    then you can do this
    $link_for_html_page = uri_escape($raw_url);

    To give a link for an outgoing HTML page.
    or
    $link_for_get_function = uri_unescape($coming_from_html_page);

    To give you a valid URL for your get function.
    Is there something similar in PHP?

Re: Passing params to a php file via perl cgi script.
by Marcello (Hermit) on Jan 20, 2002 at 02:17 UTC
    According to HTTP specs, you should URL encode the parameters value, in this case the url parameter:
    sub url_encode { my $string = shift; $string =~ s/([^a-z0-9_.!~*'() -])/sprintf "%%%02X", ord($1)/egi; $string =~ tr/ /+/; return $string; }
    But if you type this url in a browser, it should be encoded for you. My guess is that your problem has something to do with url encoding in the browser or url decoding in your script.

      Another good approach. Although, as I suspect in this case, the URLs are being entered by hand into a browser.

      dmm

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://140135]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-16 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found