如果声明为用户授权

时间:2016-03-23 22:43:29

标签: javascript php if-statement firebase

我已经为我的代码创建了一个if语句用于登录,并且我试图让按钮使用if语句,这样如果用户详细信息被授权,那么他们就可以访问该页面。

我遇到的问题是,我想获取提交按钮以验证用户是否已获得授权并将其重定向到另一个页面,但是如果登录详细信息不正确,则无法登录

这是我的代码:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ((isset($_POST['email']))  && (isset($_POST['password'])) ) {
        if (isAuthenticate($_POST['email'], $_POST['password']))
         header("Location: roster3101.php");
         exit();
   }
}

header("Location: index.php");

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta content="" name="description">
    <meta content="" name="author">
    <link href="../../favicon.ico" rel="icon">
    <script src="https://cdn.firebase.com/js/client/2.4.1/firebase.js">
    </script>
    <title>Shifts</title><!-- Bootstrap core CSS -->
    <link href="bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="softwareproject.css" rel="stylesheet">
    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <script src="ie-emulation-modes-warning.js">
    </script>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body background="dark.jpg">

    <br>
    <br>
    <br>
    <br>
    <br>
    <center>
        <table border="0" cellpadding="20px">
            <tr>
                <td>
                    <center>
                        <img height="375" src="logo10.png" width="375">
                    </center>
                </td>
                <td>
                    <form id="frmLogin" role="form">
         <h2><font color="white">The Online Roster</font></h2>

        <div class="form-group">
            <label for="txtEmail"><font color="white">Email address</font></label>
            <input type="email" class="form-control" id="txtEmail" placeholder="Enter email" name="email" />
        </div>
        <div class="form-group">
            <label for="txtPass"><font color="white">Password</font></label>
            <input type="password" class="form-control" id="txtPass" placeholder="Password" name="password" />
        </div><center>
        <button type="submit" class="rosterclicks">Login</button></center>


    </form>


                    <!-- <form class="form-signin">
                        <center>
                            <h2 class="form-signin-heading">The OnlineRoster</h2>
                        </center>
                        <label class="sr-only" for="inputEmail"></label>
                        <input autofocus="" class="form-control" id="inputEmail" placeholder="Email address" required="" type="email">
                        <label class="sr-only" for="inputPassword"></label>
                        <input class="form-control" id="inputPassword" placeholder="Password" required="" type="password">
                        <div class="checkbox">
                            <label><input type="checkbox" value="remember-me">Remember me</label>
                        </div>
                    </form> -->
                    <center>
                        <table border="0">
                            <tr>
                                <td>
                                    <!-- <form action="roster3101.php">
                                        <input class="rosterclicks" type=
                                        "submit" value="Login">
                                    </form> -->
                                </td>
                            </tr>
                        </table>
                    </center>
                </td>
            </tr>
        </table>
    </center>
<script>

// Create a callback which logs the current auth state
function authDataCallback(authData) {
  if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
  } else {
    console.log("User is logged out");
  }
}
// Register the callback to be fired every time auth state changes
var ref = new Firebase("https://shiftsapp.firebaseio.com");
ref.onAuth(authDataCallback);

function authHandler(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
  }
}

ref.authWithPassword({
  email    : 'test@shiftsapp.com',
  password : 'password'
}, authHandler);

function isAuthenticate(email, passwd) {
var ref = new Firebase("https://shiftsapp.firebaseio.com");
ref.authWithPassword({
  "email": email,
  "password": passwd
}, function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
    return false;
  } else {
    console.log("Authenticated successfully with payload:", authData);
    return true;
  }
});
    }

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

isAuthenticate是一个javascript函数。你不能将它用作php函数。你需要编写一个处理auth东西的php函数。