$_POST not populated - php

I'm having some issues with the PHP REST API of my app.
I'm submitting my parameters in the correct URL format: baseUrl?param1=val1&param2=val2
However, the parameters are being filled into the $_GET array instead. I've looked into the $_SERVER array to see if there is something wrong there, but I can't see anything obvious.
$_SERVER['CONTENT_TYPE']="application/x-www-form-urlencoded"
$_SERVER['REQUEST_METHOD']="POST"
I've also checked my php.ini for post_max_size misconfig:
post_max_size=32M
I'm not able to figure what else could cause the parameters to be placed into $_GET
If it helps, I'm using XAMPP on Windows.
Also, using "Advanced REST client" for Chrome, I've noticed that regardless of the method, POST parameters are placed into $_GET if the parameters are in the URL, but if I move them into the REST client's Payload field, they show up as POST parameters.

You cannot get parameters via $_POST which are submitted in query-string. You can get them via $_GET array.
but if I move them into the REST client's Payload field, they show up as POST parameters., seeing your trouble, you can always get them with $_REQUEST

Just sharing a thought, The place from where you are initiating post request to backend, you have to use post method.
For example if you are using jquery
$.ajax ({ type: 'POST' })
If angular js
$http.post({ 'your code' });

Related

RESTFullYii does not get data for PUT request

I use RESTFullYii v1.15 from here . RESTFullYii does not get data for PUT request
I faced the problem that making PUT method the data I submit are not inside the doRestUpdate($id, $data) function.
In the link above it is defined as public function doRestUpdate($id, $data).
So it is supposed that data are submit are in $data variable.
Vars $_GET, $_POST, $_REQUEST don't have these data too.
I watch headers of request using firebug and see that data are sent in PUT request, but they are not in doRestUpdate function.
If there is way to fix it?

some URL query variable not working in PHP

My URL: http://www.capstonehomes-mn.com/index.php?cID=127&ccm_token=1363683205:04db0d40a58c3559286c525f299e1fce".
This site was developed using concrete5. This url passing 2 query variables & calling to this page using jquery ajax form. This url doesnt display query variables & its value. what can be problem?.
I would like to get all query variable values in this screenshot: http://my.jetscreenshot.com/14061/20130319-prv0-77kb.jpgat the time of printing$_REQUEST`.
Some URLs working correctly.
My PHP code in target page:
print_r($_REQUEST);
echo $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_URI'] working correctly.
If I will paste this URL in browser address bar query variables working correctly. but not in ajax call.I'm seeing page not found in ajax call ( http://my.jetscreenshot.com/14061/20130319-iami-27kb.jpg )
Some frameworks using an own API for the request part. And in some cases they unset the global $_GET, $_POST and $_REQUEST variables.
If you look at the concrete5 documentation you can see that this framework uses such a API. I think if you use $req = Request::get(); then you can print the request variables.

javascript post to .php file one server does not work - manual entry of URL in browser to same .php works

i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP?
Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense :
i have this function to update an iframe and i want to pass on the page that that iframe is displaying :
function frameclick(pageurl)
{
$.post("session_write.php?",
{
frameurl : pageurl
}
$("#iFrame1").attr('src', pageurl);
console.log ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
}
pageurl is ex. "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).
my script serverside that its posted to is like this :
#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl']))
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>
Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. do it manually like this :
(http):
//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm
then it will be correctly set in the $_SESSION["frameurl"] variable.
I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ?
anyone with an idea ? thanks
You are using .post, which executes a POST request, but when you type in the URL in the address bar, that is a GET request.
$_GET retrieves any params passed through GET, while $_POST retrieves any params passed through POST. So if you use .post with Javascript but try to retrieve with $_GET in PHP, it wouldn't work.
When you POST variables to a PHP file, $_GET is not set. Use $_POST['frameurl'] instead. Also, it looks like you're missing a close paren in frameclick to end the post call.
You are passing data via a
POST request and retrieving for all the GET requests. Use $_POST instead. You may also be interested in $_REQUEST

access post vars JS or PHP?

I am using jquery to post vars to a php script.
Is it possible to access these vars once the script has posted? If I post the data to the php script, is the only way to access it in the html/js that i posted it from, to send it back from the php script as json?
I cant seem to do it in JS, but even php will not work?, Sorry correction i can access the post vars in the php page, but not in the html/js page i posted from
Any ideas how to access posted vars from the page thats doing the posting?
update: yep to be a bit clearer, i am using a html page with javascript to post to a php page, i would like to access the posted vars in the html javascript page. I tried outputting $.post and $.ajax and these just show a long function.
Cheers
Ke
How are you submitting your elements to php page? If you are doing everything fine and using ajax (see jquery post method) for example, you can access the php variables normally with $_POST['var_name'].
Make sure that:
Your form method type is set to POST
Your ajax request goes successful
You have specified the correct path to your server side script
In PHP, the data should be accessible through the $_POST array, just like if you posted to the script using a form (whether you make an AJAX request or a normal request through your browser, the server behaves the same). If they're not there, perhaps you actually sent your data by GET instead (you could check $_REQUEST, but it's better, and more secure, to know what method your data will be coming in), or else your AJAX request failed.
I don't recommend using $_REQUEST to post something back to your site. If someone changes their $_REQUEST vars on you, then you have an opening for cross site scripting.
Push all your vars to $_SESSION and post them as you see fit but only after they have been purified. That way even if you make some modifications to them after the fact you can rely on the source, which is in the $_SESSION. However if you are trying to perform actions after a page has executed you are straying outside the boundaries of PHP's limitations. People do it all the time with things like Jquery but it doesn't make it right.
Warning: if you allow accessing and process of vars after PHP has finished printing the page, then you run the risk of enabling attacks on your code.
I assume that you are using $.ajax or $.post to do this.
Just keep your data in local variables. There i sno reason why you should lose the posted data, or your php not being able to access it.
If you post code, maybe someone can help better.
In your php function, you can use this:
function foo() {
//do something
echo json_encode($var);
}
I use .ajax, use dataType: "json". The success attribute would be:
$.ajax(
{
url: 'ajaxfile.php',
type: "POST",
data: ($("#myForm").serialize()),
dataType: "json",
success: function(data)
{
//Insert your logic to handle the vars from php
}
});

POST or GET in XMLHttpRequest

What does post mean in the following?
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "url" + queryString, true);
because i'm not able to access variables using $_POST['var'] from url but
with $_REQUEST['var'] I can access value..
When you read from $_POST, you should pass your arguments in the HTTP body instead of using the querystring.
You would need to send your arguments as in the following example:
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "your_service.php", true);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send("var=100&another_var=200");
Your are not able to access the parameters via $_POST because you append them to the URL (i.e. they can be accessed via $_GET) and don't send them as POST data.
If you want to send the parameters via POST, have a look at the send() method.
POST is something included in an HTTP request (such as an XMLHTTPRequest).
In your case, you are adding the query string to the URL, which means that it is being passed as a GET variable. Even if it is a post request, PHP can still access any GET variables added on as a query string.
Based on your code, I don't think you are telling the request what info should be included in the POST section of the request, which would explain why you are not seeing anything with $_POST['var'].
But since $_REQUEST['var'] looks for request variables in GET and POST and any cookies passed in the request, you see the variable as it was passed via the query string.
Try echoing $_GET['var'] and you'll see that this is where the variable is getting the data from.
If you want to use POST the right way, you need to not point the request to a URL that has a query string and instead define that query string as the post data.
The post does mean the values are posted, but you should add them as post variables, while now you are only adding them to the url so you can only get them with $_REQUEST and $_GET.
Post data is usually passed in via the post data.
IIRC, you can pass it as an object via the send method.
ajaxRequest.send(requestString)

Categories