PHP+Google AppScript登录报错「密码必填」问题求助
PHP登录应用「密码必填」错误排查
提交邮箱和密码后,系统始终返回JSON格式错误:{"status":"error","message":"Senha requerida"}(意为「密码必填」),以下是相关代码及问题解决步骤:
1. login.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Page de Login (teste)</title> </head> <body> <div class="formContainer"> <form method="post" action="login_action.php"> <h2 class="title">Login</h2> <div class="tip"> log in utilizing email and password </div> <label for="email">Email</label> <input type="text" name="email"> <label for="senha">Password</label> <input type="password" name="senha"> <input type="submit" value="LOGIN"> </form> <div class="link"> still don't have an account?<br> <a href="signup.php">Create account</a> </div> </div> </body> </html>
2. login_action.php(原问题代码)
<?php session_start(); $url = "https://script.google.com/macros/s/AKfycbwfzUBRZt0h3OOSF5VSUsciOQLInZ7lOE5WPFOU4UyFtNTzKn958uOq1vjB_XyIP_eAIg/exec"; $postData = [ "action" => "login", "email" => $_POST['email'], "password" => $_POST['senha'] // 问题所在 ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $postData ]); $result = curl_exec($ch); echo $result;
3. index.php
<?php session_start(); if (isset($_SESSION['user'])) { header("location: dashboard.php"); } else { header("location: login.php"); } ?>
4. dashboard.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Dashboard</title> </head> <body> <div class="dashboardContainer"> <div class="avatar"> <img src="avatar.png" alt="avatar do usuário"> </div> <div class="welcomeMessage"> hello <b>MEUNOME</b>, welcome to my website </div> <div class="notes"> You can modify this page by editing 'dashboard.php' </div> </div> </body> </html>
5. ProjetoLogin.gs
//get data function getDataByEmail(email){ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("loginTeste"); var data = sheet.getDataRange().getValues(); var result = null; for(var i=1; i<data.length; i++){ if(data[i][0] == email){ result = data[i]; break; } } return result; } //login function function login(email, senha){ var data = getDataByEmail(email); var error = null; if(data == null){ error = "email não registrado"; }else if(data[1] != senha){ error = "Senha inválida"; } if(error == null){ return { status: "successo", data: { email: data[0], senha: data[1], nome: data[2] } } }else{ return { status: "error", message: error } } } //signup function function signup(email, senha, nome){ var check = getDataByEmail(email); if(check != null){ return { status: "error", message: "email já existe" } }else{ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("loginTeste"); var data = [[email,senha,nome]]; var row = sheet.getLastRow() + 1; var cel = 1; var rowLength = data.length; var celLength = data[0].length; sheet.getRange(row, cel, rowLength, celLength).setValues(data); return { status: "successo", message: "Inscrição realizada!" } } } // handle POST request function doPost(e){ var error = null; if(typeof e.parameter.action == 'undefined'){ error = "parâmetro action requerido"; }else if(e.parameter.action == "login"){ //LOGIN PROCCESS if(typeof e.parameter.email == 'undefined'){ error = "email requerido"; }else if(typeof e.parameter.senha == 'undefined'){ // 检查senha参数 error = "senha requerida"; }else if(e.parameter.email.trim() == ""){ error = "email não pode estar vazio"; }else if(e.parameter.senha.trim() == ""){ error = "senha não pode estar vazia"; }else{ var result = login(e.parameter.email.trim(), e.parameter.senha.trim()); } }else if(e.parameter.action == "signup"){ //SIGNUP PROCCESS if(typeof e.parameter.email == 'undefined'){ error = "email requerido"; }else if(typeof e.parameter.senha == 'undefined'){ error = "senha requerida"; }else if(typeof e.parameter.nome == 'undefined'){ error = "nome requerido"; }else if(e.parameter.email.trim() == ""){ error = "email não pode estar vazio"; }else if(e.parameter.senha.trim() == ""){ error = "senha não pode estar vazia"; }else if(e.parameter.nome.trim() == ""){ error = "nome não pode estar vazio"; }else{ var result = signup(e.parameter.email.trim(), e.parameter.senha.trim(), e.parameter.nome.trim()); } }else{ error = "unknown action"; } if(error != null){ var result = { status: 'error', message: error } } return ContentService.createTextOutput(JSON.stringify(result)); } //handle GET request function doGet(e){ return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Method GET não permitido" })) } function test(){ var data = signup("user5@example.com", "shinmas", "shinmatsu"); Logger.log(JSON.stringify(data)); }
问题原因
login_action.php的$postData数组中,密码字段键名被设为password,但Google Apps Script的doPost方法里,登录逻辑检查的是senha参数(对应表单的name="senha")。参数名不匹配导致GAS无法识别密码参数,因此返回「密码必填」错误。
解决方案
修改login_action.php中的$postData数组,将"password"改为"senha",确保和表单字段名、GAS参数名完全一致:
$postData = [ "action" => "login", "email" => $_POST['email'], "senha" => $_POST['senha'] // 修改此处键名 ];
修改后重新提交登录请求,即可正常传递密码参数,完成登录验证流程。
内容的提问来源于stack exchange,提问作者Darcount
相关产品推荐
相关产品推荐

