Recover URL id without losing any parameters - PHP

Asked

Viewed 68 times

0

Good morning, gentlemen. It may not be a good practice, alias, it’s probably not, but I’m passing some parameters in my URL by javascript to recover in PHP.

I’m passing like this in javascript. href="Analyze? id='+ par1 +'|'+ par2 + '"

ai in the URL, it’s like, weird to understand, but until then OK. Analyze? id=122963|09.27.09%204%S%20+%200.2%B%20+%200.5%CU%20+%200.3%ZN

the two parameters separated by '|', but then the bug catches.

the moment I recover for the $_GET['id'], the following happens, I use the explode to get each of the parameters, but the second parameter, was to come like this "09.27.09 4%S + 0.2%B + 0.5%CU + 0.3%ZN", with the +signals, however, the signals do not appear, they disappeared.

I even thought about encrypting the url, but I don’t know how to do it in javascript and then decrypt it in PHP, I don’t know if it actually works, but I wanted something simpler, which in the end became difficult.

I’m waiting for an answer. Thanks in advance.

  • 1

    Try using encodeURIComponent(par2).

  • you tbm can use php global variable $_SERVER['REQUEST_URI'], example $Uri = $_SERVER['REQUEST_URI'], soon after only you use methods to cut what is returned

  • The sign + is a valid character that can be used in a URL and has a meaning. If you want to pass a string that contains this character, do it with @Sam suggested, use encondeURIComponent. Ex.: href="analyze?id=' + encodeURIComponent(par1 + '|' + par2)

  • Only it would be better if you did it through a form, it would avoid a lot of headache.

  • @fernandosavio gave it right, that’s right. THANK YOU

1 answer

0


The signs of + do not appear because PHP is getting lost when it is time to do the parameters.
From what I understand you this "encoding" the special characters manually when forming your URL (analyze?id=122963|09.27.09%204%S%20+%200,2%B%20+%200,5%CU%20+%200,3%ZN) and that’s exactly what’s causing the problem.
The solution is to use the urlencode method to create the link with the parameters. Follow an example below.

<?php
    //Informe os parâmetros exatamente como você quer que seja exibido.
    $params = '09.27.09 4%S + 0,2%B + 0,5%CU + 0,3%ZN';
    //Encodando os params para substituir os caracteres especiais
    $encodedParams = urlencode($params);

    $encodeManual = '09.27.09%204%S%20+%200,2%B%20+%200,5%CU%20+%200,3%ZN';

    echo '<h2>Usando urlencode = '.$encodedParams.'</h2>';
    echo '<h2>Encode Manual    = '.$encodeManual.'</h2>';

    echo '<h1>Decode da url encodada manualmente = '.urldecode($encodeManual).'</h1>';
    echo '<h1>Decode da url encodada por urlencode = '.urldecode($encodedParams).'</h1>';

?>

Note: After searching the parameters with the $_GET[ ] do not need to use the urldecode, I only used in the example to show how it decodes your manually made url and the other created by the function urlencode.

Browser other questions tagged

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