简体   繁体   English

我可以从Perl CGI脚本重定向到PHP页面吗?

[英]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. 我正在一个使用外部资源处理付款交易的网站,前提条件之一是成功调用CGI脚本。

What I am wanting 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. 我想知道的是,可以使用CGI脚本重定向到PHP页面,并使PHP检测到它已通过Perl重定向加载,我目前在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. 在Perl中进行CGI编程的标准方法是使用CGI.pm模块。 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. 目标页面是PHP还是其他对象都没关系。

The other page can't know for sure if it came from a "perl redirect" (actually the concept is rather absurd). 另一页不能确定它是否来自“ perl重定向”(实际上,这个概念很荒谬)。 If you have control over the other PHP page, you can set some convention and pass a parameter in the query string. 如果您可以控制另一个PHP页面,则可以设置一些约定并在查询字符串中传递参数。

 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. 更新 :正如其他人所评论的那样,该问题确实没有什么意义-某些陈述(“成功地称为CGI脚本”)表明对动态网页和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. 我的示例代码显示了如何在Perl CGI中进行重定向,但是原始问题(以及答案)可能不适合实际情况。

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? 如果这是调用CGI脚本的外部源,那么如何知道它是您的站点上的PHP页面还是CGI脚本? For me, you can just set up this payment service to call your PHP page and avoid any requirement for redirection. 对我来说,您只需设置此付款服务即可调用您的PHP页面,而无需任何重定向。 Or is there something I haven't understood? 还是我不了解的东西?

If it looks for a specific URL on your site, eg /cgi-bin/trigger.cgi , you can trick it with .htaccess put in cgi-bin , like this: 如果它在您的站点上查找特定的URL,例如/cgi-bin/trigger.cgi ,则可以使用放在cgi-bin .htaccess来欺骗它,如下所示:

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

the easiest way would be to add a get variable to your URL: 最简单的方法是将get变量添加到您的URL:

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

then your PHP doesn't have to do anything fancy. 那么您的PHP不必做任何花哨的事情。 However, you can look at the $_SERVER['HTTP_REFERER'] value and parse the referring script's name. 但是,您可以查看$_SERVER['HTTP_REFERER']值并解析引用脚本的名称。 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. 搜索search.cpan.org以获取有关会话模块的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM