PHP and HTML: acquire all GET-values - php

I am in need of a PHP-program which will take all values from a HTML-link (with GET), regardless of how much one changes things in the address-bar, and then prints it out on the page. The code I have now is this:
HTML:
Link
PHP:
<?php
Echo "Value1: "; Echo $_GET["a"]; Echo "\n";
Echo "Value2: "; Echo $_GET["b"]; Echo "\n";
Echo "Value3: "; Echo $_GET ["c"];
?>
It works as intended for just these values, but it cannot cope with, for example, another variable being added in the address bar. I need some kind of PHP-function which can look for all kinds of variables through the GET-function.
Any help is appreciated!

Use a loop (modify as needed):
foreach ($_GET as $num => $value)
{
echo 'Value ' . $num . ': ' . htmlentities($value). "<br>" . PHP_EOL;
}
References:
Control structures
htmlentities()

Something like this:
<?php
foreach ($_GET as $key => $value) {
echo htmlspecialchars($key) . " - " . htmlspecialchars($value) . "<br>";
}
?>

<?php
print_r($_GET);
?>

Use a foreach loop
foreach ($_GET as $key => $val){
echo htmlentities($key).": ".htmlentities($val)."<br />\n";
}
or simply -
echo "<pre>".print_r($_GET,1)."</pre>";

To get all values and their corresponding names:
foreach($_GET as $key => $value)
{
echo $key . ' - ' . $value;
}

$my_get = array(); // store $_GET vars
if ('GET' == $_SERVER['REQUEST_METHOD']) // check that request method is "get"
foreach ($_GET as $key => $val)
{
$my_get[$key] = $val; // store
}
output $my_get:
array (
'a' => 'value1',
'b' => 'value2',
'c' => 'value3',
)

Since $_GET is nothing but an array, you can use for each loop to get the elements of it.

you get all $_GET values using print_r($GET) ...
otherwise you could to something like this
foreach(array_keys($_GET) as $key) {
echo htmlspecialchars($_GET[$key]);
}
Later Edit : took into account the security issue exposed in the comment
using this method is good because you know what kind of variable you have then you could do something cool like :
if ($key == 'a') do this
if ($key == 'b' && $_GET[$key] == 'bar') do that

Related

how to echo array data from pinterest oauth api php

I am trying to get user profile from pinterest using oauth api.
code for user data :
$me = $pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[large]'
));
and echo result by :
echo $me;
the output is as follow :
{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}
Now i want to echo this result as
id="195414208739840616"
username="rajivsharma033"
first_name="Rajiv"
and so on...
please help me.
Since you are getting json data so you need to use json_decode():-
<?php
$me = '{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}';
$array_data = json_decode($me);
echo "<pre/>";print_r($array_data);
foreach ($array_data as $key=>$value){
if($key == 'image'){
echo $key. " url is=" . $value->large->url .'<br/>';
}else{
echo $key. "=" . $value .'<br/>';
}
}
I would solve it by using two times foreach:
$a = json_decode($me);
foreach ($a as $key=>$value){
echo $key.'="'.$value.'"<br/>';
}
foreach ($a['image']['large'] as $key=>value){
echo 'image-large-'$key.'="'.$value.'"<br/>';
}
Alternatively, you can do this recursive:
function echojson($string=''){
$a = json_decode($me);
foreach ($a as $key=>$value){
if (is_array($value)) echojson($string.'-'.$key);
else
echo $string.$key.'="'.$value.'"<br/>';
}
}

PHP | Get input name through $_POST[]

HTML example:
<form method="post" id="form" action="form_action.php">
<input name="email" type="text" />
</form>
User fills input field with: dfg#dfg.com
echo $_POST['email']; //output: dfg#dfg.com
The name and value of each input within the form is send to the server.
Is there a way to get the name property?
So something like..
echo $_POST['email'].name; //output: email
EDIT:
To clarify some confusion about my question;
The idea was to validate each input dynamically using a switch. I use a separate validation class to keep everything clean. This is a short example of my end result:
//Main php page
if (is_validForm()) {
//All input is valid, do something..
}
//Separate validation class
function is_validForm () {
foreach ($_POST as $name => $value) {
if (!is_validInput($name, $value)) return false;
}
return true;
}
function is_validInput($name, $value) {
if (!$this->is_input($value)) return false;
switch($name) {
case email: return $this->is_email($value);
break;
case password: return $this->is_password($value);
break;
//and all other inputs
}
}
Thanks to raina77ow and everyone else!
You can process $_POST in foreach loop to get both names and their values, like this:
foreach ($_POST as $name => $value) {
echo $name; // email, for example
echo $value; // the same as echo $_POST['email'], in this case
}
But you're not able to fetch the name of property from $_POST['email'] value - it's a simple string, and it does not store its "origin".
foreach($_POST as $key => $value)
{
echo 'Key is: '.$key;
echo 'Value is: '.$value;
}
If you wanted to do it dynamically though, you could do it like this:
foreach ($_POST as $key => $value)
{
echo 'Name: ', $key, "\nValue: ", $value, "\n";
}
Loop your object/array with foreach:
foreach($_POST as $key => $items) {
echo $key . "<br />";
}
Or you can use var_dump or print_r to debug large variables like arrays or objects:
echo '<pre>' . print_r($_POST, true) . '</pre>';
Or
echo '<pre>';
var_dump($_POST);
echo '</pre>';
Actually I found something that might work for you, have a look at this-
http://php.net/manual/en/function.key.php
This page says that the code below,
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
would give you the output,
fruit1
fruit4
fruit5
As simple as that.
current(array_keys($_POST))
should give you what you are looking for. Haven't tested though.
Nice neat way to see the form names that are, or will be submitted
<?php
$i=1;
foreach ($_POST as $name => $value) {
echo "</b><p>".$i." ".$name;
echo " = <b>".$value;
$i++;
}
?>
Just send your form to this script and it will show you what is being submitted.
You can use a foreach Loop to get all values that are set.
foreach ($_POST AS $k=>$v){
echo "$k is $v";
}
Your example
echo $_POST['email'].name; //output: email
wouldnt make sense, since you already know the name of the value you are accessing?

How to fetch if a new query has been added

while($fetchres = mysql_fetch_array($searchquery)) {
$v1 = $fetchres['v1']; $v2 = $fetchres['v2']; $v3 = $fetchres['v3'];
$v4 = $fetchres['v4']; $v5 = $fetchres['v5'];
$v6 = $fetchres['v6'];
$v7 = $fetchres['v7'];
}
Hi! How can I set this columns automatically? I mean my system automatically adds a column. For example my system adds a new column 'v8'. But in my code, I only inputted until v7. How can I automatically code this fetching of query by depending on how many columns my system automatically made? Thanks.
Use extract.
while ($row = mysql_fetch_assoc($searchQuery))
{
// Extract the keys of the array into the local symbol table.
extract($row);
// Now, all variables exist and you can echo them like this:
echo $v1;
echo $v8;
}
You still need to know the names of the variables. If you just want to echo everything inside, use foreach with a key assingnment:
while ($row = mysql_fetch_assoc($searchQuery))
{
foreach($row as $key => $value)
{
echo "$key: $value\n"; // Will output something like 'v1: foo' and 'v2: bar', separated by newlines.
}
}
If you really want to do this, you can use extract($fetchres).
More info about extract
Example:
$fetchres = array('v1'=>'foo', 'v2'=>'bar');
extract($fetchres);
echo '$v1 is ' . $v1 . 'and $v2 is ' . $v2;
You could use variable variables for that, although I think this is a really bad idea.
I think you actually want this:
while ($fetchres = mysql_fetch_array($searchquery)) {
foreach ($fetchres as $key => $value) {
echo $key . ': ' . $value . PHP_EOL;
}
}

get all values from php associative array

I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}

Print $_POST variable name along with value

I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}
It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}
If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}

Categories