【问题标题】:URL querystring with a php include带有 php 的 URL 查询字符串包括
【发布时间】:2011-05-13 23:20:42
【问题描述】:

我正在尝试在页面的选项卡中包含要输出的文件。文件本身会很好,但是当我尝试向它添加所需的查询字符串时,它给了我一个“无法打开流:没有这样的文件或目录”错误。

我尝试了直接包含并尝试将查询字符串设置为变量。这就是我现在所处的位置。

$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");

有人成功做到了吗?

【问题讨论】:

    标签: php


    【解决方案1】:

    您不能在 include() 中包含查询字符串。

    假设这是一个本地脚本,您可以使用:

    $_REQUEST['mls'] = $_REQUEST['mlid'];
    $_REQUEST['lid'] = 0;
    $_REQUEST['v'] = 'agent';
    include("agentview.php");
    

    如果它是不同服务器上的远程脚本,请不要使用包含。

    【讨论】:

    • 这很好用——现在我必须弄清楚为什么它会破坏我的 JS!如果可以的话,我会接受这个答案。
    • 是否有任何特殊原因必须以这种方式手动重新请求查询变量……或者,为什么?
    【解决方案2】:

    我在第二页创建了一个变量 - 并在第一页传递了一个值 - 它对我有用:

    *Page with include: 'index.php'
     <?php $type= 'simple'; include('includes/contactform.php'); ?>
    
    
    *Page included: 'includes/contactform.php'
    
     switch($type){
      case 'simple':
       //Do something simple
      break;
    
      default:
       //Do something else
      break;
     }
    

    【讨论】:

      【解决方案3】:

      我稍微修改了 Frank Farmer 给出的已接受答案以适用于不同的查询:

      包含两次会导致问题:

      $_REQUEST['mls'] = $_REQUEST['mlid'];
      $_REQUEST['lid'] = 0;
      $_REQUEST['v'] = 'agent';
      include("agentview.php");
      
      //changing the v to another
      $_REQUEST['v'] = 'agent2';
      include("agentview.php");
      

      对于遇到这种多重包含问题的人,您可以将代码包装在“agentview.php”中的一个函数中:

      agentview.php 内部

      function abc($mls,$lid,$v){
         ...your original codes here...
      }
      

      文件需要调用agentview.php

      include_once("agentview.php");
      abc($_REQUEST['mlid'], 0, 'agent');
      abc($_REQUEST['mlid'], 0, 'agent2');
      

      希望它能帮助像我一样遇到同样问题的人,并感谢 Frank Farmer 的出色解决方案,为我节省了大量时间。

      【讨论】:

        猜你喜欢
        • 2016-03-27
        • 2016-09-25
        • 2012-03-31
        • 2011-01-21
        • 1970-01-01
        • 2017-05-17
        • 2013-11-21
        • 1970-01-01
        • 2014-05-21
        相关资源
        最近更新 更多