且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS和PHP登录

更新时间:2023-12-03 21:01:46

是的,它是好的,你可以做到左右逢源,做起来角的方式肯定是去的***途径。

I am trying to create a simple application using angularjs, php and mysql..

I do have a login page which uses angularjs, PDO and mysql.

however I'm not sure if I'm doing it right or the other way around..

is it okay to still use $_SESSION to verify user login. or should I use angularjs authentication? I'm still new to angularjs so I don't know if there's a similar code to $_SESSION in angularjs.

I just need your advice guys.

thanks in advance.

here's my code.

HTML Login

include "lib/connection.php";
if(isset($_SESSION['logged_in'])){
    if($_SESSION['logged_in'] == '1'){  
        header('location:index.php');
    }
}
?>
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Implementing AngularJS</title>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="css/custom.css">
</head>

<body>
<div class="container" ng-controller="AppCtrl">
    <div class="login col-md-4 col-md-offset-4">
        <h2>Please Log in</h2>
        <form>
        <input type="text" name="username" class="form-control" placeholder="Username" ng-model="users.username"  />
        <input type="password" name="password" class="form-control" placeholder="Password" ng-model="users.password"  />
        <input type="submit" name="cmd_btn" class="btn btn-primary btn-block" ng-click="login()" />
        </form>
        <br />
        <div class="alert alert-danger" ng-if="message">
            <p>Invalid Username or Password!</p>
        </div>
    </div>

</div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
    <script src="controller.js"></script>
</body> 

</html>

ANGULARJS CODE

var myApp = angular.module('myApp', []);

myApp.controller('AppCtrl', ['$window','$scope', '$http', function($window, $scope, $http) {


$scope.login = function(){
    console.log($scope.users);
    var inputs = 'myData=' + JSON.stringify($scope.users);
    $http({
        method:'POST',
        url:'lib/login_c.php',
        data: inputs,
        headers:{'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'}
    }).success(function(res){
        console.log(res);
        if(res == 1){
             $window.location.href = "index.php";
        }else{
            $scope.message = true;
        }
    });
};

}]);

PHP CODE

<?php

include "connection.php";

if(isset($_POST['myData'])){

$login_obj = json_decode($_POST['myData']);
if(!empty($login_obj->username) AND !empty($login_obj->password)){

    $instantiate_login_c = new login_c;

    $condition = "username = :username AND password = :password";
    $arr = array('username' => $login_obj->username, 'password' => $login_obj->password);

    $result = $instantiate_login_c->getWhere('users', $condition, $arr);

    if($result){
        $_SESSION['logged_in'] = '1';
        echo 1;
    }else{ 
        echo 0;}


}else{
    echo 0;
}

}

yes it is okay, you can do it both ways, but doing it the angular way is certainly the best route to go.