$_REQUEST showing garbage values - php

I have a strange issue and could not find the reason.
I have url
http://example.com/cp/user_detail?userID=2
So If I print this code
print_r($_REQUST);
it should not print
Array ( [userID] => 2 ...... ?
But its printing this array
Array ( [userID] => 84ac17a3690b4ecd8c8abfba8687e750 [_pk_id_2_2fa0] =>
26c324a269691d77.1410515405.1.1410515405.1410515405. [__utma] =>
24293118.939351632.1410515405.1410515405.1410515405.1 [__utmz] =>
24293118.1410515405.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
[PHPSESSID] => 0394d01235809edd26422eedc400b6b5 )
Is it not strange?
I have a general function for getting get or post value
function chf($value)
{
if(isset($_REQUEST[$value]))
{
if(isset($_POST[$value]))
{
$value=$_POST[$value];
}
else
{
$value=$_REQUEST[$value];
}
$keywords=array();
$keywords=array('update','delete','select');
foreach($keywords as $key=>$val)
{
$value= str_replace($val,'',$value);
}
return $value;
}
else
{
return '';
}
}
How should I changed it so it gives me correct string values?

$_REQUEST includes cookies by default: http://php.net/manual/en/reserved.variables.request.php . The PHPSESSID, __utm* are cookies. If you want just the URL parameters use $_GET.

If you want data from your URL you should use
print_r($_GET);
This wil give you an array like:
Array(
[userID] => 2
)

Related

Laravel change request input from string to array

I'm building an API with custom query filter, in my filter, there are rules that value cannot be empty and some fields has to be an array.
I have managed to filter out empty fields if they are submitted but I can't convert request input to the array, is there a way to do it?
Here is my code:
public function removeEmptyFieldsFromRequest($request)
{
$emptyFields = [];
foreach ($request->all() as $name => $value)
{
if (is_null($value)){
$emptyFields[] = $name;
}
$fields = ['transmissions', 'grades', 'colors', 'equipment', 'lots', 'chassis', 'auctions', 'models'];
if (in_array($name, $fields)){
// here need to convert request value from a string into the array
}
}
$request = $request->except($emptyFields);
return $request;
}
I want to make this filter usable in different cases, I know that I can change the input name to the array on the front end
In Laravel if your parameters is like field[0],field[1],...
you can get it with $request->field and it is array so you can check
is_array($request->field)
and in your case you can check it with below code
is_array($value)
if you query string is like this : /?a=1&b=2&a=3&c=1&a=2.
You can create a function that parses the query string something like this:
$uri = explode('?', Request::capture()->getRequestUri());
$queryStringArr = explode('&',$uri[1]);
$params = [];
foreach ($queryStringArr as $item) {
$i = explode('=',$item);
if (!empty($params[$i[0]])){
$params[$i[0]]=array_merge((is_array($params[$i[0]]))?$params[$i[0]]:[$params[$i[0]]],[$i[1]]);
}else{
$params[$i[0]]=$i[1];
}
}
print_r($params);die;
Which gives.
Array
(
[a] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[b] => 2
[c] => 1
)
I haven't tested it much so give it a thought yourself.

PHP Array - foreach - print key value only if that key name exist in current array

I am getting a response from database in array like this:
Array ( [mage_form_post_status] => draft [mage_form_post_type] => post [mage_form_post_permission] => public [mage_form_post_author] => 1 [mage_form_post_redirect] => 0 )
Array ( [mage_form_post_status] => draft [mage_form_post_type] => post [mage_form_post_permission] => public [mage_form_post_author] => 1 [mage_form_post_redirect] => 88 )
Array ( [mage_gym_name] => Fanna - test [mage_your_name] => John )
What I am trying to achieve is to display mage_gym_name value only.
I have succeed to display it with this code:
foreach($gym_titles as $gym_title){
print_r(unserialize($gym_title));
echo '<br><br><br>';
}
Problem is that when there is no result, like in first two arrays I get 3 empty breaklines.
So I believe that I should wrap:
print_r(unserialize($gym_title));
echo '<br><br><br>';
In some kind of condition which should check if that current array contains that key name.
I have tried like this but it doesn't work:
foreach($gym_titles as $gym_title){
if (array_key_exists('mage_gym_name', $gym_title) {
echo "The 'first' element is in the array";
print_r(unserialize($gym_title));
echo '<br><br><br>';
} else {
echo 'no such element in this array';
}
}
You need to unserialize() before you check for the key:
foreach($gym_titles as $gym_title){
$result = unserialize($gym_title);
if(isset($result['mage_gym_name'])) {
echo "The 'first' element is in the array";
print_r($result);
echo '<br><br><br>';
} else {
echo 'no such element in this array';
}
}
I use isset(), feel free to use array_key_exists() if you must.

PHP Array Check the attribute, return value based on attribute

I am trying to return pull a value based on an attribute from an array, and it seems straight forward enough but I can't seem to nail down the correct way to accomplish this.
Here is the array I am trying to pull from:
[1] => InfoOptions Object
(
[description] => INFO
[optSequence] => 2
[eqpObject] => CUSTOMER NTWK ENG
[attribute] =>
[eqpValue] =>
[dlrSequence] => 10
)
[2] => InfoOptions Object
(
[description] =>
[optSequence] => 3
[eqpObject] => CUSTOMER TEST
[attribute] => CUSTOMER
[eqpValue] => Jon Doe
[dlrSequence] => 10
)
Here is what I have so far:
if (is_array($provisionCVResult->path->infoOptions-_InfoOptions)) {
foreach ($provisionCVResult->path->infoOptions ->InfoOptions as $cv_obj) {
$CVA = array();
$result = null;
foreach ($CV_obj as $value) {
if($value['attribute'] == 'CUSTOMER') {
$CVA["eqpValue"] = $cv_obj->eqpValue;
break;
}
}
$this->cvArrayDataList[] = $CVA;
}
}
Where am I going wrong?
If $provisionCVResult->path->InfoOptions is an array, it does not make sense to write $provisionCVResult->path->InfoOptions ->InfoOptions in the foreach
EDIT: I red in the comments that the array is $provisionCVResult->path->InfoOptions->InfoOptions
PHP is case sensitive so $cv_obj and $CV_obj are two different variables
The second foreach is not needed
So, assuming $provisionCVResult->path->InfoOptions->InfoOptions is returning an array of InfoOptions Object, I think you should do something like this:
if (is_array($provisionCVResult->path->InfoOptions->InfoOptions))
{
$result = null;
foreach($provisionCVResult->path->InfoOptions->InfoOptions as $cv_obj)
{
if($cv_obj->attribute == 'CUSTOMER')
{
$this->cvArrayDataList[] = array("eqpValue" => $cv_obj->eqpValue);
}
}
}
Having a quick look, try changing
$value['attribute'] == 'CUSTOMER'
To
$value->attribute == 'CUSTOMER'
As the element is an "InfoOptions object" and not an array.
Note I would also recommend using strict comparison, e.g '===' instead of '=='.

preg_match not working when I send POST with some fields empty

I have following code to match preg_match function to make post clean.###
foreach ( $_POST as $key => $value) {
if (preg_match("~^[\r\n\s\w\d /<>:,.?#;-]+$~", $value)) {
echo 'Correct';
} else {
echo 'Incorrect';
}
}
I have an POST array in PHP, where some fields may be blank/empty. When I leave some fields blank it gives an error literally here says incorrect instead of accepting. I want them to pass through preg_match even some fields are empty.
I do have following as print_r of POST where some fields are empty
Array {
[email] => mi#mi2.com
[facebook] =>
[twitter] =>
[gplus] => glus
[firstname] => mi2
}
<urregex>|(^$)
Try this.See demo.
http://regex101.com/r/oO8zI4/1

How can I filter out certain items in an array using php?

I would like to filter out a php array based on some search criteria, but it's not quite working.
I've been trying this code I found on google, but it giving an error?
$shortWords = '/'.$_GET['sSearch'].'/i';
$rResult = array_filter($rResult,
function($x) use ($shortWords) {
return preg_match($shortWords,$x);
});
Here is the error:
preg_match() expects parameter 2 to be string, array given
I don't quite know what the "function($x) use...." is doing...my limitations to php.
Here is what the array looks like before the "array_filter()":
array(
[0] =>
array(
['unit_nbr'] =>'BBC 2'
['p_unit_group_id'] =>NULL
['name'] =>1
['unit_id'] =>22640
['properties_id'] =>1450
)
[1] =>
array(
['unit_nbr'] =>'BBC 3'
['p_unit_group_id'] =>NULL
['name'] =>1
['unit_id'] =>22641
['properties_id'] =>1450
)
I would like to have the unit_nbr "BBC 2" remain in the array when I pass that search string over to the function. I don't know what I'm doing wrong.
Any help is appreciated.
Thanks in advance.
The problem is the multi-dimensional array. When you pass along to the callback, $x is the array:
array(
['unit_nbr'] =>'BBC 2'
['p_unit_group_id'] =>NULL
['name'] =>1
['unit_id'] =>22640
['properties_id'] =>1450
)
but you still need to check the items within that array as well.
$shortWords = '/'.$_GET['sSearch'].'/i';
$rResult = array_filter($rResult,
function($x) use ($shortWords) {
foreach ($x as $_x) {
if (preg_match($shortWords,$_x)) {
return true;
}
return false;
}
}
);
Try something like this:
foreach ($rResult as $okey => $oval) {
foreach ($oval as $ikey => $ival) {
if ($ival != $_GET['sSearch']) {
unset($rResult[$okey]);
}
}
}
If that's not what your looking for, then I need more information about what you are trying to achieve.

Categories