Solved

PHP header redirect method does not work

Posted on 2014-01-29
18
598 Views
Last Modified: 2014-02-02
Here are two basic php files. page1.php should redirect to page2.php when the submit button is pressed but it does not. Why? Thanks.

<?php
      if($_SERVER["REQUEST_METHOD"] == $_POST) {
            header("Location: page2.php");      
            exit;
}
?>

<html>

      <form action="page1.php" action="post">
            <input type="submit" value="go to page two">
      </form>

</html>
0
Comment
Question by:centem
  • 5
  • 5
  • 3
  • +2
18 Comments
 
LVL 14

Expert Comment

by:Dzynit
ID: 39818919
Try changing your if beginning to:
$_SERVER['REQUEST_METHOD'] == 'POST'
0
 

Author Comment

by:centem
ID: 39818929
That didn't work but thanks for the response.
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 39818957
You could try putting in the full url in your location call too. I've had that happen to me before where it worked with just the page name on one host but needed full url on another.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:centem
ID: 39818967
Tried that too. Both files are in the same directory on localhost.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39818977
<form action="page1.php" method="post">
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 39818984
This works:
<?php
      if($_REQUEST['test'] == 'goforward') {
            header("Location: page2.php");      
            exit;
}
?>

<html>

      <form action="page1.php" action="post">
        <input type="hidden" name="test" value="goforward" />
            <input type="submit" value="go to page two">
      </form>

</html>

You can see it work here: http://dzynit.net/test/test.php but it goes to a 404 page because I didn't add a page2.php to go to.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39819020
Actually setting a method on your form shown here is in tandem with the suggestion here
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39819053
Just curious... Why do you want to do this?  It's a very unusual design pattern.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39819096
With corrections that others have mentioned AND making it a complete and valid page, this works.  I created 'page1.php' that goes to 'page2.php' which goes back to 'page1.php'.  I commented out some test code that I used to make a different method to verify that it worked.

page1.php
<?php
error_reporting(E_ALL);
if($_SERVER["REQUEST_METHOD"] == 'POST') {
	header("Location: page2.php");      
  exit;
	}
/* 
if(isset($_POST['submit'])) {
	header("Location: page2.php");      
  exit;
	} */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Header Redirect Test</title>
</head>
<body>
<h1>Header Redirect Test - Page 1</h1>
<form action="page1.php" method="post">
   <input type="submit" name="submit" value="go to page two">
</form>
<?php echo $_SERVER["REQUEST_METHOD"]; ?>
</body>
</html>

Open in new window

page2.php
<?php
error_reporting(E_ALL);
if($_SERVER["REQUEST_METHOD"] == 'POST') {
	header("Location: page1.php");      
  exit;
	}
/*
if(isset($_POST['submit'])) {
	header("Location: page1.php");      
  exit;
	} */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Header Redirect Test</title>
</head>
<body>
<h1>Header Redirect Test - Page 2</h1>
<form action="page2.php" method="post">
   <input type="submit" name="submit" value="go to page one">
</form>
<?php echo $_SERVER["REQUEST_METHOD"]; ?>
</body>
</html>

Open in new window

0
 

Author Comment

by:centem
ID: 39819112
Looks like I got it. I had to change one of the action statements to method.

And for some reason this
if($_SERVER['REQUEST_METHOD'] == 'POST')

won't work but this does.
if($_SERVER["REQUEST_METHOD"] == "post")

Why?
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 39819135
LOL - and none of us caught that! Nice catch on your own ;) And the lower case is probably because you have lower case in your action variable.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39819146
Are my comments invisible???
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39819149
You can use var_dump($_SERVER) to find out what is in the SERVER array.  Var_dump() is one of the programmer's best friends!

If you're new to PHP and want to find some good learning resources, this article may be helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 58

Expert Comment

by:Gary
ID: 39819167
This should work
if($_SERVER['REQUEST_METHOD'] == 'POST')

This should not work
if($_SERVER["REQUEST_METHOD"] == "post")

This is according to PHP's own specs unless you have something funny going on with your PHP install
http://php.net/manual/en/reserved.variables.server.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39819183
REQUEST_METHOD in $_SERVER is in capital letters on my installation of PHP.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39819195
Same here, capital letters on both IIS and Apache.  The code I posted should show that also.
0
 
LVL 14

Assisted Solution

by:Dzynit
Dzynit earned 250 total points
ID: 39819265
Cathal, I did see your comment - but not until I posted last :)
And when I said "action" in that last comment - I should have said "method". I was thinking the word action as I was typing. Servers act differently for me - like you said the lower case shouldn't work, but some domains I have will treat POST and post differently. I never really run into problems as long as I use lower or upper in both the form and in a check for it.

centem, you should close this since you have it working and I think Cathal should get the points for finding the actual error.
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39819287
No, you deserve half the points as you pointed out half the problem.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question