PHP - Check for GET parameters in a URL

Asked

Viewed 1,943 times

2

Good night!

This doubt is more of a curiosity.

I have a php page that displays certain topics of a phpbb forum. Among the data stored is the path (url) of the topic, which comes in the format http://meusite.com.br/forum/viewtopic.php?f=yy&t=xx. From what little I know f and the t are GET requests.

I intend to concatenate in this string the parameters of Google Analytics (what I could do without checking integrity by simply adding &utm_blablabal...), but then I thought: what if I change the structure of links (like via url_rewrite)? Or if for some reason both formats (with/without GET) are stored by PHPBB? Some links would obviously come broken (by "opening" the request part using a & instead of a ?), and so would require a check.

What I want to do is simply check in this string if there are GET variables like these (in case one day I change the schema of the Urls and so the links do not break) and, depending on the existence or not, add a & or a ? before the Analytics parameters. Basically an "integrity check".

Thank you for your help, and I’m sorry if I wasn’t clear.

2 answers

1

Simple. You can tell what’s in the $_GET

empty($_GET)

Or

(count($_GET) > 0)

If it is a specific url, do so

strlen(parse_url($url, PHP_URL_QUERY)) > 0
  • Wait... $_GET is not reserved for requests? Because that URL is not "coming" from an HTTP request. It’s a string that is stored in a database (PHPBB) and I just need to check if there are GET style parameters in it, so I can insert Adsense add-ons without repeating the ?.

0

To break your string representing a URL you can use the methods parse_url() and parse_str().

parse_url() takes a string representing a URL and breaks the string into components. Given its example URL http://meusite.com.br/forum/viewtopic.php?f=yy&t=xx parse_url() returns:

array(4) {
  'scheme' =>
  string(4) "http"
  'host' =>
  string(14) "meusite.com.br"
  'path' =>
  string(20) "/forum/viewtopic.php"
  'query' =>
  string(9) "f=yy&t=xx"
}

parse_str() takes a query string, string with GET parameters, and breaks into an array with keys and values. Given the query string of the above URL f=yy&t=xx, the method returns:

array(2) {
  'f' =>
  string(2) "yy"
  't' =>
  string(2) "xx"
}

And to rebuild your URL after adding a new GET parameter (or modifying an existing one) you can use:

  • the method http_build_query(), that takes an associative array (array with keys and values), to rebuild the query string;

  • + a simple string concatenation to reconstruct the URL from the components of the same that you took with parse_url() and the query string that you repeated in the above step.

Below, the complete code to break a URL, add GET parameters and rebuild the URL again:

<?php

$url_string = 'http://meusite.com.br/forum/viewtopic.php?f=yy&t=xx';

// quebrando a URL em partes
$url_partes = parse_url($url_string);

// quebrando os parâmetros GET da URL
parse_str($url_partes['query'], $parametros_get);

// adicionando novo parâmetro GET
$parametros_get['novoParametro'] = 'VALOR';

// reconstruindo a query string (os parâmetros GET juntos)
$url_partes['query'] = http_build_query($parametros_get);

// reconstruindo a URL
$url_string = $url_partes['scheme'] . '://' . $url_partes['host'] . $url_partes['path'] . '?' . $url_partes['query'];

echo $url_string . "\n";

will print:

http://meusite.com.br/forum/viewtopic.php?f=yy&t=xx&novoParametro=VALOR

Browser other questions tagged

You are not signed in. Login or sign up in order to post.