Solved

PHP header redirect method does not work

Posted on 2014-01-29
18
593 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
Comment Utility
Try changing your if beginning to:
$_SERVER['REQUEST_METHOD'] == 'POST'
0
 

Author Comment

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

Expert Comment

by:Dzynit
Comment Utility
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
Comment Utility
Tried that too. Both files are in the same directory on localhost.
0
 
LVL 58

Expert Comment

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

Expert Comment

by:Dzynit
Comment Utility
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
Comment Utility
Actually setting a method on your form shown here is in tandem with the suggestion here
0
 
LVL 108

Expert Comment

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

Expert Comment

by:Dave Baldwin
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:centem
Comment Utility
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
Comment Utility
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
Comment Utility
Are my comments invisible???
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
REQUEST_METHOD in $_SERVER is in capital letters on my installation of PHP.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
No, you deserve half the points as you pointed out half the problem.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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 …

743 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

16 Experts available now in Live!

Get 1:1 Help Now