Passing POST parameters to PHP does not work - php

I am sending parameters to a PHP file as a POST request, direct from my browser like :
example.com/
with example.php?CID=1
But not getting the values in the PHP file.
I am sending parameters to a PHP file as a POST request, direct from my browser like
in the php file i am trying to read :
$cid = $_POST["CID"];
$cname = $_POST["CNAME"];
But the local variables are null after executing this.
I also tried doing this :
echo var_dump($_POST);
and it returns :
array(0)
i am hosting this on a paid hosting server, and the configuration allows upto 1000 parameters.

The way you have set this up you are using the get method
$cid = $_GET["CID"];
$cname = $_GET["CNAME"];
If you are wanting to use post then please read the PHP manual
https://www.php.net/manual/en/reserved.variables.post.php

first of all try this PHP "php://input" vs $_POST
php://input
it will solve your issues and let you process developement as workaround - it is very usefull when communicating with pure json not as jason passed into Post field.
next make a check configuration of your server - i had the same problem with nginx and solved it somehow but do not remember how ;)

Related

how to run PHP in a Angualr2 CLI

I am using Angular2 CLI for my frontend framework and using PHP for my backend.
this.http.post('assets/modify.php', '')
.subscribe(result => {
console.log("success post php file");
}
);
I want to use post method to run modify.php. However, I got error:
POST XXXXX/assets/modify.php 404 (Not Found)
I can use get method to read the PHP with the same URL, it is working fine. But how can I use Post to run the PHP.
modify.php:
<?php
//lode the file
$contents = file_get_contents('button.json');
//Decode the JSON data into a PHP array.
$contentsDecoded = json_decode($contents, true);
//Modify the counter variable.
$contentsDecoded['button1Status'] = "booked";
//Encode the array back into a JSON string.
$json = json_encode($contentsDecoded);
//Save the file.
file_put_contents('button.json', $json);
?>
The folder structure is:
app------ user--------------- user.component.ts(I am runing get or post method here)
assets----button.json
modify.php
when I use get method :
Request URL:http://localhost:4200/assets/modify.php
Request Method:GET
Status Code:304 Not Modified
Remote Address:127.0.0.1:4200
Referrer Policy:no-referrer-when-downgrade
when I use post method:
Request URL:http://localhost:4200/assets/modify.php
Request Method:POST
Status Code:404 Not Found
Remote Address:127.0.0.1:4200
Referrer Policy:no-referrer-when-downgrade
**Just for a update from people's help.
I found this and it help me figure out what happened to my scenario:
executing php files in a angular2cli app
SO I am thinking at the development stage, I need to have a web server can run PHP code.Will have a try on the build-in PHP Server.**
Try fully qualifying or at least a good relative URL. I'm going to assume assets is at your docroot, if not then adjust that root slash accordingly:
eg:
/assets/modify.php
or:
https://mywebhost/assets/modify.php
Your example PHP file doesn't seem to care about anything being sent to it, why use the POST method in the ajax, why not just use GET?

Azure Functions with PHP

I'm trying out Azure Functions using PHP.
Getting the request information is not working for me.
I've not been able to find any documentation at all with the information of how to use Azure Functions with PHP code.
According to the only couple of examples, it seems that in order to retrieve the input information you need to first get the content of the req variable (or whatever name you assign in the function configuration).
That has the path of the file containing the request information (in theory).
$input_path = getenv('req');
So far, if I check the content of it, I get something like this:
D:\local\Temp\Functions\Binding\e2b6e195-02f7-481b-a279-eef6f82bc7b4\req
If I check if the file exists it says true, but the file size is 0.
Do anyone knows what to do here? Anyone with an example? Does anyone know where the documentation is?
Thanks
Ok, unfortunately there's pretty limited documentation out there for php as you have discovered.
At present, looking at the code might be the best doc. Here is the InitializeHttpRequestEnvironmentVariables function that adds request metadata to the environment for the script languages (node, powershell, php, python).
Important environment variables are:
REQ_ORIGINAL_URL
REQ_METHOD
REQ_QUERY
REQ_QUERY_<queryname>
REQ_HEADERS_<headername>
REQ_PARAMS_<paramname>
I'm assuming you've made a GET request, in which case there is no content (req is an empty file), but you will see that these other environment variables contain request data. If you were to make a POST request with a body then req would have data.
here is a full example parsing a GET request in PHP with an Azure Function :)
https://www.lieben.nu/liebensraum/2017/08/parsing-a-get-request-in-php-with-an-azure-function/
snippet from source:
<?php
//retrieve original GET string
$getReqString = getenv('REQ_QUERY');
//remove the ? for the parse_str function
$getReqString = substr($getReqString,1,strlen($getReqString));
//convert the GET string to an array
$parsedRequest = array();
parse_str($getReqString,$parsedRequest);
//show contents of the new array
print_r($parsedRequest);
//show the value of a GET variable
echo $parsedRequest["code"];
?>

$_GET not return any value in php

Hi I am facing a problem with respect to $_GET in PHP . I am trying to do payment gateway Integration with EBS. The problem is I am not able to read response I get from URL.
My code to read response is:
$RR=$_GET["DR"];
echo $RR;
$DR = preg_replace("/\s/","+",$_GET["DR"]);
When I try to check the value using echo command it returns null. Can some one please help me understand if I need to follow some other process. Also , this works well in WAMP Server I have which uses PHP 5.4.12 however this code is not working when I am hosting it to different server bought from a vendor. PHP version used there is 5.3.
The URL I have is
http://companyururl.com/response.php?DR=IXc9laP5EPzkG8rJUEkT9GPYZKb+340d1KINeq1DJAbrqc5GeRs3RVwRJ7YShbNZUyaxTmSW46lexsfKVHpZGaEckYB8kpxGvDoGUG9XULC1ZM6XhHu125hgs12+1Ql5jETUw9t8LKV32SFu+2e9n2eDeWfaVS23HN6kQFdC+0KFK8/QdCeEXY1DBNUMX2/1eUCJrLX16tG038+FnvqxqWy6U4nGM7dcwNPnq0PMioKTQs3yXTX3RaVhC83LSbeniKgkYcEBcpZsGMetiadcjQF9qYNNwzL0FgBuah7z+MgIaz1KEDx7/HJ3T8xdunrx8CeGq19oOSp5+lDqFGZZfz8Vb+tCJ4lVzfYoy+037m3jqFtkee4vHmZEkEl2Rl68PZ9shBoLH8iZaO4imURPT53+Kgm3nwFITwyztV1tk/NlG+Y9w3kKV7hlrem/c0SsqLAomL8WORidK0AXxwpitLLuXXWt5bLs14xjVTHfVjRrluSXAg5ylE5M7wt8XV6O1aniKoeapRkKOYyo2BArkIk+92SRWzA24Wb3r+fN5lpyb9aCwOWCi2Of2tikMc/XotK9X5lxix6x0Ec4YRCV2m5f/x7xRDktcKZeJuLOVyxn3Zdx5JzdAseuc/P2vzH8xDCJ6GVcvD+7eeyv1+ZqCnapQmyAWyaCZJ0cQG+YZfKkd30IE5UMRquym0KqrOvrCJZtV4F4T+n8wPq1U4V2j57hfnTp3WzF/l3rjNizCW68Akkmk9d17sVKkGg=
I am trying to read DR value in the form .Looking for a reply.
Check weather request contains data or not by using
var_dump($_REQUEST);
and then check using
var_dump($_GET);
var_dump($_POST);
if you are get null in get and data in post it means you have to check form method from get to post as post is default method for sending data. change it to get method. like this one:
<form action="" method="get">
....
</form>

Strange behaviour when trying to send data with ajax (jQuery)

I'm coding an website which must have same functionality as SO. My server use Litespeed, which increase the speed of runing php files.
When I'm trying to send php code in regular form, everything seems to be ok, data being sent to the action file without interpreting the code inside the variable. But when I'm trying to send the same data in comments textareas, with jQuery $.post method, I'm getting Forbidden 403 error. Data is blocked on the road by Litespeed server. I don't know why is happening this, why is $_POST data auto-evaluated when using ajax?
This situation doesn't appear everytime, but just in some cases, for example:
this is a new message which presents this php code: <?php mysql_query("SELECT * FROM my_table"); ?>
Note that if I remove <?php and ?> from my message, it is ok. I've already tried to use encodeURI() method but this don't change anything.
Is there any setting which must be applied in .htaccess for not auto-evaluate $_POST data?
or I should replace the open and close tags, before sending the comment?
Later edit
This is how I managed to resolve this right now, but I'm still waiting for a suggestion with .htaccess:
$but.prev('textarea').val().replace(/(<\?php)/gi,'-?php ').replace(/(<\?)/gi,'-? ').replace(/\?>/gi,' ?-');
and on the server side
str_replace(array('-?php','?-','-?'), array('`<?php','?>`','`<?'), $_POST['comment']);

How do I send a variable in a URL through AJAX?

I'm sending the variable like this:
xmlhttp.open("GET","insert-karma.php?pid=<? echo $pid; ?>",true);
The AJAX part doesn't seem to be the problem, if I check the Sript the PHP is echoing the integer correctly. (It displays the url as: insert-karma.php?pid=5). However the PHP isn't getting the variable. (I tried echoing and it does't show anthing)
This is the PHP file:
// Connect to db;
$pid = $_POST['pid'];
$sql="UPDATE Poems SET Karma = Karma + 1 WHERE Pid = '$pid'";
// Disconnect form database;
What am I doing wrong? How do I manage to send $pid to the PHP update-karma.php?
try $pid = $_GET['pid']
or
$pid = $_REQUEST['pid'];
You are sending the variable using GET, so in your php you have to use the $_GET variable
$pid = $_GET["pid"];
Also avoid using your variable directly in your sql query. you will be vulnerable to sql injection.
if using mysql:
$pid = mysql_real_escape_string($_GET["pid"]);
or in case you are passing an integer:
$pid = (int)$_GET["pid"];
first you should not use PHP in you ajax requests it's just make things more complicated and PHP is for server side scripting in the first place
secound , you should use xmlhttp.open("POST","insert-karma....) if u plain to use POST
Third the only important difference (not the only but the important) between POST and Get is :
GET requests can be cached
GET requests can remain in the browser history
GET requests can be bookmarked
GET requests can be distributed & shared
GET requests can be hacked lool
so u cant use Get For unsecured and dangerous action like LOGIN OR ...
POST Can handel too long Data
Post is more secured Cuz it's not gonna be cached or saved in history or bookmarked
u can clearly notice that POST's dont display in the browsers address bar but Get do
'www.anything.com/index.php?bla=bhe'
i hope that i am helping here !! :)
Just a suggestion here since you have already got the answer. Try to use some javascript library to aid you while you are writing your JS code.
I would suggest jquery
And also please read what is the difference in GET, POST requests and when it should be used.

Categories