Solved

PHP header redirect method does not work

Posted on 2014-01-29
18
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

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 110

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

738 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