Damo
Damo

Reputation: 73

How can I add this dynamically constructed URL to a PHP variable?

I have a feeling I'm being very silly, but go easy on me as I'm very new to PHP.

So as part of an application, I'm building, I allow users to generate a URL that can be used to access some of their data from outside the platform. The URL is constructed by the PHP script based on URL parameters in the users browser (fear not, the parameters only allow the passing of integers to prevent abuse).

So to create the URL that will be shared, the script does this:

https://example.com/shared.php?function=form&account=<?php echo $userid; ?>&form=<?php echo $myform; ?><?php if (isset($_GET['ref'])) { echo '&ref='.$ref; } ?><?php if (isset($_GET['upload'])) { echo '&upload='.$upload; } ?>&key=<?php echo $key; ?><?php if (isset($_GET['saving'])) { echo '&saving='.$_GET['saving']; } ?><?php if (isset($_GET['expdate'])) { echo '&expdate='.$expdate; } ?><?php if (isset($_GET['enc'])) { echo '&enc='.$enc; } ?>

I know I don't need to keep opening and closing the PHP tag, but it was just easier visually when I was putting everything together.

Anyway, the above does a good job at spitting out the URL, but if I want to add the final URL to a variable, how can I do it?

Obviously, normally it would be trivial:

$whatever = 'https://example.com/shared.php?function=form&account='.$variable.'&something='.$variable2.'etc etc

But because I've got if statements that check for URL parameters, I'm not sure how to accomplish it.

Upvotes: 0

Views: 722

Answers (3)

Damo
Damo

Reputation: 73

Big thanks to everyone that made suggestions. In the end I went with the following:

$array = array(
    'account' => $userid,
    'form' => $myform,
    'key' => $key,
);

if (isset($_GET['ref'])) {
    $array['ref'] = $ref;
}

if (isset($_GET['upload'])) {
    $array['upload'] = $upload;
}

if (isset($_GET['saving'])) {
    $array['saving'] = $_GET['saving'];
}

if (isset($_GET['expdate'])) {
    $array['expdate'] = $expdate;
}

if (isset($_GET['enc'])) {
    $array['enc'] = $enc;
}

$finalURL = http_build_query($array);

Works perfectly.

Upvotes: 1

Justinas
Justinas

Reputation: 43479

You can always prepare parameters before putting into string. Or there is ternary operator ?:


$ref = $_GET['ref'] ? '&ref=' . $_GET['ref'] : '';
$upload = $_GET['upload'] ? '&upload=' . $_GET['upload'] : '';
$saving = $_GET['saving'] ? '&saving=' . $_GET['saving'] : '';
$expdate = $_GET['expdate'] ? '&expdate=' . $_GET['expdate'] : '';
$enc = $_GET['enc'] ? '&enc=' . $_GET['enc'] : '';

$whatever = "https://example.com/shared.php?function=form&account={$userid}&form={$myform}{$ref}{$upload}&key={key}{$saving}{$expdate}{$enc}";

Or make it dynamic using http_build_query():

$params = [
    'function' => 'form',
    'account' => $userid,
    'form' => $myform,
    'key' => $key,
    'ref' => $_GET['ref'] ?? null,
    'upload' => $_GET['upload'] ?? null,
    ...
];

$url = 'https://example.com/shared.php?';
// array_filter will remove any empty value
$url .= http_build_query(array_filter($params));

Upvotes: -1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

You can concatenate strings with the . operator, you don't need to build the URL at once. However, in this case, it'd be easier to just use the builtin http_build_query() function. Populate an array as you validate parameters and feed it to the function when done.

P.S. Don't trust client-side validations. People may not care to enforce them when they type in the browser location bar.

Upvotes: 3

Related Questions