-1

So nginx send all the request of the site to the index.php

location / {
            try_files $uri $uri/ /index.php;
}

So when I send mine post request to the /login-post as a post request and try to get the post data. The array is always empty and I dont understand what I am missing cause Im posting towards the correct URL? but the post data is not there?

the Form:

<form class="form-signin text-center" action="/login-post" enctype="multipart/form-data" method="post" style="max-width: 400px">
    <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>

    <div style="width: 100%; display: flex; align-content: end; flex-direction: row-reverse;">
    <button class="btn btn-lg btn-primary btn-block" style="width: 100px" type="submit">Sign in</button>
    </div>
    <p class="mt-5 mb-3 text-muted">&copy; 2017-2018</p>
</form>

the php code that should log the post data from (index.php)

$request = $_SERVER['REQUEST_URI'];
echo $request;

switch ($request) {
    case '/' :
        (new HomeController)->index(); // this works
        break;
    case '/login' :
        (new LoginController())->index(); // this works
        break;
    case '/login-post':
        print_r($_POST); <---- this is always empty
        break;
    default:
        http_response_code(404);
        echo "404";
        break;
}

I dont see whats going wrong? or why I cant see the post data.

HashtagForgotName
  • 651
  • 1
  • 7
  • 23
  • Have you tried to check your browser's network console? Does it contain the data to be posted? – Nico Haase Nov 18 '21 at 12:01
  • Also, why do you need `multipart/form-data` for a simple form with two text fields? – Nico Haase Nov 18 '21 at 12:03
  • the post request is 200 with and empty form data body in the network tab, for the second question I had auto complete on mb for that but that should in this case not make a difference on the post request it self – HashtagForgotName Nov 18 '21 at 12:05
  • Well, if the body is empty, nothing is sent, and nothing can be read out of that empty content. If that does not make a difference, remove `multipart/form-data` to check whether this resolves the problem – Nico Haase Nov 18 '21 at 12:10
  • i removed `multipart/form-data` and the post data is still empty I think it has to do something with nginx and redirecting but im not to sure about it // know not much about nginx – HashtagForgotName Nov 18 '21 at 12:14
  • If the body is empty (as seen in your browser's network console), nginx is not the one to blame. If the browser does not send any form fields, nginx cannot magically invent them – Nico Haase Nov 18 '21 at 12:20
  • Also, see https://stackoverflow.com/questions/12543848/does-form-data-still-transfer-if-the-input-tag-has-no-name for details about your problem – Nico Haase Nov 18 '21 at 12:21
  • Do you think id = name ? (study HTML please) – Ken Lee Nov 18 '21 at 12:26

2 Answers2

3

Your POST is empty, because your <input>s do not have the name='' attribute

Ron
  • 5,900
  • 2
  • 20
  • 30
0

I had the same issue and I could perform a good solution, the problem is related with the htaccess configuration, so lets try adding this in it

RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]

It will allow you to preserve the information that you send thru a form

If it does not work please try to change the whole htaccess for something like this

RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*) $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]
RewriteRule ^(.+)$ index.php [QSA,L]

ErrorDocument 404 /404.php