如果使用 php api 验证失败,则显示错误

  if($api_response["success"] === true) {


        header("Location: https://google.com");

        exit;


    } else {

        // print_r($api_response);

        $msg = "Some Error Occured!";

        header("Location: ". $_SERVER['HTTP_REFERER']);

        // echo $msg;

    }

在上面的代码中,如果为 true,则代码将完美运行并按预期重定向。但如果情况不正确,我想显示错误index.php


<?php

  if(isset($msg) && $msg != ''){

    echo $msg;

}

?>

当条件为假时,我没有得到 $msg 变量的值。


偶然的你
浏览 109回答 1
1回答

慕哥6287543

发送$msg标头if($api_response["success"] === true) {&nbsp; &nbsp; header("Location: https://google.com");&nbsp; &nbsp; exit;} else {&nbsp; &nbsp; $msg = "Some Error Occured!";&nbsp; &nbsp; header("Location: ".$_SERVER['HTTP_REFERER']."?msg=".$msg);}然后$msg像这样读你的if(isset($_GET['msg'])){&nbsp; &nbsp; print_r($_GET['msg']);&nbsp;}在你的情况下,你需要使用会话来保持私密,如下所示if($api_response["success"] === true) {&nbsp; &nbsp; header("Location: https://google.com");&nbsp; &nbsp; exit;} else {&nbsp; &nbsp; $msg = "Some Error Occured!";&nbsp; &nbsp; session_start();&nbsp; &nbsp; $_SESSION['msg'] = $msg;&nbsp; &nbsp; header("Location: ". $_SERVER['HTTP_REFERER']);&nbsp; &nbsp; exit();}//and read like this<?phpsession_start();if(isset($_SESSION['msg'])){&nbsp;echo $_SESSION['msg'];&nbsp;unset($_SESSION['msg']); // remove it now we have used it}?>
打开App,查看更多内容
随时随地看视频慕课网APP