gpt4 book ai didi

php - 在这个特定场景中,我如何使用 POST 而不是 GET

转载 作者:行者123 更新时间:2023-12-04 05:07:40 27 4
gpt4 key购买 nike

我有一个名为 insert_comment.php 的表单,它包含这个函数:

function died($error) { // if something is incorect, send to given url with error msg
header("Location: http://mydomain.com/post/error.php?error=" . urlencode($error));
die();
}

在代码的更下方,$error_message 被发送到函数 die,然后函数 die 将用户重定向到 mydomain.com/post/error.php,在那里我从 URL 获取错误消息:
$error = $_GET["error"];

echo 'some text '. $error .' sometext';

有没有办法使用 POST 重定向来做完全相同的事情?我不喜欢在 URL 中显示整个错误消息,它看起来非常难看。

最佳答案

虽然使用 POST 可能很复杂,但这是错误的策略,而不是 POST 请求的目的。

正确的策略是放置这些信息 into the session ,并从那里显示它,然后在显示时删除 session key 。

// session_start() must have been called already, before any output:
// Best to do this at the very top of your script
session_start();

function died($error) {
// Place the error into the session
$_SESSION['error'] = $error;
header("Location: http://mydomain.com/post/error.php");
die();
}

错误.php
// Read the error from the session, and then unset it
session_start();

if (isset($_SESSION['error'])) {
echo "some text {$_SESSION['error']} sometext";

// Remove the error so it doesn't display again.
unset($_SESSION['error']);
}

完全相同的策略可用于在重定向后向用户显示其他消息,例如操作成功。在 $_SESSION 中使用尽可能多的不同键数组,并在向用户显示消息时取消设置它们。

关于php - 在这个特定场景中,我如何使用 POST 而不是 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15317950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com