PHP MVC Login System

R44

Paragon
Joined
Jan 31, 2013
Messages
1,614
Reaction score
0
FP$
2,188
So, I'm busy creating Asperger's Network's login system.

I'm getting this as an error when I submit my form:

Notice: Undefined property: UserController::$model in C:\inetpub\wwwroot\asperger-dev\controllers\UserController.php on line 30

Fatal error: Call to a member function Login() on a non-object in C:\inetpub\wwwroot\asperger-dev\controllers\UserController.php on line 30

Pages:

login.tpl
Code:
<?php include HOME . DS . 'includes' . DS . 'header.inc.php'; ?>
<div id='pageheader-bg' style="background-image: url(http://farm9.staticflickr.com/8534/8604393943_050634bd5d_o.jpg) !important;" >
			<h1>Log In to Asperger's Network</h1>
		</div>
<div class="breadcrumb"><a href="/">Asperger's Network</a> &raquo; Log In</div>
	<h1>Log In to Asperger's Network</h1>
<div style="float:left; width:30%; margin: 0 auto;">
	<form method="post" action="/user/login" name="submit">
		<center>
			<table border="0" width="100%">
				<tr>
					<th style="background:none!important; color:#000!important;">Username</th>
					<td>
						<input type="text" name="user">
					</td>
				</tr>
				<tr>
					<th style="background:none!important; color:#000!important;">Password</th>
					<td>
						<input type="password" name="pass">
					</td>
				</tr>
				<tr>
					<th style="background:none!important; color:#000!important;">Remember Me</th>
					<td>
						<input type="checkbox" name="reme" value="1">
					</td>
				</tr>
				<tr>
					<th style="background:none!important; color:#000!important;"><input type="submit" value="Log In" class="btn"></th>
					<td>
						
					</td>
				</tr>
			</table>
		</center>
	</form>
</div>
<div style="float:right; width:64%; border-left:1px solid #ccc; padding:8px;">
	<b>Welcome to Asperger's Network</b>
	<p>You've been redirected to this page either because you have bookmarked it, or something has happened and we were unable to log you in. That's OK, because this page will allow you to attempt to log in again. </p>
	<b>No Account?</b>
	<p>No worries - Head over to our <a href="/user/register">Registration Section</a> and create an account. We'll ask for a few details from you and set you up with an account in moments. From there, you'll be able to create wiki articles and submit news.</p>
	<b>Try something different</b>
	<p>This site isn't another promotion forum, isn't another gaming forum and isn't another social network - it's a site that actually means something to a lot of people. We would love you to become a part of the site and to learn, and share experiences you might have had with people in your life - That's what we are here for</p>  
</div>
<?php include HOME . DS . 'includes' . DS . 'footer.inc.php'; ?>

UserController.php
Code:
<?php

class UserController extends Controller 
{
	public $LoggedIn;
	public $id;
	
	public function __construct($model, $action)
    {
        parent::__construct($model, $action);
        $this->_setModel($model);
    }

    public function Login() {
	/**
     *  Name:		login()
     *  Vars:		N/A
     *  Purpose:	Log a user in based on their entered credentials.
     *  Return:		A view with a login form, or a header redirect.
     *  Edited:		12/AUG/13 ^LR
     */
	
		//if($this->CheckSession() === true) { // You are logged in.
		//	header("Location: " . URL); // Redirect to index.
		//}
		//else{

			if($_POST) { // The form has been _POSTed
				try {
			 		$this->model->Login($_POST['user'], $_POST['pass']); // Send login request to model
			 	} catch(Exception $e) {
			 		// Handle failed login
			 	} 
			}
		//}

		$this->_view->set('title', 'Log In');
        return $this->_view->output();
	}

	public function CheckSession() {
		return $this->model->CheckSession();
	}	

	public function Register(){
		// if($_POST['submit']) { // The rego form has been _POSTed
			
		// }
		require_once("/utilities/recaptcha.php");
		$publickey = "6Ld4K-YSAAAAAGT5mAnzoyTzl9LF2bNPngJSi1eY"; // you got this from the signup page
		$captcha = recaptcha_get_html($publickey);
		$this->_view->set("captcha", $captcha);
		$this->_view->set('title', 'Register');
        return $this->_view->output();
	}
}

?>

usermodel.php
Code:
<?php

class UserModel extends Model 
{
	public function Login($username, $password) {
		 $sql = "SELECT
                   user_password, pass_salt
                FROM 
                    users
                WHERE
                	username=?";
         
        $this->_setSql($sql);
		$user = $this->getRow(array($username)); // Pull the correct user from the DB where the username is as parsed.

		if($user) {
			$CheckHash = sha1($password, $user['salt']); // Create a new hash for comparison.
			if($CheckHash === $user['password']) { // The authentication was correct, proceed with login.
				try {
					$this->CreateSession($username, $salt);
					header("Location: " . URL);
				} catch(Exception $e) {
					// The session creation failed.
					return False;
				}
			} else {
				throw new Exception("Password was incorrect!"); // The user did not authenticate correctly.
				return False;
			}
		} else {
			throw new Exception("No user found!"); // The user was not in the DB.
			return False;
		}
	}

	public function CreateSession($username, $salt) {
		$expire = time()+3600; // 1 hour from now()
		$ip = $_SERVER['REMOTE_ADDR']; // Pull the visitor IP.

		$hash = sha1($username . $expire . $salt . $ip); // Create an authentication hash.

		setcookie("username", base64_encode($username), $expire, "/"); // Create a cookie with their username.
		setcookie("expire", base64_encode($expire), $expire, "/"); // Create a cookie with the cookie expiry time.
		setcookie("hash", $hash, $expire, "/"); // Create a cookie with the authentication hash.

		return True; // The session was created properly.
	}

	public function CheckSession() {
		if(isset($username)){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function Encrypt($password, $salt) {
		$hash = sha1($password . $salt);
		return $salt;
	}

	public function Register() {
		
	}

	public function resetPassword($username){
		$sql = "SELECT
                   *
                FROM 
                    users
                WHERE
                	username = ?
               	";
	}

	private function GenerateSalt() {
		$salt = sha1(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
		return $salt;
	}
}

?>

Any help would be appreciated. I need to get this fixed as soon as possible.<br /><br />-- 03 Sep 2013, 14:05:15PM --<br /><br />This is fixed - $this->_model->...
 
Back
Top Bottom