Why is $_SERVER['REQUEST_METHOD'] always GET? - php

I'm a bit confused about this. I'm hoping it's something wildly obvious I've missed! I have a very simple form:
<form class="form-signin" role="form" name="login" method="POST" action="/page">
<input type="password" name="password" />
<input type="submit" value="Sign in" />
</form>
Note: this page lives at /page and is echoed after the following HTML:
On /page I have this at the very top of the file:
<?php
var_dump($_SERVER['REQUEST_METHOD']);
For some reason, it always shows up as GET when I submit this form. If I take the action="/page" part out then it shows up as POST. What am I missing here?
Note: Even when I load the page, then put at exit after the above var_dump() call, it still shows GET.
In the inspector's timeline I see this for the request:

Thanks to the comments to my question I have found the answer to be in apache configuration. It appears that, because the index.php file is inside a folder called page, apache will automatically redirect to the page with a slash on it. This is the default setting as seen in the Apache directorySlash documentation.
As they warn against turning this off, I will just change the url to what I'm posting. Alternatively, of course, I could add a .htaccess file with proper rewrite rules setup.\
Thanks for everyone's help! As a side note, Safari's inspector left me a little wanting in this case. Chrome turned out to be a far better option for testing.

Related

Post does not work

I have a really annoying problem.
I have a form and when I Submit it it doesn't set the post.
<form action="pages/post-reply" method="post">
<div class="row">
<div class="large-12 columns">
<textarea name="comment" placeholder="Comment on admin"></textarea>
</div>
</div>
<input type="submit" name="submit" class="tiny button" value="Post"/>
</form>
I use a framework called Processwire and Foundation but I don't think this has anything to do with it.
When I try it out on my webserver (a dedicated host) it works. I am using a WAMP install on Windows 8. Could this have anything to do with it?
When I use:
echo $_SERVER['REQUEST_METHOD'];
It just says:
GET
Edit: .htaccess file:
http://textdump.net/raw/2212/
Still not any progress, does anyone know if it might be Wamp or Windows 8 screwing this up?
This may be caused by redirection either from the page of pages/post-reply itself or .htaccess rule on the root directory who is redirecting the request pages/post-reply as a GET Request.
Check your .htaccess rule
According to your .htaccess file, this is the line that is redirecting your request to a different URL
RewriteRule ^(.*)$ index.php?it=$1 [L,QSA]
However, modifying this rule will probably crack your application, so basically what happens is the request gets send to the script as
index.php?it=pages/post-reply
So may be this is help you solve further.
I know this is a very old question, but the right answer might just benefit someone. The first line should read.
<form action="<?php echo $config->urls->templates ?>pages/post-reply" method="post">
See http://cheatsheet.processwire.com/ for details.

PHP POST not working

<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>
This code should print whatever is enter in text box name="ss" when click submit.
But its not printing. Working with method="get" but not with post, What's the problem.
If you're just refreshing the page, do:
action=''
instead of:
action="<?php echo $_SERVER['PHP_SELF'];?>"
Also, add this to line 2 to see what's being stored (if anything) in the $_POST array:
var_dump( $_POST );
Hmm... so it's empty on submit? Try adding this to the top of your php file:
if(empty($_SERVER['CONTENT_TYPE']))
{
$_SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded";
}
Okay, now check your php.ini (normally requires sudo or root in /etc):
post_max_size = 8M
variables_order = "EGPCS"
Do you have those two rules set? If so, be careful of how much memory you're allocating. Anything over 2048MB could start to give you trouble, depending on your system specs.
NOTE: If you make changes to your php.ini file and PHP is running as an apache module, you'll need to restart apache. Something along the lines of:
sudo /etc/init.d/httpd restart
I broken my post method once that I set post_max_size the same with upload_max_filesize.
I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0
It may be due to rewrite rules in the .htaccess file.Add this condition to your .htaccess file
RewriteCond %{REQUEST_METHOD} !POST [NC]
OR add this line
RewriteRule ^welcome_post.php - [PT]
Finally ...Got it.... Firstly I have an Article folder in my htdocs file with welcome.php as the php file in focus and a main HTML file , whose contents we will be discussing about.
Here's what worked for me..
Turns out the problem was not XAMPP or any of its related files..
The problem was this :
<form action="welcome.php" method="post">
With this code whenever I pressed the submit button, the url redirects to here file:///C:/xampp/htdocs/Article/welcome.php, Which is not what it needs to be..It has to be a localhost link ..So I changed the value of action attribute to localhost form and now it looks like this
<form action="https://localhost/Article/welcome.php" method="post">
That did the trick..Now the submit button redirects to https://localhost/Article/welcome.php , this link..Which is exactly what is needed..
Remember the browser may ask you for some permission issues ..Just accept them it will run fine...
Best of luck..
P.S : This one is for Windows..Hope it will work also in Linux and Mac.
My friend ran into this problem today. The answer was pretty simple - basically, you have to capitalize the POST part of method="POST"
The final result should look like
<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>
First make sure that your web service (GET/POST etc) is acting as desired using the Chrome Advanced Rest Client. Then you should check your PHP part.
I solved mine with including following into header.
Content-Type: application/x-www-form-urlencoded
Just use this in the header while making a request and my problem was solved.
<form action="" method="post"> method="post" is important for POST Data.
Use PHP REQUEST instead:
<form action="" method="post">
<input type="email" name="mail">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_REQUEST['submit'])){
$val= $_REQUEST['mail'];
echo $val;
}
use this instead;
$variable_name = $_REQUEST["ss"];
echo $variable_name;
change your IDE ,i use phpstorm ,it's fantastic but when i use dreamweaver it works probably, for test you can run your page directly from wampserver localhost , i change default port of apache and i think problem is from there , if you use phpstorm or change port of apache server change your IDE.

PHP $_POST not working? [duplicate]

This question already has answers here:
PHP POST not working
(10 answers)
Closed 8 years ago.
I have the simplest form possible and all I want to do is echo whatever is written in text box.
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_POST['submit'])){
$test = $_POST['firstname'];
echo $test;
}
The problem is it's not working on my server (it works on another server). Does anyone has an idea what could be wrong? There are other forms on the server and are working fine.
I had something similar this evening which was driving me batty. Submitting a form was giving me the values in $_REQUEST but not in $_POST.
Eventually I noticed that there were actually two requests going through on the network tab in firebug; first a POST with a 301 response, then a GET with a 200 response.
Hunting about the interwebs it sounded like most people thought this was to do with mod_rewrite causing the POST request to redirect and thus change to a GET.
In my case it wasn't mod_rewrite to blame, it was something far simpler... my URL for the POST also contained a GET query string which was starting without a trailing slash on the URL. It was this causing apache to redirect.
Spot the difference...
Bad: http://blah.de.blah/my/path?key=value&otherkey=othervalue
Good: http://blah.de.blah/my/path/?key=value&otherkey=othervalue
The bottom one doesn't cause a redirect and gives me $_POST!
A few thing you could do:
Make sure that the "action" attribute on your form leads to the correct destination.
Try using $_REQUEST[] instead of $_POST, see if there is any change.
[Optional] Try including both a 'name' and an 'id' attribute e.g.
<input type="text" name="firstname" id="firstname">
If you are in a Linux environment, check that you have both Read/Write permissions to the file.
In addition, this link might also help.
EDIT:
You could also substitute
<code>if(isset($_POST['submit'])){</code>
with this:
<code>if($_SERVER['REQUEST_METHOD'] == "POST"){ </code>
This is always the best way of checking whether or not a form has been submitted
Dump the global variable to find out what you have in the page scope:
var_dump($GLOBALS);
This will tell you the "what" and "where" regarding the data on your page.
I also had this problem. The error was in the htaccess. If you have a rewrite rule that affects the action url, you will not able to read the POST variable.
To fix this adding, you have to add this rule to htaccess, at the beginning, to avoid to rewrite the url:
RewriteRule ^my_action.php - [PT]
Instead of using $_POST, use $_REQUEST:
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_REQUEST['submit'])){
$test = $_REQUEST['firstname'];
echo $test;
}
Have you check your php.ini ?
I broken my post method once that I set post_max_size the same with upload_max_filesize.
I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0
FYI:
$_POST in php 5.3.5 does not work
PHP POST not working
try doing var_dump($_GLOBALS).
A potential cause could be that there is a script running before yours which unsets the global variables. Such as:
unset($_REQUEST);
or even.
unset($GLOBALS);
This could be done via the auto_prepend_file option in the php.ini configuration.
try this
html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
php code:
if(isset($_POST['Submit'])){
$firstname=isset($_POST['firstname'])?$_post['firstname']:"";
echo $firstname;
}
There is nothing wrong with your code. The problem is not visible form here.
Check if after the submit, the script is called at all.
Have a look at what is submitted: var_dump($_REQUEST)
html file and php file both should reside in htdocs folder in c:\apache2 (if you use apache web server).
Open html file by typing http://"localhost/html_file_name.html"
Now enter Your data in fields.. Your code will run..
Try get instead for test reasons
<form action="#?name=test" method="GET">
<input type="text" name="firstname" />
<input type="submit" name="submit" value="Submit" />
</form>
and
if(isset($_GET)){
echo $_GET['name'] . '<br>';
echo $_GET['firstname'];
}

how to enable cross domain POST-ing in PHP?

I'm tying to send POST data from one site to another (both sites have been developed by us).
The problem is that the POST variables are not available if the page is requested from another domain.
Even if I test it locally, but specify the complete url, the POST data is gone.
So, this will work:
<form method="POST" action="test.php">
But this will not:
<form method="POST" action="http://example.com/test.php">
Here is the HTML for the page:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="http://example.com/test.php">
<input type="text" name="request" value="" id="" />
<input type="submit" value="" id="" />
</form>
</body>
</html>
After the comments I got that this should work, I tested it on another server and there everything worked fine indeed. This might have something to do with the fact on the first server https is enabled. But if this is the case, I find it strange that I do get information back but that just the POST data has gone missing.
What you have should work fine - forms came before the same-origin policy - you're allowed to submit to different domains.
If I were to hazard a guess, I'd say there's a 301 or 302 redirect getting in there somehow? Follow the HTTP headers with Firebug for example to be sure.
As others have said, there should be no problem doing this. I would suggest replacing your test.php script with something simple, like this
<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
You should find it works, which means the blame lies somewhere in the "real" script...
Maybe also a timesaver:
If you POST to domain.com make sure it doesn't get redirected to www.domain.com.
The redirect doesn't take the POST variables into account , only GETvariables.
If it does get redirected to the www.domain.com add the www. in your POST-action
Thank you. I too found out that redirection to www and https was blocking the performance of my $_POST request.
By changing my action to include https://www.
I fixed my problem.
Thank you

URL rewriting and GET forms in Apache/PHP

I enabled URL rewriting on my PHP site with Apache (http://example.com/index.php?param=12 becomes http://example.com/index/param/12).
I have a few forms which are in GET instead of POST.
After subitting the form, the resulting URL is not rewritten.
Is it possibile to keep rewritten URLs after submitting a GET form?
UPDATE: I found this article on the topic http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewritten-urls but I really don't like the idea of redirecting to rewritten URL. Is there really no way to keep rewritten URLs without redirecting?
UPDATE 2: Here is an example of what I'm trying to do.
Let's say I have a simple form like this:
<form method="get" action="">
<fieldset>
<input type="text" name="q" />
<input type="submit" value="Search" />
</fieldset>
</form>
and let's say my url is http://example.com/index/param/12
After submitting the GET form, the url becomes http://example.com/index/param/12?q=my-input-text, while I would like to get a rewritten url like http://example.com/index/param/12/q/my-input-text
Seems like if you want your form to go directly to /q/my-input-text you should use JavaScript to make that happen on the form's onSubmit.
apache mod_rewrite only processes incoming (request) urls - it has no control of the urls you generate in your php scripts. This is something you should take care of yourself.

Categories