[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to user header("Location... with dynamic URLs

Posted on 2006-04-27
15
Medium Priority
?
224 Views
Last Modified: 2008-02-26
What's wrong with this line?:

header("Location:https://www.paypal.com/cgi-bin/webscr?cmd=$_POST['cmd']&redirect_cmd=$_POST['redirect_cmd']&business=$_POST['business']
&upload=$_POST['upload']&item_name_1=$item1&quantity_1=$_POST['quantity_1']&amount_1=$_POST['amount_1']");
0
Comment
Question by:weikelbob
  • 4
  • 3
  • 3
  • +4
15 Comments
 
LVL 3

Accepted Solution

by:
dancablam earned 336 total points
ID: 16555114
You just need to concat the string and vars. Below is a working fix for your problem:

header("Location:https://www.paypal.com/cgi-bin/webscr?cmd=".$_POST['cmd']."&redirect_cmd=".$_POST['redirect_cmd']."&business=".$_POST['business']."&upload=".$_POST['upload']."&item_name_1=".$item1."&quantity_1=".$_POST['quantity_1']."&amount_1=".$_POST['amount_1']);
0
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 336 total points
ID: 16556024
Hi Bob, you need to remove the quotes of the arrays when it is used inside the arrays.

header("Location:https://www.paypal.com/cgi-bin/webscr?cmd=$_POST[cmd]&redirect_cmd=$_POST[redirect_cmd]&business=$_POST[business]&upload=$_POST[upload]&item_name_1=$item1&quantity_1=$_POST[quantity_1]&amount_1=$_POST[amount_1]");


---
Harish
0
 
LVL 7

Author Comment

by:weikelbob
ID: 16556330
dancablam and mgh_mgharish,

Which of the two ways above is correct?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 16556351
Both !

And there is another way.. you can surround the variables in {} as in:

...cmd={$_POST['cmd']}&...
0
 
LVL 7

Author Comment

by:weikelbob
ID: 16556394
Thanks guys, I'll leave this open a little longer
0
 
LVL 6

Assisted Solution

by:soapergem
soapergem earned 332 total points
ID: 16556482
Another way would be to compile the string on a separate line (or lines), and just reference that in the header command. Could be cleaner. There's a lot of ways to do things in PHP!

$url  = 'https://www.paypal.com/cgi-bin/webscr?cmd=';
$url .= $_POST['cmd'] . '&redirect_cmd=' . $_POST['redirect_cmd'];
$url .= '&business=' . $_POST['business'] . '&upload=' . $_POST['upload'];
$url .= '&item_name_1=' . $item1 . '&quantity_1=' . $_POST['quantity_1'];
$url .= '&amount_1=' . $_POST['amount_1'];

header("Location:$url");
0
 
LVL 9

Assisted Solution

by:blue_hunter
blue_hunter earned 332 total points
ID: 16559378
remember the <space> between your
"Location:" and your "url"

it should look like this

header("Location: ".$url);

0
 
LVL 6

Expert Comment

by:soapergem
ID: 16559544
Ah yes, sorry about that. Or like this:

header("Location: $url");


(You don't need to separate variables when you're using double quotes.)
0
 
LVL 6

Assisted Solution

by:ixti
ixti earned 332 total points
ID: 16559569
Take a note at blue_hunter's, soapergem's, mgh_mgharish's and dancablam's suggestions.
For me, mgh_mgharish's solution is much closer. So for me it's simplier to write something like this:
<?php
header("Location: https://www.paypal.com/cgi-bin/webscr?cmd={$_POST['cmd']}&redirect_cmd={$_POST['redirect_cmd']}&business={$_POST['business']}&upload={$_POST['upload']}&item_name_1={$item1}&quantity_1={$_POST['quantity_1']}&amount_1={$_POST['amount_1']}");
?>

Also I suggest you to insert die() or exit() after header("Location: ...") like this:
<?php
header("Location: https://www.paypal.com/cgi-bin/webscr?cmd={$_POST['cmd']}&redirect_cmd={$_POST['redirect_cmd']}&business={$_POST['business']}&upload={$_POST['upload']}&item_name_1={$item1}&quantity_1={$_POST['quantity_1']}&amount_1={$_POST['amount_1']}");
exit();
?>
0
 
LVL 6

Expert Comment

by:ixti
ID: 16559624
Another thing you have to note is if $_POST['redirect_cmd'] will be somthing like "text with spaces" then this header will cause an error. So you have to validate all data in according to url's conditions.
If you're using PHP 5 or above then you can use http_build_query(); If not you have to create your own function. Anyway you can create a PHPVersion-Independant function to do this:
<?php
function prepareUrl($dataArray)
{
    // If our server is PHP 5 and higher, then
    // http_build_query() function must exist
    // and we can use it.
    // $dataArray must be an array.
    if (is_array($dataArray) && function_exists("http_build_query")) {
        return http_build_query($dataArray);
    }
    // If http_build_query() is not exist, and
    // $dataArray is array, then do urlencode
    // dor all values of $dataArray
    elseif (is_array($dataArray)) {
        $parsed = array();
        foreach ($dataArray as $k => $v) {
            $parsed[] = $k . "=" . urlencode($v);
        }
        return implode("&", $parsed);
    }
    else {
        trigger_error("First param of prepareUrl must be an array.", E_USER_WARNING);
        return null;
    }
}

$url_data = array(
    "cmd" => $_POST['cmd'],
    "redirect_cmd" => $_POST['redirect_cmd'],
    "business" => $_POST['business'],
    "upload" => $_POST['upload'],
    "item_name_1" => $item1,
    "quantity_1" => $_POST['quantity_1'],
    "amount_1" => $_POST['amount_1']
);

$url_query = prepareUrl($url_data);

header("Location: https://www.paypal.com/cgi-bin/webscr?{$url_query}");
exit();
?>
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16559662
Why not just use urlencode()?

//  URL stored in variable $url
header('Location: ' . urlencode($url));
0
 
LVL 6

Expert Comment

by:ixti
ID: 16559705
<?php
$url = "https://www.paypal.com/cgi-bin/webscr?cmd=test&var2=a lot of text&var3=with\'special chars";
var_dump(urlencode($url));
?>

That's why...
0
 
LVL 8

Assisted Solution

by:KennyTM
KennyTM earned 332 total points
ID: 16560840
If you have PHP 5 you can also call http_build_query() on $_POST.

Ref: http://hk2.php.net/manual/en/function.http-build-query.php
0
 
LVL 6

Expert Comment

by:ixti
ID: 16560928
KennyTM, I've wrote about it. But he cannot use http_build_query() on $_POST, because:
1. _POST can conatin data that has not to be inserted in query
2. He need not to put only some _POST values, but $item1

Also his server may not have PHP 5, but PHP4. That's why I've suggested a function.
0
 
LVL 7

Author Comment

by:weikelbob
ID: 16565528
I got it. I learned a lot from all of your posts. Thanks you guys,

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question