"使用$ _GET而不是新文件"的机制是什么?

Mil*_*d R 0 php methods get

我看到了一些网站(例如):如果你想查看你的消息框,那么它是:example.com/file.php?action=pm如果你想再次发送消息,地址和文件是相同的,但是$_GET不同的:example.com/file.php?action=sendpm

它是如何工作的?

它只是:

if ($_GET['action'] == "pm")
{
    //lots of html code(divs, forms, etc) paste here for action=pm.
}
else
{
    //lots of html code paste here for action=send
}
Run Code Online (Sandbox Code Playgroud)

而不是有文件:pm.php,send.php ......?或者机制不同?

我看到SMF使用这种机制,我也看到了类似于facebook.com/?ref=something的内容.

它是如何工作的?

Thi*_*ter 6

最简单的方法是:

$action = isset($_GET['action']) ? $_GET['action'] : 'default';
if(!in_array($action, array('list', 'of', 'allowed', 'pages')))
    $action = 'default';
include($action . '.php');
Run Code Online (Sandbox Code Playgroud)

请注意,验证步骤非常重要.未正确验证该值会导致用户能够读取脚本可以访问能够执行任意代码的用户的任何文件的令人讨厌的安全漏洞.

将所有代码放在您问题中建议的单个文件中是一个坏主意,因为它会导致代码无法读取.特别是在同一个文件中混合使用PHP和HTML时,如建议的问题(提示:使用模板引擎!).