Slim 4 Application throwing a HttpMethodNotAllowed on file upload - php

I created an application using php slim 4. When I submit a form with a file attached to it, It works perfectly on my local machine but when I hosted it on the server, it also works but only a certain conditions.
If the filename has some certain characters, the application throws an 405 HttpMethodNotAllowed exception.
With the same filename, I tested it on previous applications I hosted using php slim 4 as well but they work well on those servers.
I later tried to use slim 4 methodoverridemiddleware by adding an X-Http-Method-Override with a POST value header to force the request method to POST. It doesn't throw the 405 HttpMethodNotAllowed anymore but the request strips off the body of the file.
Please has anyone ever experienced this before and what do you think might be the solution

Related

503 in php file when making an API Call

I am having trouble with several php files.
The problem is when I try to access my WebApp in said php files it results in a 503 error. Both files have API calls to different services that have worked perfectly until yesterday. When I remove the API call from the code the WebApp opens with no problem. I called both API services and they are both getting the API requests made from my server with no problem.
I've already exhausted all options to fix this error but came up with no results.
Any help is valid.
Thank you so much.
I've tried to change PHP version, but as I said there is no problem with PHP. Removing the API call makes the webapp function normally.

php $_SESSION on server undefined on local angular application

I am working on a project which require sessions. I have my PHP files on a server and I am coding the front end in Angular, locally at the moment.
I am creating a $_SESSION in my php file for when a user logs in. Creating the post request on my local angular project, any $_SESSION property is returned as empty (e.g. $_SESSION['key'] is empty). However, it works fine when testing on Postman, and also works fine when I compile my angular app and put it on the server... So it works on Postman, and on the angular app on the server, but not the angular app locally (with the exact same code).
I have added CORS to my PHP files, and can use many other post/get requests locally, just not the session.
Is there any particular reason this doesn't work, or a way to get it working locally? As I don't want to compile and upload the project every time to test it out.
Any other information needed, please let me know.
Thank you in advance.

Create React App, data via AJAX, PHP page backend on XAMPP Apache

I'm trying to learn React by replicating a simple CRUD page I created earlier.
The old CRUD page was a Javascript/jQuery front-end with a form. It sent data via AJAX to a PHP backend page, and the PHP page interacted with a mySQL/mariaDB database.
Everything was locally hosted on my PC on XAMPP Apache, and everything worked fine.
I have now managed to replicate the front end using React, via Create React App and it's running on localhost:3000
I am now stuck. I don't know how to make my front-end (on localhost:3000) send data via AJAX to my back-end PHP page (on localhost:80 running on XAMPP Apache)
There are a lot of us PHP / mariaDB types out there who want to continue using that sub-stack as our back-end. The answers to this question will be enormously helpful in encouraging our crowd to try Create React App and React (I'm loving React btw!). (I was surprised that when I searched [create-react-app] [php] in StackOverflow, I got only 2 hits.)
Update: if this is off-topic for SO, then I'd appreciate suggestions on which SE I should post this on... thanks!
Answering my own question.
The basic problem is one of CORS (cross-origin resource sharing). The error one gets is (in the Chrome console):
No 'Access-Control-Allow-Origin' header is present on the requested resource.
During development only, this issue is easily resolved by adding this line of code to the top of the PHP file:
<?php
header("Access-Control-Allow-Origin: *");
This code should never be left there in production unless you're okay with anyone in the world accessing the PHP file API.
In actual production of this React app, one would expect that the JavaScript and the PHP files would share the same domain, so there won't be a CORS issue.

PHP & Angular Rest Calls, Cross Origin Issue

I am developing my app using Angularjs for front end and php mysql for backend. in php i am using slim framework. Things went smoothly while i was developing on my local machine and testing on localhost. However when I uploaded the code to a free shared hosting, all my rest calls stopped workng and started getting the following error.
XMLHttpRequest cannot load http://error404.myhostingprovider.com/?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myfreehosingdomain.com' is therefore not allowed access.
Can anybody please tell me what is going on.
In short, websites have to be careful about including content from other websites, because that content may be undesired / evil or changed en-route. Websites are generally responsible for whitelisting cross-domain requests they are expecting to make for the sake of the client's security.
I would suggest that you read up on a Browser Content Security Policy: https://en.wikipedia.org/wiki/Content_Security_Policy.
Then take a look at how to handle this case in the slim framework. This might be a good starting point:
http://help.slimframework.com/discussions/problems/810-no-subject
In my case this was because I was being redirected to an erro404 page outside my domain. The actual error was due to other factors (no mysql native driver in my case).

Custom http methods on Cloud9-IDE

I've written some API code for a project of mine, on my local machine (Wampserver), there's no problem sending custom request methods, and the stack handles them the same as GET and POST.
In Cloud9, however, all the basic methods go to the application bootstrap (a PHP file), but custom method names, for example "GENERATE", which I totally made-up, is stopped before the PHP bootstrapper with the following message: "Something went wrong! Server timed out or sent incomplete response" with error code 502.
I attempted to investigate and fix it myself, but couldn't. I don't even know which behaviour (Wampserver or Cloud9) is the default expected behaviour.
Can anyone help? How do I tell Cloud9 instances to handle custom methods the same as regular ones?

Categories