无法解决“期望POST重定向为GET,但检测到POST”错误的技术求助
问题:POST请求未正确重定向为GET,触发自动 grader 错误
我用ngrok和XAMPP运行WA4E作业代码,实际功能和授权都正常,但自动 grader 报错:Checking to see if the POST redirected to a GET. Expecting POST to Redirected to a GET - found POST,错误提示为“Expecting POST to Redirected to a GET - found POST”。我尝试给header()添加true, 303参数,但没有效果。
相关代码
HTML表单部分
<form method="POST"> <label for="nam">User Name</label> <input type="text" name="who" id="nam"><br/> <label for="id_1723">Password</label> <input type="text" name="pass" id="id_1723"><br/> <input type="submit" value="Log In"> <input type="submit" name="cancel" value="Cancel"> </form>
PHP处理逻辑
// Check to see if we have some POST data, if we do process it if ( isset($_POST['who']) && isset($_POST['pass']) ) { if ( strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1 ) { $failure = "User name and password are required"; } else { $check = hash('md5', $salt.$_POST['pass']); if ( $check == $stored_hash ) { // Redirect the browser to game.php header("Location: https://c561-212-58-119-178.ngrok-free.app/rps/game.php?name=".urlencode($_POST['who']), true, 303); return; } else { $failure = "Incorrect password"; } } }
排查与解决办法
确认303状态码是否生效:用浏览器Network面板查看登录请求的响应状态码,若不是303,尝试拆分设置头信息:
http_response_code(303); header("Location: https://c561-212-58-119-178.ngrok-free.app/rps/game.php?name=".urlencode($_POST['who'])); exit;确保
header()之后无任何输出(包括空格、换行),必要时用ob_start()开启输出缓冲。检查意外输出干扰:排查PHP文件开头是否有BOM、空格或提前的echo/print输出,这些会导致头信息无法正确发送。
验证重定向后的请求方法:在
game.php中添加echo "Request method: " . $_SERVER['REQUEST_METHOD'];,确认是否为GET。若还是POST,需检查服务器是否支持HTTP/1.1协议。处理Cancel按钮的POST请求:grader可能检测到Cancel按钮触发的POST未重定向,可给Cancel按钮添加
formmethod="get",或在PHP中单独处理Cancel逻辑并执行303重定向:<input type="submit" name="cancel" value="Cancel" formmethod="get">
内容的提问来源于stack exchange,提问作者Kseniia
相关产品推荐
相关产品推荐

