1

In PHP it is very common to redirect to a new URL and pass along information such as a 'username.'

<?php

   header( 'Location: http://www.yoursite.com/new_page.php?user_name=$username' ) ;

?>

I can then access the value of 'user_name' in 'new_page.php.'

I can not figure out how to do this using perl.

print $cgi->redirect(-uri=>http://www.yoursite.com/new_page.pl?user_name=$username, -status=>303 , -cookie => $c );

I've tried using my $username = $cgi->param('user_name'), but it's not printing anything.

Thanks.

Wes Crow
  • 2,971
  • 20
  • 24
Miek
  • 1,127
  • 4
  • 20
  • 35

2 Answers2

4

You forgot the quotes:

print $cgi->redirect(
         -uri=>"http://www.yoursite.com/new_page.pl?user_name=$username", 
         -status=>303,
         #... 
);

Also, remember escape the username before calling redirect:

my $username = escape( $cgi->param('user_name') );
Miguel Prz
  • 13,718
  • 29
  • 42
  • The `escape` subroutine would need to be imported from somewhere, right? – gpojd May 01 '13 at 20:26
  • Thanks, I don't use CGI much and didn't know it exported that subroutine. – gpojd May 01 '13 at 20:29
  • I did forget the quotes in the example code but not in the actual code. It appears my problem was using single quotes. I just switched to double quotes and it appears to be working. I also got it to work with single quotes appending the $var with . I can now access the value of user_name with my $username = $cgi->param('user_name'); Thanks for the help. – Miek May 01 '13 at 20:37
  • 1
    In Perl, there are differences between single and double quotes. See this: http://stackoverflow.com/questions/943795/whats-the-difference-between-single-and-double-quotes-in-perl – Miguel Prz May 01 '13 at 20:40
1

I don't think that PHP will work, but I'm not a PHP developer. If you gave the error it would help, but I think you need to quote the string.

print $cgi->redirect(
    -uri    => "http://www.yoursite.com/new_page.pl?user_name=$username",
    -status => 303,
    -cookie => $c,
);

You should add the following two lines (enabling the strict and warnings pragmas) to the top of your script, they would have helped you debug the issue:

use strict;
use warnings;

For the record, I think the PHP example should use double quotes:

header( "Location: http://www.yoursite.com/new_page.php?user_name=$username" );
gpojd
  • 22,558
  • 8
  • 42
  • 71
  • I'm sure your right. I threw the php in there without checking syntax just so you would know what I'm referring to. – Miek May 01 '13 at 20:27