Dr.Mezo
Dr.Mezo

Reputation: 869

Get content after question mark in PHP

I am getting a request like this and the url looks like this : www.site.com/test.php?id=4566500

Now am trying to get the id number to make the code in test page work, is there a way to do this?

<?php

echo("$id"+500);
 ?>

Upvotes: 1

Views: 2595

Answers (4)

Sanchit Gupta
Sanchit Gupta

Reputation: 3234

You cannot access direct url parameter without using predefined PHP super global variable like $_GET["$parameter"] OR $_REQUEST["$parameter"].

So for : www.site.com/test.php?id=4566500

<?php
    $id = (int)$_GET['id'];     // Or  $_REQUST['id'];
    if(is_numeric($id)){
        echo $id + 500;
    }else{
        echo $id;
    }

?>

For more detail :
PHP $_GET Reference
PHP $_REQUEST Reference

Upvotes: 0

Stephan Kulla
Stephan Kulla

Reputation: 5077

Do not forget to check the right setting of your Getter parameter:

if (isset($_GET['id']) && preg_match("\d+", $_GET['id'])) {
    // do something with $_GET['id']
} else {
    // appropriate error handling
}

Remember that anyone can set the id parameter to any value (which can lead to possible XSS attacks).

Upvotes: 0

John Conde
John Conde

Reputation: 219934

This is basic PHP. You want to use the $_GET superglobal:

echo $_GET['id'] + 500;

Upvotes: 4

Emily Shepherd
Emily Shepherd

Reputation: 1369

You can access these values via the $_GET array:

<?php

echo($_GET['id'] + 500);
 ?>

Upvotes: 4

Related Questions