0

I'm doing form validation using a PHP script. I initially wrote this code in 2007 but now it just stopped working, and I've been trying to figure out why.

Here's the code:

<?php
$error_msg = '';

// Only Validate Form when it is submitted
if (isset($formSubmit)) {
   if (!isset($_SESSION["First_Name"])) {
     $get_mbr_id = urlencode ($_POST["GetMbrID"]);
     $_SESSION["MemberID"] = $get_mbr_id;
     }

   if (!headers_sent()) {
     header ("Location: mywebsite.com");
     exit (0);
     }
}

if (isset($formExit)) {
  if (!headers_sent()) {
    header ('Location: mywebsiteexit.com');
    exit (0);
    }
}
?>
<html><head></head><body>
<form name="select_action" method="POST" action="select_action">
<br>
<center>
<input type="submit" name="formSubmit" value="Next">
<input type="reset" name="fieldReset" value="Reset">
<input type="submit" name="formExit" value="Cancel">
</center>
</form></body></html>

If the HTML form code is present, then the header redirect doesn't work.

However, if I remove the HTML form code, change the if(isset(formSubmit)) statement to if(!isset(formSubmit)), then the header redirect will work.

I can't figure what is happening with the form code that causes the header() redirect not to happen.

Any help would be appreciated!

3
  • Have you tried printing the contents of $formSubmit through var_dump() or the likes? It sounds like you (or your host) disabled the register_globals setting. Commented Jan 20, 2010 at 15:29
  • 2
    That's not going to solve your problem, but any rate, the RFC for HTTP states that the Location header expects a full URL, like http://www.example.com/. Commented Jan 20, 2010 at 15:29
  • +1 zneak. Not just an academic concern; browsers do unexpected things with relative URLs, and certainly if you're expecting Location: mywebsiteexit.com to actually go to the site http://mywebsiteexit.com/, that will never happen. Commented Jan 20, 2010 at 15:59

2 Answers 2

3

You have to check for posts data in the $_POST superglobal. Register_globals has turned off.

if (isset($_POST['formSubmit'])) {
//etc.
Sign up to request clarification or add additional context in comments.

2 Comments

If you have updated your PHP, then this is most likely what is wrong. In old versions of PHP you could use $formSubmit to access variables in the post, but due to security reasons this was changed. You can still enable it in the php.ini file, but it is depreciated, and in future versions that won't be possible.
That was exactly the problem. Register_globals has turned off. Changed the code to the superglobal and works fine. Thanks folks! Really appreciate it! This was driving me crazy!
0

Make 100% dure there isn't a blank line at the top before the

1 Comment

I am 100% sure there isn't a blank line at the top. I've read through numerous articles about the possibility of headers being sent, but that's not the case here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.