PHP include() 问题

发布于 2024-12-12 07:51:26 字数 778 浏览 0 评论 0 原文

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

$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。我不知道该怎么办。有什么建议吗?提前致谢。

I am trying to do some simple form validation making sure that whatever is entered does not exceed a certain character limit, but can't seem to get it to work. When I attempt to run this code:

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

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

I get this error:

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

From what I've been reading, it is actually looking for the file edit.php?id=97 as opposed to looking for edit.php and appending the id. I have no idea what to do. Any suggestions? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

习ぎ惯性依靠 2024-12-19 07:51:26

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

如果您的 php.ini 启用了 HTTP,您可以通过 HTTP 包含它(以有效协议开始字符串)(allow_url_include,默认禁用),但我不推荐它。

An include file is just a file, it is not a HTTP request, therefore a GET param does make not sense in that context. You could access $id and $str in that include if you need to.

You can include via HTTP (start the string with a valid protocol) if your php.ini has it enabled (allow_url_include, disabled by default), but I wouldn't recommend it.

泪意 2024-12-19 07:51:26

您不能包含带有 HTTP 查询参数的本地文件。

据猜测,我想说您想要进行标头重定向

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

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

  1. GET 表单页面。表单动作是同一页面
  2. POST表单数据。检测 POST 方法并验证结果
  3. 如果有效,则重定向到成功页面。如果没有,则重新显示带有通知/标志的表单

You can't include a local file with HTTP query parameters.

At a guess, I'd say you want to do a header redirect

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

That aside, I find it much easier to follow this workflow when dealing with forms:

  1. GET form page. Form action is the same page
  2. POST form data. Detect POST method and validate results
  3. If valid, redirect to success page. If not, re-display form with notifications / flags
怀里藏娇 2024-12-19 07:51:26

您无法按照您尝试的方式包含文件。尝试删除 ?id=x 部分,它应该仍然有效。

You cant include a file the way you're trying. Try removing the ?id=x part and it should still work.

冷情妓 2024-12-19 07:51:26

在您的包含文件中:

include('edit.php');

edit.php 内部:

$id = $_POST['id'];

In your including file:

include('edit.php');

Inside of edit.php:

$id = $_POST['id'];
两人的回忆 2024-12-19 07:51:26

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

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();
}

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

If you really wanna do it that way...

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();
}

but try it like this...

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

or tell us what really you want to do...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文