user1568736
user1568736

Reputation: 133

variable in definition

I'm loading an XML file into the page and want to get information from the URL parameter and put it in the url for the XML file that simplexml is laoding. is this possible? Here is the current code:

$gid = $_GET['gid']; 
$gach = simplexml_load_file('http://xml.com/GIDHERE/page.html?xml=1');

How can I put $gid where it says GIDHERE ?

Upvotes: 0

Views: 43

Answers (2)

Hawili
Hawili

Reputation: 1659

$gach = simplexml_load_file("http://xml.com/$gid/page.html?xml=1");

you have to use double quote, single quote disable variable parsing and watch out for putting unchecked variable into url, you may end with exploits! if gid is integer, just force it to be so by doing this

$gid = (int) $_GET['gid'];

Upvotes: 0

Core Xii
Core Xii

Reputation: 6441

$gach = simplexml_load_file('http://xml.com/' . rawurlencode($gid) . '/page.html?xml=1');

You might also want to validate the input before using it, e.g.:

if (!preg_match('{^[1-9][0-9]*$}', $gid)) die;

If $gid is always an integer.

Upvotes: 1

Related Questions