Set multidimensional array in Yii::app()->request->post - php

I am trying to set multi-dimensional array in Yii post:
Yii::app()->request->post(['PaymentMethodForm'][$_POST['PaymentOptionsForm']['payment_option']]['jazzcash_phone'], $phoneNumber);
to replace traditional $_POST.
$_POST['PaymentMethodForm'][$_POST['PaymentOptionsForm']['payment_option']]['jazzcash_phone'] = $phoneNumber;
$_POST code works fine but Yii post doesn't.
I have to replace all $_POST with Yii post.

Yii::app()->request->post() is GETTING value, with default fallback. It is not setting anything. If You need to populate $_POST array, You should use it directly. More info about request: http://www.yiiframework.com/doc-2.0/guide-runtime-requests.html
Your line Would evaluate to:
$name = $request->post('name', '');
// equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : '';
However, because You are not assigning value to any variable, it does nothing.

Related

empty text input means empty $_POST[] element? (Null coallesce)

So my question is:
If I don't write anything in an input
<input type="text" name="test">
in a simple form with post method, when I receive the $_POST array in my action url, the $_POST["test"] exists as empty string ($_POST["test"] => "").
So I can't use null coalesce because $var = $_POST["test"] ?? 'default'; because it is always $var = ""; (as is normal).
Any way to solve this problem?
for Only check if a PARTICULAR Key is available in post data
if (isset($_POST['test']) )
{
$textData = '+text'.$_POST['test'];
echo $textData;
}

Check for the existence of any other POST variable that it is not the required one

I noticed (reading logs of websites I administer), hackers try to submit post requests, literally "inventing" post variables names.
Some website features old PHP code, eg.
if (isset($_POST["mail"]) && !empty($_POST["mail"])) {
//...
}else{
exit;
}
This basically checks if there is a $_POST variable "mail" and it is not empty.
Is it possible to check for the existence of any $_POST variable that it is NOT "mail" and exit the script in that case?
Use array_diff_key to check for differences:
$whitelist = ['mail' => null];
$hasOthers = !empty(array_diff_key($whitelist, $_POST));
I have a different way using filters and not accesing directly to $_POST.
At first, you have to create a definition of what $_POST elements you are interested in. So you have to create an array with the corresponding filters, as example for login definition
$definition = array(
["mail"] => FILTER_SANITIZE_EMAIL,
["passwd"] => FILTER_SANITIZE_STRING
);
Next you can filter all the desirable $_POST elements with filter_input_array
$desirablePost = filter_input_array(INPUT_POST, $definition);
And finalyly you can filter again all the $_POST values usign a filter constant (remembering that all $_POST elements are strings).
$allPost = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
So, to know if someone has injected another $_POST fields, we can compare the count() of both arrays.
if(count($desirablePost) !== count($allPost)){
//error or exit(1) ...
}

Store session value in $_post

hi i know how to store post value into session but how can i store session value into post.
post into session
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mno'] = $_POST['mno'];
$_SESSION['age'] = $_POST['age'];
I have an array stored in session and i want to store it into post.
Can i do this? If yes then how?
I want to store all array value in post from session
The opposite should work without any problems.
$_POST['name'] = $_SESSION['name'];
$_POST['email'] = $_SESSION['email'];
$_POST['mno'] = $_SESSION['mno'];
$_POST['age'] = $_SESSION['age'];
If you want to hold an array you can do it like this:
$datapost = array ( 'name' => $_SESSION['name'], 'email' => $_SESSION['email']);
$_POST['info'] = $datapost;
$_POST is not a persistent store where you can put things. The point of $_POST is that it is populated at the start of the request, with the data that was passed to the server from the client (usually, web browser).
You can write into that array, but it won't have any special effect. It's just an array variable that's globally available in all scopes of your code. Normally, you'd just want to create a new array, and assign whatever you want in there:
$data = [];
$data['stored_foo'] = $_SESSION['foo'];
$data['submitted_foo'] = $_POST['foo'];
See also Why are Global Variables Evil?
If you want to send data back to the browser, you can:
put it in the output (using echo etc)
put it in an HTTP header (using the header() function)
put it in a cookie (which will be sent as an HTTP header, crafted for you by the setcookie() function)

Is it possible to get empty array value via $_GET?

Is it possible to get empty array(array with 0 items) value via $_GET?
Is it possible with direct value setting?
count($_GET['param'])==0
You just need an empty value url = myform.php?param=&param2=
In form just let the value blank:
<input type='text' name='param' value ='' />
For an empty array:
url: myform.php?param[]=&param2[some_key]=
in form: <input type='text' name='param[]' value ='' />
From Ajax: (I remember this was so anti-intuitive and hard to search for):
ajax{
...
data: {'params[]':'','params2[some_key]':''}
}
Workaround:
Just edit the back-end and if there is no data for params or it is not an array (null, empty whatever ..) just assign an empty string to it:
$param = (isset($_GET['param']) && is_array($_GET['param']))? $_GET['param'] : array();
Update:
I did few tests and it seems there is no way to put "nothing" in the request ussing a form or ajax.
0, '', Null are valid values for the $_GET but empty array is not even created.
So to answer your question, it is NOT possible to get empty array value from the front-end.
There are few options to edit $_GET manually in the back-end:
<?php
if(!isset($_GET['param']) || !$_GET['param']){ //not set or (null,0,"")
$_GET['param'] = array();
}
if(count($_GET['param'])==0){...}; // 0 if no 'param' was provided.
If array is empty or containing values that doesn't matters. Just declare a variable and pass the $_GET[] to the variable.
for example,
$para=$_GET['param'];
and now
if(is_array($para))
{
//
}
else{
$para=new array();
}
passing empty array via GET is not possible under normal situation. That said, I can think of a really rare way that the checking you used will return true.
http://domain.com/receiver?param=a%3A0%3A%7B%7D
The above is basically a serialized and urlencoded empty array with the key 'param'
if the target server have some sort of filter that auto unserialize all incoming data, then it might happen.
(or something like the below)
foreach($_GET as $key => $value){
$_GET[$key] = unserialize($value);
}
count($_GET['param'])==0
I know this is a far fetch but it is possible, maybe some private test server that only handles serialized data but accidentally open to public e.t.c.
That said, it is still only passing a serialized empty array instead of a empty array itself. But let's be honest, this answer is more like a joke/fun answer that tries to point out under some very rare case
count($_GET['param'])==0
will return true (Without actively assigning values # server side)

set value to $_REQUEST variable in PHP

Is it possible to assign a value to $_REQUEST variable in PHP? For example:
if ($_REQUEST['name'] == "") {
$_REQUEST['name'] = "John";
}
Yes. $_REQUEST is a variable like any other, but can be pre-populated and is superglobal.
it seems that that variable is read only, if you want to save global variable you can try to make a new variable in the global scope, like this :
$GLOBALS['some_var'] = 'new var';
then you can get the value everywhere in your code, like this :
$your_var = $GLOBAS['some_var'];

Categories