1

I'm trying to prevent direct access to a the following file (and only allow access to those who submitted a form), Can someone tell what is wrong with this code?

<?php
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('HTTP/1.0 405 Method Not Allowed');

    } else {

if(isset($_POST['a'])){

    switch ($_POST['a']) {

    case "1":
        $var = "hey";
        break;

    case "2":
        $var = "now";
        break;

    default:
        $var = "other";
    }
}

?>
rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • 3
    You are not closing the top if else statement – superphonic Apr 02 '14 at 10:38
  • 1
    you have an error in the code.. close the else with `}` at the end of your code and I just checked on the firebug and I can see the response status as 405 Method Not Allowed – Abhik Chakraborty Apr 02 '14 at 10:38
  • do you mean add an additional single `}` at the end of the code? Dreamweaver alerts on a syntax error If I do that. – rockyraw Apr 02 '14 at 10:42
  • `} else {` is not closed in the supplied code as u gave. – Abhik Chakraborty Apr 02 '14 at 10:44
  • 1
    If Dreamweaver is giving a syntax error then there is more code you are not showing us that is causing it. In the above code as is, the first if else statement needs closing at the end. – superphonic Apr 02 '14 at 10:47
  • Your'e right, I've had a `}` at the very bottom of the page, thanks. – rockyraw Apr 02 '14 at 10:51
  • please see [this thread](http://stackoverflow.com/questions/22809362/php-header-isnt-working-page-is-displayed-as-usual) as even with the fix, there's now another problem – rockyraw Apr 02 '14 at 11:02

1 Answers1

0
 <?php
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
   //header('Location: http://www.google.com/');
    header("HTTP/1.0 404 Not Found");


    } else {

          if(isset($_POST['a'])){

          switch ($_POST['a']) {

                   case "1":
                            $var = "hey";
                            break;

                   case "2":
                            var = "now";
                            break;

                   default:
                            $var = "other";
                  }
          }
}

this will not print anything but will set the response header code. If you comment the current heder line and uncomment line with google url, it will take you to google web site. If there is no more code belove this code, than you can safely ignore php closing tag '?>'.

pinkal vansia
  • 10,240
  • 5
  • 49
  • 62