0

Is it possible to set part of the URL as a variable in PHP?

So for example if this was the URL:

http://domain.com/?=customer&p[1]=data&p[1]=admin_data&type_id=5&client_id=2345

Could I take the last segment of the URL which is client_id=2345 and set the 2345 as a variable $client_id

4
  • 1
    yes extract your $_GET like extract($_GET) Commented Jan 16, 2014 at 8:09
  • this you can find on google, no? Commented Jan 16, 2014 at 8:11
  • wow everyone is so fast at night. Commented Jan 16, 2014 at 8:12
  • This might help: php.net/manual/en/reserved.variables.get.php Commented Jan 16, 2014 at 8:16

4 Answers 4

2

From url you use $_GET

$client_id = $_GET['client_id']

There is similar variable for data sent from form $_POST

Sign up to request clarification or add additional context in comments.

Comments

2

y, ofc, thats what $_GET is for:

$client_id = $_GET['client_id'];

Dont forget to validate pls, for example:

$client_id = filter_var($_GET['client_id'], FILTER_VALIDATE_URL);

2 Comments

so does the filter strip anything that gets attached beyond the numbers following equal?
I dont know exactly how the Filter works tbh.^^ Its a predefined PHP function and makes sure that the input is valid. If you Filter with FILTER_VALIDATE_URL, you cant enter something like "hi im gosu" or whatever. For further information about filters, you should look at the php docs: de3.php.net/manual/en/function.filter-var.php
1

You can get parameter from url by $_GET($_GET is a php reserved variable) by parameter name. format is : $_GET['parameter_name'];

It seems that your url is not correctly formated:

http://domain.com/?=customer&p[1]=data&p[1]=admin_data&type_id=5&client_id=2345

it should be like this:

http://domain.com/?param_name=customer&p[1]=data&p[1]=admin_data&type_id=5&client_id=2345

Then you can get:

$var1 = $_GET['param_name'];
$client_id =  $_GET['client_id']; // get client id

here is the reference : http://www.php.net/manual/en/reserved.variables.get.php it will help you to understad.

let me know if you have any query

Comments

1

See by yourself :

<?php
if (count ($_GET) == 0)
{
    // redirect with GET parameters
    $strange_get = "=customer&p[1]=data&p[1]=admin_data&type_id=5&a[1]->x=zz&b[1][2]=one&b[1][]=two"
    header ("Location:${_SERVER['PHP_SELF']}?$strange_get");
    exit();
}
// see what we got
print_r($_GET);
?>

Result:

Array
(
    [p] => Array
        (
            [1] => admin_data
        )
    [type_id] => 5
    [a] => Array
        (
            [1] => zz
        )
    [b] => Array
        (
            [1] => Array
                (
                    [2] => one
                    [3] => two
                ) 
        )
)

customer was ignored because no name was given
p[1] was set twice, so only the last value remained
a[1]->x=zz was interpreted as a[1]=zz
b[1][2] and b[1][] worked as expected

Arrays are the only things you can expect to work out of the box.

For instance, passing "?index=10&value[index]=123" will not interpret index magically

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.