如何:PHP動態URL檢查

[英]How to: PHP dynamic url checking


I am making an admin panel for a small project. I want to use dynamic URLs to edit specific data entries. For instance:

我正在為一個小項目制作一個管理面板。我想使用動態URL來編輯特定的數據條目。例如:

file.php?edit&n=53

I want this URL to edit entry 53.

我希望此URL編輯條目53。

I use a switch statement to check for the edit page, but how do I check whether or not the URL has the &n=x extension in the same switch statement?

我使用switch語句來檢查編輯頁面,但是如何檢查URL是否在同一個switch語句中具有&n = x擴展名?

Ex:

switch $_SERVER['QUERY_STRING']
{
    case "edit"
        //shows a list of entries to edit  
        break;
}

Would I just make another case with a reg expression? How would I make said expression?

我會用reg表達式創建另一個案例嗎?我怎么說這個表達?

I realize I could just make seperate file named edit and use only one tier of query string, but I would like to know how to do this.

我意識到我可以創建名為edit的單獨文件,並且只使用一層查詢字符串,但我想知道如何執行此操作。

Thanks in advance!

提前致謝!

3 个解决方案

#1


Like everyone else said use $_GET

像其他人一樣說使用$ _GET

I recommend modifying your urls so they look like...

我建議修改您的網址,使它們看起來像......

file.php?action=edit&n=53

now you can...

現在你可以...

$id = intval( $_GET['n'] );

switch( $_GET['action'] ) {

    case 'edit':
        // Edit entry
        break;
    case 'delete':
        // Delete entry
        break;
    case 'create':
        // Create new entry
        break;
    default:
        // Invalid action

}

PHP page on $_GET - http://us.php.net/manual/en/reserved.variables.get.php

$ _GET上的PHP頁面 - http://us.php.net/manual/en/reserved.variables.get.php

#2


Check if $_GET['edit'] and $_GET['n'] exist:

檢查$ _GET ['edit']和$ _GET ['n']是否存在:

if (isset($_GET['edit'])) {
    echo 'edit mode';
    if (isset($_GET['n'])) {
        echo 'editing '.intval($_GET['n']);
    }
}

#3


you should be using the $_GET to track variables passed in through the URL.

你應該使用$ _GET來跟蹤通過URL傳入的變量。

you can check to see if a variable exists by using isset($_GET['edit']) with isset($_GET['n']) for the page

您可以通過使用isset($ _ GET ['edit'])和isset($ _ GET ['n'])來檢查變量是否存在


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/11/72fc5bfe3dec247010fe2da42fd3aba1.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com