开发者

Can I redirect to a PHP page from a Perl CGI script?

I am working with a site that uses an outside source to work with payment transactions, one of the prerequisites is that on success a CGI script is called.

What I am wa开发者_C百科nting to know is it possible to do a redirect to a PHP page with the CGI script and have the PHP detect that it has been loaded via a Perl redirect, I currently have this is in my Perl.

#!/usr/bin/perl
#
# fixedredir.cgi

use strict;
use warnings;


my $URL = "http://www.example.com/";

Location: $URL;


The standard way to do CGI programming in Perl is using the CGI.pm module. A redirect can be coded like this (not sure about the syntax).

 #!/usr/bin/perl

 use strict;
 use warnings;
 use CGI;

 my $URL = "http://www.example.com/";

 print CGI::redirect($URL); 

I does not matter if the target page is PHP or whatever.

The other page can't know for sure if it came from a "perl redirect" (actually the concept is rather absurd). If you have control over the other PHP page, you can set some convention and pass a parameter in the query string.

 my $URL = "http://www.example.com/x.php?redirectFromPerl=1";

Update : As others have commented, the question really makes little sense - and some statements ("on success a CGI script is called") suggest some basic misunderstandings about the basics of dynamic web pages and CGI. My example code shows how to make a redirect in Perl CGI, but it's probable that the original question (and hence the answer) is inadequate for the real scenario.


If this is an outside source that calls CGI script, how would it know if it is a PHP page or a CGI script on your site, that it calls? For me, you can just set up this payment service to call your PHP page and avoid any requirement for redirection. Or is there something I haven't understood?

If it looks for a specific URL on your site, e. g. /cgi-bin/trigger.cgi, you can trick it with .htaccess put in cgi-bin, like this:

<Files trigger.cgi>
    SetHandler application/x-httpd-php
</Files>


the easiest way would be to add a get variable to your URL:

my $URL = "http://www.example.com/?from=perl"; 

then your PHP doesn't have to do anything fancy. However, you can look at the $_SERVER['HTTP_REFERER'] value and parse the referring script's name. But since this value is set by the user agent, it is not necessarily reliable or present.


To redirect a page to another use the following method.

use CGI::Session;
use CGI::Session::Plugin::Redirect;
my $session = new CGI::Session();
print $session->redirect('http://exsample.com/redirect-path/');

Search search.cpan.org for more details about the session module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜