duanreng3439 2012-03-11 16:00
浏览 379
已采纳

PHP中的QUERY_STRING处理

It's possible via .htaccess send query to another file? A bit explaining:

index.php handle all pages like this:

switch($page) {
    case 'index':
        require_once START . '/includes/pages/index.php';
    break;

    case 'another':
        require_once START . '/includes/pages/another.php';
    break;
       and so on...

In .htaccess:

RewriteRule ^index$ index.php?page=index [L]
RewriteRule ^another$ index.php?page=another [L]

Links(eg. localhost/84ss7d41):

RewriteRule ^(.*)$ index.php?QUERY_STRING=$1 [L]

So in /includes/pages/index.php:

if ($_GET['QUERY_STRING']) {
$_SERVER['QUERY_STRING'] = $_GET['QUERY_STRING'];
$query_string = check_input($_SERVER['QUERY_STRING']);
    //checking/validating processing further...

How to make links like this one localhost/84ss7d41 work in same way as now, but without index.php ? I'm trying to seperate my "project" if something would happen (files accidentally deleted) checking/validating data would work same, without main script, it's like:

if (main project file index.php/ || /includes/pages/index.php doesn't exist) {
  check/validate data with reserve.php
}
  • 写回答

1条回答 默认 最新

  • dongwen1935 2012-03-15 23:14
    关注
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !=/index.php
    RewriteRule ^(.*) /index.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) /reserve.php [L]
    

    This redirects all requests that do not match an existing file (for example, an image) to index.php, where you can then use $_SERVER["REQUEST_URI"] to decide how to act. This is an implementation of the Front Controller pattern http://en.wikipedia.org/wiki/Front_Controller_pattern

    Additionally, if index.php cannot be found by apache, then the request will be forwarded to reserve.php

    Although, building in this kind of redundancy is not necessarily the 'right' thing to do (what if reserve.php goes missing too?). Enforcing proper access controls over index.php is probably a better approach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?