简体   繁体   English

PHP 开关盒网址

[英]PHP Switch Case Url's

We currently use Switch case url config to help us with the navigation on some of our urls, Im not sure if there is an easier way to do it but i couldnt seem to find 1.我们目前使用 Switch case url 配置来帮助我们在一些 url 上导航,我不确定是否有更简单的方法,但我似乎找不到 1。

<?php if (! isset($_GET['step']))
    {
        include('./step1.php');

    } else {    
        $page = $_GET['step'];  
        switch($page)
        {
            case '1':
                include('./step1.php');
                break;  
            case '2':
                include('./step2.php');
                break; 
        }
    }
    ?>

Now this system works perfectly but the only snag we hit is if they type in xxxxxx.php?step=3 boom they just get a blank page and that should be correct as there is no case for it to handle '3' but what i was wondering is.. is there any php code i could add to the bottom that may tell it for any case other than those 2 to redirect it back to xxxxx.php?现在这个系统运行良好,但我们遇到的唯一障碍是如果他们输入 xxxxxx.php?step=3 繁荣他们只是得到一个空白页,这应该是正确的,因为它没有处理“3”的情况,但我想知道的是.. 是否有任何 php 代码我可以添加到底部,可以告诉它除了那些 2 之外的任何情况将其重定向回 xxxxx.php?

Thanks谢谢

Daniel丹尼尔

Use the default case.使用default大小写。 That is, change your switch to something like this:也就是说,将您的开关更改为以下内容:

<?php if (! isset($_GET['step']))
    {
        include('./step1.php');

    } else {    
        $page = $_GET['step'];  
        switch($page)
        {
            case '1':
                include('./step1.php');
                break;  
            case '2':
                include('./step2.php');
                break; 
            default:
                // Default action
            break;
        }
    }
?>

The default case will be executed for every case which is not explicitly specified.对于未明确指定的每个案例,都将执行默认案例。

All switch statements allow a default case that will fire if no other case does.所有switch语句都允许在没有其他情况时触发default情况。 Something like...就像是...

switch ($foo)
{
  case 1:
    break;
  ...
  default:
    header("Location: someOtherUrl");
}

would work.会工作。 You may, however, want to Google around for other, more robust and extensible, page dispatch solutions.但是,您可能想在 Google 上搜索其他更强大和可扩展的页面调度解决方案。

How about a different approach with something along the lines of:采用以下方式的不同方法怎么样:

<?php
$currentStep = $_GET['step'];
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number

if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page
    $includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2
}

include($includePage);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM