Ross
Ross

Reputation: 49

How to get different parts of url using php?

I have coded so that i get the following url upon clicking a certain link.

.../project/auction/auction.php?user=ernie6?auc=1

I just wondered what is best way to "get" the following details "ernie6" (as the username) and "1" this being the first auction. Moreover what is the "general rule" to extract data of the form "y=z?a=b?c=d?..."?

Thanks a lot.

Upvotes: 0

Views: 197

Answers (4)

koopajah
koopajah

Reputation: 25562

Your URL is not correct, if you want to provide arguments you need to start with a ? and then separate each arguments by a &

Then on your PHP script auction.php you retrieve each arguments like this:

$user = $_GET['user'];

The $_GET variable is a global array containing every parameters provided on the URL. More info on the query string here : http://en.wikipedia.org/wiki/Query_string

EDIT: If you try to retrieve an argument that does not exist you will have PHP warnings or errors. To avoid these it is better to ensure the index exists in the array before retrieving. Something like this would be better:

if(isset($_GET) && isset($_GET['user'])) {
    $user = $_GET['user'];
}

Upvotes: 1

the_man_slim
the_man_slim

Reputation: 1215

Assumimg that you are using GET or POST, you can simply get the values like so:

$user = $_POST['user'];
$auc = $_POST['auc'];

or

$user = $_GET['user'];
$auc = $_GET['auc'];

You might find the following documentation useful:

http://php.net/manual/en/reserved.variables.get.php http://php.net/manual/en/reserved.variables.post.php

Upvotes: 0

Zoltan Toth
Zoltan Toth

Reputation: 47667

It should be & instead of the second/third/etc ?

user=ernie6&auc=1

and then you can refer to your $_GET global array

To see its full content you can do a var_dump($_GET) or get the specific values by:

$user = $_GET['user'];
$auc = $_GET['auc'];

Upvotes: 3

cryocide
cryocide

Reputation: 741

For that url, $_GET['user'] and $_GET['auc'] would be defined in the scope of your script if you properly formatted the link (begin get data with ?, separate variables with &).

Upvotes: 0

Related Questions