I am trying to get my login for my application to work however it keeps refreshing to index page. Whilst doing some debugging it seems the problem is with one line in my controller -
//if email and password matches one in the database
[php]$view->LoginDataSet = $LoginDataSet->fetchLoginDetails($_POST[‘Email’], $_POST[‘Password’]);[/php]
I am using MVC and PDO too for my application.
Model
LoginData.php
[php]<?php
class LoginData {
protected $Name, $Email, $Password;
public function __construct($dbrow) {
$this->Name = $dbrow['Name'];
$this->Email = $dbrow['Email'];
$this->Password = $dbrow['Password'];
}
function getName() {
return $this->Name;
}
function getEmail() {
return $this->Email;
}
function getPassword() {
return $this->Password;
}
function logout() {
$_SESSION = array();
session_destroy();
}
}
[/php]
LoginDataSet.php
[php]<?php
require_once(‘Model/Database.php’);
require_once(‘Model/LoginData.php’);
class LoginDataSet {
protected $_dbHandle, $_dbInstance = null;
public function __construct() {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getdbConnection();
}
public function fetchLoginDetails($Email, $Password) {
$Password = crypt($Password, $Email);
$sqlQuery = "SELECT * FROM users WHERE Email=:u AND Password=:p"; //basic SQL Query
//var_dump($sqlQuery); die();
$statement = $this->_dbHandle->prepare($sqlQuery); //Prepare PDO statement
//SQL Injection
$statement->execute(array(
':u' => $Email,
':p' => $Password
)); //Executes PDO statement
$dataSet = [];
while ($row = $statement->fetch()) { //Fetches the next row matching the query
$dataSet[] = new LoginData($row);
}
return $dataSet;
}
public function fetchProfileDetails($Name) {
$sqlQuery = "SELECT * user WHERE Name='" . $Name . "'";
$statement = $this->_dbHandle->prepare($sqlQuery); //Prepare PDO statement
$statement->execute(); //Executes PDO statement
$dataSet = [];
while ($row = $statement->fetch()) { //Fetches the next row matching the query
$dataSet[] = new LoginData($row);
}
return $dataSet;
}
}
?>[/php]
View
[php]<!DOCTYPE>
<?php if (!isset($_SESSION)) { session_start(); } ?> My Comics[/php]
Controller
login.php
[php]<?php
//session start will always be an email
session_start();
$view = new stdClass();
$view->pageTitle = ‘LoggedIn’;
require_once (‘Model/LoginDataSet.php’);
//if submit is pressed
if (isset($_POST[‘submit’])) {
//check the email and password against the one in the database.
$LoginDataSet = new LoginDataSet();
//if email and password matches one in the database
$view->LoginDataSet = $LoginDataSet->fetchLoginDetails($_POST[‘Email’], $_POST[‘Password’]);
//get the variables below using the functions of logindataset
if (count($view->LoginDataSet) == 1) {
$_SESSION[‘Email’] = $_POST[‘Email’];;
//continue on to profile page
header(“Location:home.php”);
} else {
//if incorrect return to index page with error
$_SESSION[‘error’] = “logindetails”;
header(“Location:index.php”);
}
}
require_once(‘View/home.phtml’);
[/php]
How do I rectify the problem as I have been going through books and other forums however nothing is helping. I am new to PHP and more towards ASP.net . Any help would be appreciated as I have come to a dead end.