default case in a PHP switch-case statement doesn&

2019-08-12 15:12发布

I'm using the following PHP code, on a page that a user reach on after submitting a form with a certain foo input name in it, which this PHP code processes, and decides to which URL that user be forwarded accordingly.

I've just noticed that If a user does not enter that page with an foo input name (for example, let's say the form on the previous page had a `vvv' as the input name instead, due to some error), then this PHP code would not send the user to the default URL. instead, it would refresh itself every 3 seconds in a loop.

Why? shouldn't the default value be obtained in case of any error, including the above scenario?

CODE:

<?php

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

    switch ($_POST['foo']) {

    case "aaa":
        $url = "http://www.aaa.com/";
        break;

    default:
        $url = "http://www.bbb.com/";
    }
}

header( "refresh:3;url=$url" );


?>  
<!doctype html>
<html>
<head>
<style>
.test {display: block;}
</style>
</head>
<body>
test
</body>
</html>

4条回答
\"骚年 ilove
2楼-- · 2019-08-12 15:35

Please verify that the switch snipet is reached when this page is navigated to without foo parameters.

In your case, the $url has not been initiated yet (may be the if condition was failed). So that the header function will actually send the browser this header: refresh:3;url= (empty string after url=), which cause the browser refresh with current url every 3 seconds.

查看更多
神经病院院长
3楼-- · 2019-08-12 15:45

1) I think you are messing with $_POST and $_GET. Get params are visible in URL, POST is not shown in url.

2) If there is no $_POST['foo'], than it will throw error, because there is no set $url variable. Better change to this code:

if(isset($_GET['foo'])){

    switch ($_GET['foo']) {
        case "aaa":
            $url = "http://www.aaa.com/?foo=bbb";
            header( "refresh:3;url={$url}" );
            break;
        default:
            $url = "http://www.bbb.com/?foo=aaa";
            header( "refresh:3;url={$url}" );
    }
    die(); // Optionally, if you wish not to continue any script
} else {
    /* Show default code */
}
查看更多
够拽才男人
4楼-- · 2019-08-12 15:47
<?php
$url = "http://www.bbb.com/"; 
if(isset($_POST['foo'])){

    switch ($_POST['foo']) {

    case "aaa":
        $url = "http://www.aaa.com/";
        break;

    }
}

header( "refresh:3;url=$url" );


?>  
<!doctype html>
<html>
<head>
<style>
.test {display: block;}
</style>
</head>
<body>
test
</body>
</html>

It would be better if you set the default url at the top. So even if none of the case get match. Default value will always be there. If the value in case gets matched, then it will be replaced.

查看更多
狗以群分
5楼-- · 2019-08-12 15:53

Does $_POST['foo'] has any value at all? Try dump the value to check if there is anything in it:

var_dump($_POST['foo'])

If the result of this is null then there is an error in your form.

Something like this should work:

<form method='post' action = $this->url()>
  <div>
    <input type='text' name='foo' value='foo'>
  </div>
  <div>
    <input type='submit' value='submit' name='submit'>
  </div>
</form>

And the action:

if(isset($_POST['foo']) && !empty($_POST['foo'])){

    switch ($_POST['foo']) {

    case "aaa":
        $url = "http://www.aaa.com/";
        break;

    default:
        $url = "http://www.bbb.com/";
    }
}
查看更多
登录 后发表回答