I need to take the current URL and know if after index.php you have "?" or "&"

Asked

Viewed 1,195 times

-3

The initial part to get the URL I already made using this code:

//URL ATUAL
$url_atual = str_replace("/", "", $_SERVER[REQUEST_URI]);
echo $url_atual;

..... If I’m at the url http://localhost/index.php me returns: index.php

if I’m at the url http://localhost/index.php? module=guide return me: index.php? module=tab

But I need to know how to know if after index.php you’re using some "?" like this example: index.php? module=guide.....

because I need to create a link

if it’s only index.php adds: lang=1">

index.php? lang=1 result

or

if it is index.php? module=guide &lang=1">

index.php? module=gui&lang=1 result

3 answers

3


Wouldn’t it be simpler to use $_SERVER['QUERY_STRING'] which is made for exactly this?

If I understand you want to add to existing links, it should look like this:

$querystring = $_SERVER['QUERY_STRING'];

//Adiciona o ? se a querystring não for vazia
if ($querystring != '') {
     $querystring = '?' . $querystring;
}

echo '<a href="index.php', $querystring,'">';
echo '<a href="index.php', $querystring,'&foo=bar">';

If you want to edit the values of the query string just use the function http_build_query() with $_GET, assuming you have a URL like this:

 index.php?foo=bar&baz=2

And want to exchange the value of baz= and generate the link then do this:

$querystring = '';

//Array vazias não passa no IF, não é necessário `if (!empty())`
if ($_GET) {
    //Copia o GET
    $manipula = $_GET;

    $manipula['baz'] = 10; //Trocou o valor para 10

    $querystring = '?' . http_build_query($manipula, '', '&amp;'); // O uso de `&amp;` é para HTML, mas na URL ele será tratado como & apenas
}

echo '<a href="index.php', $querystring,'">';

This will generate a link like this:

index.php?foo=bar&baz=10
  • I used your second example, it worked, as well as adding what I wanted to an existing link.. it does not repeat before when I selected a language from Portuguese to English with the existing link EX: index.php? module=registro&lang=pt he repeated: index.php? module=registro&lang=pt&lang=en now with this manipulation, there is no more this problem, Thanks!!

  • https://www.gbrwc.com <---- ja esta funcionando neste site =)

0

Pick up the initial part with

$url_atual = str_replace("/", "", $_SERVER[REQUEST_URI]);

may bring unexpected results, see for example, if the url is

http://dominio.com/diretorio3/index.php?module=guia

$_SERVER[REQUEST_URI] will be

diretorio3/index.php?module=guia

and after replace the result will be

diretorio3index.php?param=module=guia

Suggestion:

$url_atual = $_SERVER['REQUEST_URI'];

$result = (parse_url($url_atual , PHP_URL_QUERY));

if ($result !== null) {
    echo $url_atual."&lang=1";
} else {
    echo $url_atual."?lang=1";
}

parse_url- Interprets a URL and returns its components

PHP_URL_QUERY after the interrogation ?

example in the ideone

-2

I made a code here that solved the problem

//URL ATUAL $url = str_replace("/", "", $_SERVER[REQUEST_URI]); if ($url == '') { $url_atual = "index.php"; } else { $url_atual = $url; }

echo $url_current; if ($url_current != "index.php") { echo "&lang=en"; } Else { echo "? lang=en"; }

Browser other questions tagged

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