April 23, 2012

How do i echo php for json string?

Question by Azukah

i need to echo $account into $url so that the url/path is whatever i get from $account plus the extension json. I can’t figure it out with the quotes. i tried single and double quotes but no luck. any ideas?

<?php 
$account = $_POST['account'];
$url = 'echo $account.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

Answer by Brombomb

Like this?

<?php 
$account = $_POST['account'];
$url = $account. '.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

the . operator is used for string concatenation in php. This means take the value of $account and append the string .json to the end, and store that in the variable $url. The rest of the code looks all right from there. There are a few other ways to do this as well with strings in php, but I find this one simplest.

Answer by Starx

You can do this as this

$account = $_POST['account'];
$content = file_get_contents($account.'.json');
$json = json_decode($content, true);

Need I remind you how, vulnerable your codes are to injection?

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!