$_POST as a variable - php

Hello guys i have seen a code which uses $_POST as a variable .The code is
private
$something
function example()
if($_POST) $_POST = $this->$something-1;
if($_SESSION) $_SESSION = $this+1;
if($_COOKIES) $_ COOKIES = $this->something
In this code i have seen $_POST as a variable .Can we us $_POST as a variable instead of
$code = $_POST['code']
I am sorry if i asked this question wrongly .Please help me ..:) .Thanks in advance

A legitimate use of $_POST without a key as in
$_POST['email']
would be ...
is_array($_POST)
This is not using $_POST as a variable, but it is an example of using the $_POST identifier without trying to access one of its elements. Based on your example, this might be something everyone should do before using $_POST?

This has been asked a few times and the general consensus seems to be that there is no issue referencing the $_POST variable directly. In fact by using the line
$code = $_POST['code']
You are in fact adding more code and more work for yourself.
But it is entirely up to you as to how you use $_POST, either way is fine.
Update
You do not necessarily need data posted to the page in order to use $_POST see below
echo '<pre>';
print_r($_POST); //return blank array as nothing has been posted to the page
echo '</pre>';
$_POST[] = 1; //even though no data has been posted we can use $_POST as a variable if we wish
echo '<pre>';
print_r($_POST); // This will print an array where $_POST[0] is equal to 1
echo '</pre>';
$_POST[0] = $_POST[0] + 1;
echo '<pre>';
print_r($_POST); // This will print an array where $_POST[0] is equal to 2 as we added one earlier
echo '</pre>';
So here we can see the use of $_POST. In the third print we have altered the $_POST[0] value. You can also as shown in your code, make $_POST represent a single value rather than an array which is default.
Therefore if we do say
$_POST = 1+1;
echo '<pre>';
print_r($_POST); // This will print 2
echo '</pre>';
This will print 2.
Much like any variable you can use $_POST as a variable name, though I would not recommend this unless dealing with posted data.

Related

PHP $_POST array print_r

I have a general question. I wrote the below code to add an array of arrays to the $_POST variable. I know the post variable is an array too. So, after adding my multidimensional array to the post variable one by one, I tried to print_r the data to view it but, only one array would print out.
why is it that only one array would print out?
$x = 0;
for($x; $x < count($return_auth); $x++){
$_POST = $return_auth[$x];
}
print_r($_POST);
Although nerdlyist answer is absolutely right, but I think it is not the right way, in short, you are modifying $_POST variable and since $_POST variable has a meaning attached to it that it contains all the parameters sent in that POST request. If you overwrite it that change will be affected throughout your application and other modules running in your application will see an additional post parameter which is not actually sent in that request in $_POST array which is not right IMO.
What you are doing is overwriting post. Based on what you have here you could do something like this:
$_POST[return_auth] = $return_auth;
Unless you have a reason to loop an array to create an array...
use this in your for loop
$_POST[] = $return_auth[$x];
Edit
this will work better
$_POST['something' . $x] = $return_auth[$x];
now you can access to (foo) for example
$_POST['somethingfoo'];

How to check if *any* GET field is populated html php

Situation: An attempt to prevent DOM-based XSS.
My solution: Check if ANY GET fields are populated, since most DOM-based XSS appends to the end of URLs such as https://example.com/page?<script>XSS</script>
Question: Is there any way I can check if there is anything filled after "?", which usually holds the $_GET fields.
Additional information: I tried $_GET['']."%" to try to fish out anything that is there, did not work.
This is what appears to me using http://domain.tld/page?<script>XSS</script>.
$_GET is not empty as you say.
Just iterate every single $_GET attribute and make sure they are sanitized prior any data processing with your business model.
foreach ($_GET as $key => $attribute)
{
// do something with $key or $attribute!
}
There are several way possible .. eg:
You could iterate over $_GET
foreach( $_GET as $key => $value) {
// if any you can perform your check
}
or you can check for
if (isset($_GET) {
count($_GET) ; // the you know the number of elemns
}
You can use $_GET. Just print_r($_GET); If you have anything in query string after ? then data will be in $_GET array. If the $_GET array is blank then there is no data in query string
echo "<pre>";
print_r($_GET);
echo "</pre>";
Go to http://php.net/manual/en/reserved.variables.get.php

Using $_POST as a variable

Hello guys am a newbie to php .I am here to ask a question as usual.I have seen a code like this ..
switch($_SERVER['REQUEST_METHOD'])
{
case 'GET': $the_request = &$_GET; break;
case 'POST': $the_request = &$_POST; break;
.
. // etc.
.
default:
}
?>
My doubt is $_get here .As we know s_get is used to post the details of the variable to the server.But here $_POST is used as a variable ..
My question is that can we use $_POST as a variable like $_POST = $something..
Am sorry if my question is not up to the standdard since am a newbie ..Any help would be appreciated ..Thanks .:)
yes, $_POST its just an array.
In PHP $_POST is a superglobal array that is populated by data sent via the http post method.
You can manually add to the array, delete things from the array and manipulate it in any way you can manipulate other arrays in PHP.
See the manual page
$_POST is normal array, visible in global scope (see docs). There's rather no point of assigning its values to another array as in most cases there's no benefit from doing so.
As we know s_get is used to post the details of the varibale to the
server
Wrong. $_GET is used to grab the data that you have passed through the querystring of your URL.
an we use $_POST as a variable like $_POST = $something..
Yes but not directly.. You could do this way..
<?php
$somevar = $_POST;
echo $somevar['name'] = 'John';//
You can directly use $_GET and $_POST variables as a normal array.. you can add values, assign values, alter, delete values from it.
There is no need to create a new array and assign values of $_GET or $_POST to it except in some special cases.

PHP - output values of a variable

I am a newbie to php. I am working on a existing wordpress site. I want to print the values of a variable $post as output. What is the equivalent of Java's System.out.print in php.
While googling i found the same, system.out.print for php but its not working.
--
Thanks
You can use echo to print something: docs
$_POST is an array, if you want to examine its value for debug purposes use var_dump (docs):
var_dump($_POST);
To access array's elements, use square brackets: $_POST['index']. PHP arrays may be associative, which means they are indexed not only by numbers, but also by strings. $_POST is a pure associative array, indexed only with strings.
To print POST parameter called username use this code:
echo $_POST['username'];
or
echo($_POST['username']);
Both will work because echo is not a function, but a language construct (see docs linked above).
printf("var = %s", $var);
or
echo $var;
or (for objects, arrays, etc)
print_r($_POST);
or
var_dump($_POST);
Check php.net for more!
PHP's echo construct is probably what you want.
<?php echo $_POST['variable']; ?>
where variable is the name of the form element.

Variable in $_POST returns as string instead of array

In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.
I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!
Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }
If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
​
Array
foo
$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];

Categories