Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP header redirect method does not work

Posted on 2014-01-29
18
Medium Priority
?
641 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
Technology Partners: 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 111

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 84

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 111

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 111

Expert Comment

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

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 1000 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 1000 total points
ID: 39819287
No, you deserve half the points as you pointed out half the problem.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

580 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