PHP include()问题

时间:2011-10-25 23:50:19

标签: php include

我正在尝试做一些简单的表单验证,确保输入的内容不超过某个字符限制,但似乎无法使其工作。当我尝试运行此代码时:

$str = "?id=";
$id = $_POST['ID'];

if (strlen($_POST['fname']) > 1) {
    $message = "Character cannot be more than...";
    include 'edit.php' . $str . $id;
    exit();
}

我收到此错误:

Warning: include(edit.php?id=97) [function.include]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/update.php on line 15

Warning: include() [function.include]: Failed opening 'edit.php?id=97' for inclusion (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/htdocs/update.php on line 15

从我一直在阅读的内容来看,它实际上是在寻找文件edit.php?id=97,而不是寻找edit.php并附加id。我不知道该怎么做。有什么建议?提前谢谢。

5 个答案:

答案 0 :(得分:4)

包含文件只是一个文件,它不是HTTP请求,因此GET参数在该上下文中没有意义。如果需要,您可以访问其中的$id$str

可以包含通过HTTP(使用有效协议启动字符串),如果php.ini已启用它(allow_url_include,默认情况下禁用),但我不会推荐它。

答案 1 :(得分:2)

您不能include具有HTTP查询参数的本地文件。

猜测一下,我想说你想做一个标题重定向

header(sprintf('Location: http://your-domain.com/edit.php?id=%d', $id));
exit;

除此之外,我发现在处理表单时更容易遵循此工作流程:

  1. 获取表单页面。表单操作是同一页面
  2. POST表格数据。检测POST方法并验证结果
  3. 如果有效,请重定向到成功页面。如果没有,请使用notification / flags重新显示表单

答案 2 :(得分:1)

您无法按照您尝试的方式添加文件。尝试删除?id=x部分,它仍然有用。

答案 3 :(得分:0)

如果你真的想这样做......

if (strlen($_POST['fname']) > 1) {
    $message = "Character cannot be more than...";
    echo file_get_contents('http://example.com/edit.php'.$str.$id.'&message='.$message);
    exit();
}

但尝试这样......

if (strlen($_POST['fname']) > 1) {
    $message = "Character cannot be more than...";
    include_once 'edit.php';
    exit();
}

或者告诉我们你真正想做什么......

答案 4 :(得分:0)

在您的包含文件中:

include('edit.php');

在edit.php里面:

$id = $_POST['id'];