PHP 切换大小写 URL

发布于 2024-11-29 08:31:59 字数 650 浏览 2 评论 0原文

我们目前使用 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; 
        }
    }
    ?>

现在这个系统工作完美,但我们遇到的唯一障碍如果他们输入 xxxxxx.php?step=3 繁荣,他们只会得到一个空白页,这应该是正确的,因为它没有处理“3”的情况,但我想知道的是..有没有任何 php 代码我可以添加到底部这可能会告诉它除了这 2 种情况之外的任何情况,将其重定向回 xxxxx.php ?

谢谢

丹尼尔

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.

<?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 ?

Thanks

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2024-12-06 08:31:59

使用default 情况。也就是说,将您的开关更改为如下所示:

<?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;
        }
    }
?>

对于未明确指定的每种情况,都将执行默认情况。

Use the default case. 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.

守不住的情 2024-12-06 08:31:59

所有 switch 语句都允许一个 default 情况,如果没有其他情况发生,该情况就会触发。像...之类的东西

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

会起作用。但是,您可能希望通过 Google 搜索其他更强大且可扩展的页面调度解决方案。

All switch statements allow a default case that will fire if no other case does. 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.

温柔嚣张 2024-12-06 08:31:59

采取不同的方法怎么样:

<?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);
?>

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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文