Javascript alert and php header

后端 未结 7 929
执念已碎
执念已碎 2020-12-18 10:33

I got a little problem. When I got my PHP script without header it\'s fine, I am getting javascript alert box. But when I use header before alert it\'s not working. It\'s re

相关标签:
7条回答
  • 2020-12-18 11:13

    I know this is a old post, but here is how I made it in a similar example. This example checks if the administrator has logged in from a specific IP address, if it returns false it should redirect the user back to the login page and show an error message.

    User has logged in page:

    if ($_SERVER['REMOTE_ADDR'] == "ip address"){
        $admin = True;
    }else{
        session_start();
        $_SESSION['errorMessage'] = "WARNING: You dont have access to this area.";
        header("Location: login.php");
    }
    

    login.php:

    session_start();
    
    if(isset($_SESSION['errorMessage'])){
        echo "<script type='text/javascript'>
                alert('" . $_SESSION['errorMessage'] . "');
              </script>";
        //to not make the error message appear again after refresh:
        session_unset($_SESSION['errorMessage']);
    }
    
    0 讨论(0)
提交回复
热议问题