Nickydonna
Nickydonna

Reputation: 802

PHP - Using data from url, but not via post nor get

I'm sorry if is a repeated question, but i having trouble with PHP. I'm working with the code of another programer, and he is using php. In one of the pages que get the information from the url, but no via post nor get, but by attaching it to the url and putting / between them, like this: www.example.com/memorial/31/john

the he uses 31 and john as data. I have no memorial directory, neither a file called memorial.

Is there a way to do this in PHP without a framework, he doesn't seem to be using any libraries either.

Upvotes: 0

Views: 120

Answers (2)

cwallenpoole
cwallenpoole

Reputation: 81988

You can find that information in the global $_SERVER variable: $_SERVER['REQUEST_URI']. As @Oli mentioned, this is probably through a rewrite engine.

For example if you went to the URL you mentioned (www.example.com/memorial/31/john):

$uriPieces = explode('/',$_SERVER['REQUEST_URI'],'/');
// this gets rid of all of the cases of double-slashes as well as the initial 
// and trailing. This is optional, but I will generally prefer it.
$uriPieces = array_filter($uriPieces);
print_r($uriPieces);

Outputs

Array
(
    [0] => memorial
    [1] => 34
    [2] => john
)

If you search your project for REQUEST_URI, you will find where the variables are being set.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

The most likely explanation is that he is using an URL rewrite engine, such as Apache's mod_rewrite. This will be doing something like turning the directory parts into GET variables.

Upvotes: 2

Related Questions