Get all possible $_GET parameters from URL - php

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

Related

get value from array passed in url

I am passing a array value in url
&settings=[{"ID":"1","visibility":"not_selected"},{"ID":"56","visibility":"not_selected"},{"ID":"57","visibility":"not_selected"}]
And getting it in php using $settings=$_REQUEST['settings'] But using
foreach(is_array($settings) as $tag => $val) {
echo $combied_final[$tag]=$val;
}
Does not seems to working in this case . I want ID and Visibility separate . How can i do that ?
To access it as an array, make:
$settings=json_decode($_REQUEST['settings'], true);
and you better use $_GET, otherwise in $_REQUEST you will have also cookies.
First use json_decode to decode the values:
$list = json_decode($_REQUEST['settings']);
foreach($list as $item) {
print $item->ID . $item->visibility;
}

$_GET All the transmitted data

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";

get name of a post variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP $_POST print variable name along with value
I have a form (whatever number of fields).. this form will send $_POST data.. I want to return every $_POST variable value and name.
I wanna do soemthing like this :
foreach($_POST as $field){
//****some code*****//
}
in a way that will display the fields and values like this:
name : Simo Taqi email : example#ymail.com
in other words :
if I have a post variable : $_POST['city']='las vegas'
I want to know how to get the name of the variable : 'city'.
$_POST is populated as an associative array, so you can just do this:
foreach ($_POST as $name => $val)
{
echo htmlspecialchars($name . ': ' . $val) . "\n";
}
Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.
foreach (array_keys($_POST) as $field)
{
echo $_POST[$field];
}
foreach documentation
array_keys documentation
Use the extended foreach syntax:
foreach ($_POST as $key => $value)
{
echo $key . ": ". $value . "\n";
}
I disagree with this post, since it assumes that output is always intended for a browser. One should not get into the habit of this. \n is the correct usage and can be easily converted before output ( to a browser ) using the nl2br() function.

How Can I Pass Array Values through Form Submission?

I am trying to pass an array of values through a form submission. As an example:
example.com?value1=178&value2=345&value3=2356
The easy solution would be for me to do the following to get the values on the new page:
$value1=$_GET['value1'];
$value2=$_GET['value2'];
$value3=$_GET['value3'];
The difficulty that I am having is that the variable after the word 'value' passed through the form will change with each submission. So I have amended the code to pass as:
example.com?value14=178&variable=14&value23=345&variable=23&value63=2356&variable=63
As you can see here, I have now passed the variable that comes in from of the value as a GET parameter. My attempt then GET these values to display individually on the submitted page is as follows:
$variable=$_GET['variable'];
$value=$_GET['value'.$variable];
echo $value . '<br>';
This code almost works. I am able to get the last array which is passed through to display. How can I fix this code to get all of the passed values to display on the submitted page?
Use PHP's array notation for form fields:
val[]=178&val[]=14&val[]=345&etc...
This will cause $_GET['val'] to be an array:
$_GET = array(
'val' => array(178, 14, 345, etc...)
)
If you can't rearrange the URL like that, you can try using preg_grep:
$matches = preg_grep('/^variable\d+$/', array_keys($_GET));
which'll return :
$matches= array('variable1', 'variable2', 'variable3', etc...);
Use an array, for example like this without the need for variable $variable.
example.com?value[14]=178&value[23]=345&value[63]=2356
foreach ($_GET['value'] as $key => value) {
echo $key . " => " . $value . "<br/>";
}
EDIT: Another way for getting the values would be looping the whole $_GET -array and parsing values from there like this (variables are always in the form of "value" followed by X numbers):
example.com?value14=178&value23=345&value63=2356
$values = array();
foreach ($_GET as $key => $value) {
if (preg_match('/^value[\d]+$/', $key)) {
// remove "value" from the beginning of the key
$key = str_replace('value', '', $key);
// save result to array
$values[$key] = $value;
}
}
See http_build_query()

How to return the names of $_GET variables in a url php

I want to save the name of all the $_GET variables in a url, but im not sure where to start, or finish for that matter.
For example:
if i have:
url: test.com/forums.php?topic=blog&discussion_id=12
can i use php to get the name, i.e. "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?
You can get this by calling array_keys on $_GET:
$getVars = array_keys($_GET);
If this isn't about the current URL, but just some $url string you want to extract the parameters from then:
parse_str(parse_url($url, PHP_URL_QUERY), $params);
will populate $params with:
[topic] => blog
[discussion_id] => 12
Use the following code to grab data from the URL using GET. Change it to $_POST will work for post.
<?php
foreach ( $_GET as $key => $value )
{
//other code go here
echo 'Index : ' . $key . ' & Value : ' . $value;
echo '<br/>';
}
?>
$_GET is usual php-array. you may use it in foreach loop:
foreach ($_GET as $k => $v)
echo ($k . '=' . $v);
It's an array:
print_r($_GET);
Fetch the elements as you would with any other array.

Categories