简体   繁体   English

PHP开关盒网址

[英]PHP switch case Url

I want to make my link ex: 我想做我的链接前:

  • www.bestproducts.com/products.php to www.bestproducts.com/products.php至
  • www.bestproducts.com/?products or www.bestproducts.com/?products或
  • www.bestproducts.com/?page=products. www.bestproducts.com/?page=products。 So how can I do it? 那我该怎么办呢?

I think that you want to have pretty urls. 我认为您想要漂亮的网址。 Then you can use htaccess . 然后,您可以使用htaccess More info about pretty urls . 有关漂亮网址的更多信息 (you have to create additional urls in the htaccess for the page arguments) (您必须在htaccess为页面参数创建其他网址)

How you can do your task: 如何完成任务:

$url = 'www.bestproducts.com/products.php';

$url_array = explode('/', $url);

$result_url1 = implode('/?', $url_array);

$result_url2 = implode('/?page=', $url_array);

I can give you an example for the botom one. 我可以举一个例子为例。 I did it for my page as well, however, instead of page=products, i have just adressed a single value you to it, in my case: p 我也是在我的页面上执行此操作,但是,在我的情况下,我只是将单个值赋予了它,而不是page = products:p

the link is then index.php?p=members 链接是index.php?p = members

Bassically what happens in here that my script checks what i give the to the value p. 基本上,在这里,我的脚本检查了我赋予值p的值。 let me show you in a code 让我用代码给你看

html: 的HTML:

 <a href="index.php?p=members" title="Members">Members</a>

I request the link p=members to be dynamically loaded in my hyperlink. 我要求将链接p = members动态加载到我的超链接中。 What runs in my content area is a script that looks for that. 在我的内容区域中运行的是一个寻找该脚本的脚本。

<?php
     if (!empty($_GET['p'])){
        $pages_dir = 'inc/content';
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);

        $p = $_GET['p'];

        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');  
            }
                else
            {
                echo 'Sorry, page not found';
            }
        }
    else
        {
             include($pages_dir.'/home.inc.php');
        }
?>

To spill it out, the if statement checks request the p value out of the hyperlink (remember i said in the html p=members?) i create a variable that checks the folder inc/content which contain my content pages. 为了说明问题,if语句检查从超链接中请求p值(还记得我在html中说的p = members吗?),我创建了一个变量来检查包含我的内容页面的inc / content文件夹。 that variable contains an array with the names of the pages (the unset part is that it removes the . and .. within a folder) It creates an array. 该变量包含一个带有页面名称的数组(未设置的部分是它删除了文件夹中的。和..)。它创建了一个数组。 Then i let it run through another if else statement. 然后,我让它通过另一个if else语句运行。 This tells to look for the file name (members) with the taggs inc.php (the inc part is not needed, but i did it so it was clear that file was an include). 这告诉您要使用taggs inc.php查找文件名(成员)(不需要inc部分,但我这样做了,因此很明显该文件是包含文件)。 If it is in the array, include it, if not, it tells me page not found. 如果它在数组中,请包括它,否则,它告诉我找不到页面。 If there is no value in p (to close it) it basically opens my index page. 如果p中没有值(将其关闭),则基本上会打开我的索引页。

I hope this helps 我希望这有帮助

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

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