Sorry in advanced for the longnessnes of this post.
I'm trying to create a login script for a website and since OOP seems to be all the rage these days i thought i might use it. But i seem to have got into some difficulties.
This is the idea -
Every page has the class 'UserAccount' set to a variable userAccount (creative i know). Anyway, this class then checks to see if the user is already logged in (if so it takes the data from the session and stores them as variables), if the login form has been sent via post to the page (if so if validates the data and logins in through mysql query) or whether is neither (in that case its someone viewing an ordinary page while not logged in).
So, this is the class so far. What I am having trouble with is the validation of the email address and the password (that's all they need to log in).
So can someone please teach me how to validate them, i could do it if i was not using oop, i have lots of times, but now it just seems too hard.
Many Thanks In Advance for all of your time.
So, at long last the code.
Code: Select all
<?php
class UserAccount
{
private $_isLoggedIn;
private $_isFormFilled;
private $_userToken;
private $_formErrors;
private $_userMail;
private $_userPassword;
private $_userFirstName;
private $_userLastName;
private $_userID;
public function __construct()
{
$this->_isLoggedIn = (isset($_SESSION['userActive'])) ? true : false;
$this->_formErrors = array();
($this->_isLoggedIn) ? $this->getSessionData() : $this->isFormSent();
}
private function getSessionData()
{
$this->_userFirstName = $_SESSION['userFirstName'];
$this->_userLastName = $_SESSION['userLastName'];
$this->_userID = $_SESSION['userID'];
}
private function isFormSent()
{
if(isset($_POST['login'])) $this->checkInput();
}
private function checkInput()
{
try
{
if(!$this->isDataValid())
throw new Exception('You Have Entered Invalid Characters In The Form.');
if(!$this->checkToken())
throw new Exeption('Please Try Again Later, Or Contact Us For Help.')
}
}
private function isDataValid()
{
$this->_userMail = $this->filterInput($_POST['userMail']);
$this->_userPassword = $this->filterInput($_POST['userPassword']);
return () ? true : false;
}
private function filterInput($tempVar)
{
return preg_replace('/[^a-zA-Z0-9]/','',$tempVar);
}
private function checkToken()
{
$this->_userToken = $_POST['userToken'];
return ($this->_userToken == $_SESSION['userToken']) ? true : false;
}
private function sendToDB()
{
}
}
?>
Many Thanks
Chris