Is it possible to take $_GET parameter from the url and put it in php variable without using foreach?
If I do:
var_dump($_GET);
I will get the result:
array(1) { ["block_simple_clock"]=> string(0) "" }
I need to put that value block_simple_clock in a string variable, and I can do that with a foreach:
foreach($_GET as $key => $value) {
var_dump($key);
}
But I would really like to take that value without a foreach if it is possible. I hate using foreach for such a trivial problem :)
Sorry for confusion, that block_simple_clock is a variable, and it can be a different value, I am sending this value with this code:
foreach ($pluginlist->plugins as $key => $plugin) {
if (empty($plugin->component)) {
continue;
}
if (in_array($plugin->component, $arr)) {
if (!in_array($plugin->component, $contribs)) {
$target_url = new moodle_url('redirect.php');
$mform->addElement('advcheckbox', $plugin->component, "<a href={$target_url}?$plugin->component target='_blank'>" . $plugin->name . "</a>", '', array(0, 1));
}
}
$requestedPluginIdCounter++;
}
As you can see the $plugin->component is the value that I am sending via $_GET.
$variable = $_GET['block_simple_clock'];
OR
$variable = $_REQUEST['block_simple_clock'];
If you know exactly which ones you need, you can use
$var = $_GET['name-in-get'];
You mays want to use the function
extract( $_GET );
Which creates global variables with the name of the key :
//suppose GET = $_GET['name']
extract( $_GET );
echo $name; //will echo $_GET['name']
Remember to ALWAYS filter gets and posts values, since they are user input (obviously it depends what you do with them, but especially if they're used for database purposes, it is extremely important)
For more information, see filter_input function
You can do this :
$t = array_keys($_GET);
echo $t[0];
Thank you guys, I have added a name=$plugin->component, and was able to extract the variable with that $_GET["name"] :)
Related
I have a large HTML form posting many fields to a PHP page. I'm assigning all those fields to PHP variables one by one. Is there a way to put to create a function to auto assign the POST value to a PHP variable?
This is my code now:
if (!empty($_POST["x"])) {
$x = clean_post($_POST["x"]); }
if (!empty($_POST["y"])) {
$y = clean_post($_POST["y"]);
}?>
Thanks!
You can do this using the array_keys() function and a foreach-loop like this:
foreach (array_keys($_POST) as $key) {
${$key} = $_POST[$key];
}
But why not use the $_POST array in the first place?
Yes, but it's a rubbish idea. They actually had this in PHP, but removed it. It was called register_globals. So for instance, $_POST['name'] would automatically have $name created.
If you insist on this terrible idea, you should be able to do it like this:
foreach ($_POST as $key => $value) {
$$key = $value;
}
Don't do it though! Read this for more info https://secure.php.net/manual/en/security.globals.php
I want to look through my PHP $_POST variables and change anything that is "undefined" (because of how jQuery is sending it) to "" or at least " " so that when the email form is submitted it doesn't say "undefined" wherever the value wasn't filled out (optional fields).
Thanks!
I recommend you consider altering your javascript to handle the undefines better, BUT..
Here is a no loops approach to do it:
$_POST = unserialize(str_replace('s:9:"undefined";', 's:0:"";', serialize($_POST)));
There is also the more typical approach of using a single loop like so:
foreach($_POST as &$p) if($p=='undefined') $p = '';
Note: The serialize + unserialize approach is cool in that is does not use a loop, but the loop approach is probably slightly faster, especially when $_POST is large.
In PHP if you use an & symbol in an foreach loop it will use it as a reference instead of a value. Changing a reference will set the original value.
<?php
$_POST['text1'] = 'undefined';
$_POST['text2'] = 'undefined';
foreach($_POST as &$var)
{
if($var == 'undefined')
$var = '';
}
print_r($_POST);
?>
<!-- Will output -->
array(
text1=>
text2=>
)
if (isset($_POST['var'])) {
// it is set
} else {
// it isnt set
}
foreach ($_POST as &$var) {
if ($var === 'undefined') {
$var = '';
}
}
$prefix = 'some';
$name_of_variable = $prefix.'_var';
So I have a variable named $some_var.
How can I check the value of it?
if($name_of_variable) ...
will return the value of $name_of_variable instead of the value of $name_of_variable.
Variable variables. But you do NOT want to use them. They make for impossible-to-debug code. They're almost always a sign of bad design.
DO NOT use a variable which is partially created from a string.
Use arrays instead.
$prefix = 'some';
$name_of_variable = 'var';
echo $array[$prefix][$name_of_variable];
Variable variable usually used when you need to create variables from string,for example convert $_POST keys into variable with its value .
$allowed_var = array('name',..);
foreach( $_POST as $key => $value
{
if( isset($allowed_var[$key] ) )
${$key} = $value;
}
...
Here's a php function and it works perfectly.
$values = array(
'php' => 'php hypertext processor',
'other' => array(
'html' => 'hyper text markup language',
'css' => 'cascading style sheet',
'asp' => 'active server pages',
)
);
function show($id='php', $id2='') {
global $values;
if(!empty($id2)) {
$title = $values[$id][$id2];
}
else {
$title = $values[$id];
}
echo $title;
}
When i execute this <?php show(other,asp); ?> it displays active server pages and it works, but when i do it this way it shows an error
<?php
$lang = 'other,asp'
show ($lang);
?>
It doesn't work., Please help me out here
P.S : It works if i pass variable with single value (no commas)
If you'd like to pass it in the way you have it,
maybe try using explode:
function show($id='php') {
global $values;
$ids = explode(',',$id);
if(!empty($ids[1])) {
$title = $values[$ids[0]][$ids[1]];
}
else {
$title = $values[$ids[0]];
}
echo $title;
}
You can't pass two variables in one string. Your string $lang needs to be split up into two vairables:
$lang1 = 'other';
$lang2 = 'asp';
show($lang1, $lang2);
It fails because the key "other,asp" doesn't exist in $values.
In other words, it is trying to evaluate the following:
$title = $values['other,asp'];
PS, it's always useful to provide an actual error rather than saying "it doesn't work".
This is because $lang will get interpreted as a single argument, so $id2 will be 'other,asp'. You need to pass them into the function separately:
$id1 = 'other';
$id2 = 'asp';
show($id1,$id2);
P.S : It works if i pass variable with single value (no commas)
You are assigning $lang to the value 'other,asp', and then passing that sole $lang variable to the show function. There is no key named "other,asp" in your $values array.
Having a comma in the string doesn't mean you're splitting the parameters, it means you're passing a single string value. You have to "pass a variable with a single value", or do it like this for multiple parameter values:
$lang = "other";
$sub_lang = "asp";
show ($lang, $sub_lang);
You are returning one string instead of the two required... how about rewriting your function to handle them instead?
There's got to be a much more elegant way of doing this.
How do I convert all non-empty post data to session variables, without specifying each one line by line? Basically, I want to perform the function below for all instances of X that exist in the POST array.
if (!empty($_POST['X'])) $_SESSION['X']=$_POST['X'];
I was going to do it one by one, but then I figured there must be a much more elegant solution
I would specify a dictionary of POST names that are acceptable.
$accepted = array('foo', 'bar', 'baz');
foreach ( $_POST as $foo=>$bar ) {
if ( in_array( $foo, $accepted ) && !empty($bar) ) {
$_SESSION[$foo] = $bar;
}
}
Or something to that effect. I would not use empty because it treats 0 as empty.
The syntax error, is just because there is a bracket still open. it should be
$vars = array('name', 'age', 'location');
foreach ($vars as $v) {
if (isset($_POST[$v])) {
$_SESSION[$v] = $_POST[$v];
}
}
It should work like that. If you use
if ($_POST[$v])...
it strips down empty data.
Here you go,
if(isset($_POST) {
foreach ($_POST as $key => $val) {
if($val != "Submit")
$_SESSION["$key"] = $val;
}
}
Well the first thing I would suggest is you don't do this. It's a huge potential security hole. Let's say you rely on a session variable of username and/or usertype (very common). Someone can just post over those details. You should be taking a white list approach by only copying approved values from $_POST to $_SESSION ie:
$vars = array('name', 'age', 'location');
foreach ($vars as $v) {
if (isset($_POST[$v]) {
$_SESSION[$v] = $_POST[$v];
}
}
How you define "empty" determines what kind of check you do. The above code uses isset(). You could also do if ($_POST[$v]) ... if you don't want to write empty strings or the number 0.
This lead me to this bit, which is a simplified version of #meder's answer:
<?php
$accepted = array('foo', 'bar', 'baz');
foreach ( $accepted as $name ) {
if ( isset( $_POST[$name] ) ) {
$_SESSION[$name] = $_POST[$name];
}
}
You might also substitute !empty() for isset() above, though be aware of how much farther-reaching empty() is, as #meder pointed out.
$_SESSION["data"] = $POST;
$var = $_SESSION["data"];
Then just use $var as a post variable. For example:
echo $var["email"];
instead of
echo $_POST["email"];
Cheers