PHP - Check if there are GET parameters in a URL

2

Good evening!

This question is more for curiosity.

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

I want to concatenate the Google Analytics parameters in this string (which I could do without checking for integrity simply by adding% with%), but then I was thinking: what if I change the link structure (type via url_rewrite)? Or if for some reason both formats (with / without GET) are stored by PHPBB? Some links would obviously be broken (by "opening" the part of the request using an & instead of a?), And so would need a check.

What I want to do is simply check this string if there are GET variables like these (in case I ever change the layout of the URLs and so the links do not break) and, depending on whether or not they exist, add a &utm_blablabal... or & before the Analytics parameters. Basically an "integrity check".

Thanks for the help, and sorry if I was not clear.

    
asked by anonymous 27.02.2016 / 04:06

2 answers

1

Simple. You can tell what's in $ _GET

empty($_GET)

Or

(count($_GET) > 0)

If it is from a specific url, do so

strlen(parse_url($url, PHP_URL_QUERY)) > 0
    
28.02.2016 / 16:23
0

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

parse_url() receives a string that represents a URL and breaks the string into components. Given its sample 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() receives a query string, string with GET parameters, and breaks into an array with keys and values. Given the query string of the URL above 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 link method, which receives an associative array (array with keys and values) to reconstruct the query string;

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

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";

go to printar:

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