Page 1 of 1

$_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 11:24 am
by James_PHP
This is a simplified example, so the reasons for the files being set up this way may not make sense to anyone, but they help to illustrate the problem.

Here are two files:

404.php (a custom Error 404 script)

Code: Select all

<?php
include("login.php");
?>
login.php

Code: Select all

<?php
echo '<html><body>';
 
if (isset($_POST['name_submit']))
{
    echo 'You have submitted your username';
} else {
    echo '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">';
    echo 'Username: <input name="username" type="text" /><br />';
    echo '<input name="name_submit" type="submit" value="Submit" />';
    echo '</form>';
}
 
echo '</body></html>';
?>
The problem is that when typing a username and clicking Submit, the form redisplays instead of the success message. I don't understand why $_POST['name_submit'] isn't being transferred through the whole 404/include process.

Now you're probably wondering why I don't just use action="login.php" in the form, but in my real scenario that isn't an option.

Can anyone shed any light?

Re: $_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 12:29 pm
by kryles
I've run into this a little while back. I used a hidden field instead of the actual button name

Code: Select all

 
<?php
echo '<html><body>';
 
if (isset($_POST['name_submit']))
{
echo 'You have submitted your username';
  } else {
 echo '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">';
  echo 'Username: <input name="username" type="text" /><br />';
  echo '<input type="submit" value="Submit" />';
echo '<input type="hidden" name="name_submit" value="true" />'
 echo '</form>';
}
 
echo '</body></html>';
?>
 

Re: $_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 1:03 pm
by James_PHP
Thanks for the suggestion, kryles. But I've made the change and it doesn't seem to be making a difference. :(

Re: $_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 2:03 pm
by SpecialK
I've done a similar thing by grabbing my $_POST variables in a new file with no problem. Perhaps you can dump them with print_r() and see what is incorrect in these post values?

Re: $_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 2:15 pm
by Christopher
I think your problem is that the 404 is doing a redirect, so you $_SERVER['REQUEST_URI'] is the original requested URL, not the 404.php script. So when you post you are getting redirected back to yourself in error each time.

Re: $_POST variables for file "included" in 404 script

Posted: Thu Feb 21, 2008 4:05 pm
by kryles
another off the wall try here

use $_SERVER['PHP_SELF'] instead of $_SERVER['REQUEST_URI']

Not sure what the differences are in the two, but I know $_SERVER['PHP_SELF'] calls the script it is in.