views:

171

answers:

3

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 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.

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

use strict;
use warnings;


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

Location: $URL;
A: 

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.

dnagirl
+2  A: 

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>
codeholic
What you haven't understood is that in Web programming, when people see the term "call a CGI script" they think "Perl" and when they see the word "Perl" they think "CGI script". I think you're right; even the requirement "on success a CGI script is called" doesn't make any sense, there doesn't appear to be any reason for CGI to be involved, let alone Perl.
reinierpost
+2  A: 

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.

leonbloy
I wonder, how easy it is to provoke people obsessed with their XY problem into accepting a wrong answer ;)
codeholic