The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How to validate login using Slim PHP

himeda.2000's Avatar

himeda.2000

27 May, 2015 02:05 PM

I'm using AngularJS (Front end), Slim Framework (Back end), PHP and MySQL. I am trying to validate both user email and password with the database stored values but I am able to login with incorrect email and password. I can't see where is my problem. Please help me! Here is my code

login.html


<form novalidate name="SigninForm" id="add-new-form" method="post" action=""> <div class="form-group">
  <label for="first_name">Email:</label>
  <input class="form-control" type="text" ng-model="user.email" required />
</div>


<div class="form-group"> <label for="address">Password:</label> <input class="form-control" type="text" ng-model="user.password" /> </div> <button class="btn btn-primary" ng-disabled="SigninForm .$invalid || isUnchanged(user)" id="login" ng-click="signin(user)">Login!</button></form>

app.js


angular.module('CrudApp', []).
  config(['$routeProvider', function($routeProvider) 
{
  $routeProvider.
when('/', {templateUrl: 'assets/tpl/home.html', controller: HomeCtrl}).
 when('/login-user', {templateUrl: 'assets/tpl/login.html', controller: LoginCtrl}).
otherwise({redirectTo: '/'});
}]);
function HomeCtrl($scope, $http) 
{
  $http.get('api/users').success(function(data) 
  {
    $scope.users = data;
  });
}
function LoginCtrl($scope, $http, $location) {
  $scope.master = {};
  $scope.activePath = null;
  $scope.signin = function(user, AddNewForm) {
    $http.post('api/login_user', user).success(function(){
      $scope.reset();
      $scope.activePath = $location.path('/list-user');
      //alert('No access available.');
    });
    $scope.reset = function() {
      $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
  };
}

index.php


<?php
require 'Slim/Slim.php';
$app = new Slim();
$app->post('/login_user', 'login');
$app->run();
function login()
{ $request = Slim::getInstance()->request(); $user = json_decode($request->getBody()); $email= $user->email; $password= $user->password; if(!empty($email)&&!empty($password)) { $sql="SELECT name, email FROM user WHERE email='$email' and password='$password'"; $db = getConnection(); try { $result=$db->query($sql); if (!$result) { // add this check. die('Invalid query: ' . mysql_error()); } $row["user"]= $result->fetchAll(PDO::FETCH_OBJ); $db=null; echo json_encode($row); } catch(PDOException $e) { error_log($e->getMessage(), 3, '/var/tmp/php.log'); echo '{"error":{"text":'. $e->getMessage() .'}}'; } } } function getConnection() { $dbhost="127.0.0.1"; $dbuser="root"; $dbpass=""; $dbname="TQA"; $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbh; } ?>

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac