Solved

PHP header redirect method does not work

Posted on 2014-01-29
18
596 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
 

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now