$_GET All the transmitted data - php

I have a.php file that send some elements data to b.php with GET method.
I b.php we don`t know id of elements in a.php file.
But we need access them.
Are there any way to read all submited data in b.php?
Thanks.

you can use the foreach structure:
foreach ($_GET as $key => $value)
{
// Do something
}
$_GET is a superglobal array. Here is the doc about it.
Like for all variables, you can dump its content and structured information about it to the output with the var_dump() function. This will help you to understand how it work.
$_GET only returns parameters passed by HTTP GET. It's commonly the part after the question mark in the URI, e.g. ?key1=value1&key2=value2
You can also use $_REQUEST (documentation here) to retrieve all the values passed in the request by GET, POST, and COOKIE.
Some people think using $_REQUEST is unsafe, but IMHO, check the HTTP method have a very poor interest (well, not interest at all), because it depend of the user. And the user can trick this very easily.

Use the following to list all arguments.
foreach ($_GET as $key => $value) {
echo $key . ' => ' . $value . '<br />';
}

To access all the data in the $_GET global array, you can do something like this:
foreach($_GET as $key => $value)
echo "$key: $value";

Related

Get all possible $_GET parameters from URL

I have link to a website that runs a php code with the following variable:
http://www.example.com/run.php?test=abc
There are other $_GET variables usable in the link other than test, say id and title, which I don't know about. Is it possible to get the missing variables (If there are any)? In my example case it would be the id and title variables.
$_GET is an associative array of variables passed to the current script via the URL PHP Docs
So it will hold all the url parameters. You can see this by doing:
var_dump($_GET);
Then you can do something with your $_GET parameters like so:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Yes...
$_GET is an array.
So $_GET[0] could be the value of test
$_GET[1] could be the value of title
So what you need to to is find out how many values are held in the $_GET array or loop through the array:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Also, if your do print_r($_GET); you can see how all the different entries in the array.
Yes, array_keys($_GET) will give you the keys, just iterate through them

PHP get unknown $var from url

I've a page that pass dynamically different $var in URL on another page.
My goal is to retrieve and list this var on the second page in order to pass them to another page.
On page A the user can choose a value via select X, Y, Z (just one, both or as the user likes) the select is passed via form GET.
EXAMPLE
user choice is: X,Z
page B receives http://example.com?X=X&Z=Z
my issue is that I don't know the var name so I cant do $_GET['X'], $_GET['Z']
Please can someone can help?
Many THANKS!!
You can illiterate over whole _GET var like
foreach ($_GET as $name => $value)
And build your request URL
Loop through the $_GET array:
foreach ($_GET as $key => $value) {
echo "$key: $value";
}
Use sensible, known key names:
example.com?choices[]=X&choices[]=Z
var_dump($_GET['choices']);
You can use the $_GET super global array also.
$arguments = array();
foreach( $_GET as $key => $value ) {
$arguments[$key] = $value
}
print_r( $arguments );
$_SERVER["QUERY_STRING"] helps you to detect the query string after the ? in the URL.

PHP post all form inputs without creating variables for each

Usually when I use PHP I create a variable and post statement for each form input like so:
$myVar1 = $_POST["formItem1"];
$myVar2 = $_POST["formItem2"];
ect......
I know if i use:
echo $_POST;
I can get all the key and values from the form inputs but I don't know how to use them in a script.
So I guess I have 2 questions:
How do I quickly post all form inputs without creating a variable for each input?
How do I use a posted in a script without having it assigned to a specific variable?
To simply echo all the inputs, you can use a foreach loop:
foreach ($_POST as $key => $value) {
echo $value.'<br/>';
}
If you don't want to store them in variables, use arrays:
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
Now, $data is an array containing all the data in $_POST array. You can use it however you wish.
You don't have to assign to a variable. You can use directly $_POST['input_name']
If you want to deal with each sended params, you can use foreach loop:
foreach ($_POST as $key => $val)
{
echo "$key : $val <br/>";
}
for this instance of just quickly checking and testing i typically just use the print_r() function: documentation here
as quoted from the docs:
print_r() displays information about a variable in a way that's readable by humans.
one line easy to toggle on and off (with comments)- no need to use any form of variables
print_r($_POST);
if i need my output nice and readable i like to expand it as follows:
function print_r2($x){
echo '<pre>';
print_r($x);
echo '</pre>';
}
and then you can call with: print_r2($_POST);
this way you get the pre-formatted text block on your html page and can see the line breaks and tabbed spacing provided from the $_POST object printout
Another way to extract (besides the extract function) variables is;
$array = array('foo'); // Which POST variables do you want to get
foreach($array as $key) {
if(!isset(${$key})) { // Check if variable hasn't been assigned already
${$key} = $_POST[$key];
}
}
echo $foo;
I would not recommend it because it can get quite messy to keep up.
View everything in $_POST is useful for debugging
echo '<pre>'.print_r($_POST, true).'</pre>';
Access a specific checkbox
HTML
<input type="checkbox" value="something" name="ckbox[]" checked>
<input type="checkbox" value="anotherthing" name="ckbox[]" checked>
PHP
echo $_POST['ckbox'][0]; // something
echo $_POST['ckbox'][1]; // anotherthing
// isolate checkbox array
$ckbox_array = $_POST['ckbox'];
Define a function that allows you to access a specific $_POST item by name (key):
function get_post_value($name, $default)
{
if ( isset($_POST[$name]) ) {
return $_POST[$name];
} else if ( $default ) {
return $default;
}
return null;
}
$default allows you to pass a value that can be used as a fallback if the key you specify isn't present in the $_POST array.
Now you can reference $_POST items without assigning them to a variable, and without worrying if they are set. For example:
if ( get_post_value('user-login-submit', false) ) {
// attempt to log in user
}
You can use extract($_POST), it will create variables for you.
For example, you can have for the code you posted :
<?php
$_POST["formItem1"] = "foo";
extract($_POST);
echo $formItem1; // will display "foo"
?>
EDIT : it's not PHP explode, it's extract.

php string manipulation received by url after installing facebook app

I get this string from URL after installing a facebook page app on several pages.
tabs_added[255408179646]=1&tabs_added[197573531148]=1&tabs_added[225556742602]=1&tabs_added[201931451540]=1&tabs_added[205657687417]=1
I am looking for a way to itterate and be able to loop and echo each numeric value within brackets [].
The values are stored in $_GET['tabs_added'], and you can access them like this:
$tabs_added = $_GET['tabs_added'];
foreach($tabs_added as $key => $value){
echo $key;
}
You should probably check if the values are present with if(isset($_GET['tabs_added'])) first.
The array should be stored in $_GET['tabs_added']. So you can try something like this to loop through it;
foreach($_GET['tabs_added'] AS $key => $value)
{
echo $key;
}
parse_str is made for parsing query strings.
parse_str('tabs_added[255408179646]=1&tabs_added[197573531148]=1', $parsed);
var_dump($parsed);
You can get an array of tabs through either the $_GET or $_POST superglobal variable, depending on whether this is a GET or POST request.
$tabs = $_GET["tabs_added"]; // or $_POST['tabs_added'];
foreach($tabs as $k => $v) {
echo $k;
}
You can also check if the param is present first to avoid an Undefined Index Notice for $_GET["tabs_added"]; and subsequent Invalid argument Warning passing NULL to the foreach loop.
isset($_GET['tabs_added']

How to grab all variables in a post (PHP)

How to grab all variables in a post (PHP)?
I don't want to deal with $_POST['var1']; $_POST['var2']; $_POST['var3']; ...
I want to echo all of them in one shot.
If you really just want to print them, you could do something like:
print_r($_POST);
Alternatively, you could interact with them individually doing something like:
foreach ($_POST as $key => $value) {
//do something
echo $key . ' has the value of ' . $value;
}
but whatever you do.. please filter the input. SQL Injection gives everyone sleepless nights.
If you want the POST values as variables in your script, you can use the extract function, e.g.
extract ( $_GET );
or
extract ( $_GET, EXTR_IF_EXISTS );
There are several flags you can use (check the manual); this one restricts extraction to variables already defined.
You can also use the import_request_variables function.
Cheers
Jeff
Use:
var_dump($_POST);
echo '<pre>';
print_r($_POST);
echo '</pre>';
Use this function in a loop.
extract ( $_GET );

Categories