Redeem id passed by URL without using GET

Asked

Viewed 637 times

0

I have a question that I believe is simple for almost everyone, but I did not find any material that really help me to solve this.

I am developing a micro-service in php that has as its sole objective to reuse a set of functions in php that I took from another application.

So far so good, but one of the main functions of this set does a check of the current url, being it in a format similar to ".../users/{userid}" to use this userid for a certain processing.

The question is as follows: how do I, when I type something like ".../users/15" in the browser, redirect me to this function file and play the value 15 where is "{userid}"?

Edit 1:I managed by solving using the user url method in the reply marked as the appropriate, thank you all.

  • In case you are connecting to a database or are you trying to return data from an html page? I searched here and found this site http://www.mauricioprogramador.com.br/posts

  • I’m not using a bank. What I’m going to do is take the id that the user type at the end of the url and pass to the function I described there so that it checks on social networks and returns a result according to what the query results. I could even change to GET but I can’t change the function code because it’s a shared code

  • 1

    Regarding redirect, you will need to configure the file .htaccess to get every url in php you can use $_SERVER["REQUEST_URI"]. Search for friendly url

  • I looked at the internet and saw that depending on the service or site, the information can be returned in JSON,XML among others. But all by the GET method. I don’t know if you can do it the way you described.

  • Take a look at the suggestion I put in at the end. It will probably answer you.

2 answers

2


I’m not very good at REGEX.

If your url is like this: "www.site.com.br/algo/seila/tantofaz/{15}"

You can do it:

$string = $_SERVER["REQUEST_URI"]; // pega a url
preg_match_all ('/\{\d*\}/', $string, $matches) ; // pega o valor que está {15}
$valor = str_replace(array("{", "}"), "", $matches[0][0]); // pega apenas o numero
echo $valor; // imprime

With the value, you can do whatever you want in your job.

You could also work this way:

"www.site.com.br/users/index.php/{15}"

Using the file in the users directory.

how do I, when I type something like ".../users/15" in the browser, redirect me to this function file and play the value 15 where is "{userid}"

Using an example with explode() suggested by @Bacco would:

Url: www.site.com/users/index.php/15

include("../diretorioDasFuncoes/arquivoFuncoes.php");  // inclui o arquivo que contém as funções
$string = $_SERVER["REQUEST_URI"]; // pega essa string "/users/index.php/15"
$urlParts = explode("/", $string); // transforma em array
$userid = $urlParts[3]; // recupera o valor
funcaoUser($userid); // usa a função necessária

/*

$urlParts <-- array("", "users", "index.php", 15)

  /users/index.php/15

^    ^       ^      ^
.    .       .      ....... [3]
.    .       .............. [2]
.    ...................... [1]
........................... [0]

*/
  • 1

    You can also break the url by the bars and use its parts

  • Yes @Guilhermecostamilam ! I did so because it will always have the value { between keys }

  • I get it, but where do I need to put this code? In an index file inside "tantofaz"?

  • @matiosao who calls his functions? The file that calls the functions that will have to pick up this id, check to which it launches.

  • @matiosao may be on the index

  • @if your application is in directories, then it must be in the index of that directory for example

  • but I don’t know if it will work without url friendly

  • @matiosao Amigo, if I’m understanding you well... You need to first learn about url friendly.

  • @Andreicoelho no htacces apenas to filtrando para não precisa do . php at the end of the link, then I will program the index with the check function following the examples, in case, in the folder referring to the path of the url that will precede the given id, correct?

  • id is a variable.

  • When you use URL friendly, you are not using directories. HTACCESS simulates this. Imagine that you have this page. site.com/index.php?page=1. with htaccess it looks like this: site.com/1

  • @matiosao is how you’re working?

  • If you don’t use URL friendly will be tricky, you would need a file or directory for each id

  • @Exact guilhermecostamilam.

  • 1

    Remembering that you don’t need to have anything special for the URL to be friendly, let alone .htaccess. This works perfectly "www.example.com/fotos.php/276" - And better than using Regex is a simple explode. - And if you don’t want the PHP extension, just configure the server to treat files with no extension as PHP itself.

  • @Bacco living and learning.. "www.example.com/fotos.php/276" . Didn’t know this! Tks!

  • 1

    @Andreicoelho take a peek at $_SERVER['PATH_INFO'] - depending on the config, that’s enough. A good test is for a test page.php with a phpinfo(); inside and experience various Urls (type teste.php/a/b/c, and track the generated variables.

  • @Bacco very good! Thanks!

Show 13 more comments

1

Picks up the explode and selects the part of the numbers.

After you do a regex or a function that captures only the numbers of the string stored in the variable.

Would something like this:

$url_desejada =" https://www.url.com.br/?id=0909090908";
$numeros=explode('/' , $url_desejada);
$imprime =$numeros[3];

function números($imprime)
{

    return 

      preg_replace("/[^0-9]/", "", $imprime);
}

Simplifying:

$url_desejada =" https://www.url.com.br/?id=0909090908";
$numeros=explode('/' , $url_desejada);

$id= preg_replace("/[ 0-9]/", "", $numbers);

echo $numbers[3];

Good luck!

:)

Browser other questions tagged

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