Jump to content

[SOLVED] Something's going wrong in my user.class.php but I do not have a clue what.


matthewhaworth

Recommended Posts

Any advice on how to debug this?

 

<?php
/*
* user.class.php
*
* @package 
* @author matthew haworth
* @copyright 2007
* @version $Id$
* @access public
*/
class user
{

    private $_db;

    //private $_email;
    //private $_password;

    function __construct(&$db)
    {
		$this->_db = $db;
    }

function checkLogin() {

	if($_SESSION['user']['loggedin']) {

		return true;

	}
	else
	{
		return false;
	}

}

function register($email, $password) {

	// Validation.
        
	$email = $this->_db->realescapestring($email);
	$email = strip_tags($email);
	$password = $this->_db->realescapestring($password);
	$password = strip_tags($password);

	// End validation.	

	$password = md5($password);
        $date = date('Y-m-d H-i-s');
        
        // See if the user already exists..
        if($this->login($email, $password, false)) {
		return false;
	}
        
	$sql = "INSERT INTO users(email, password, registered) VALUES('".$email."', '".$password."', '".$date."');";

	if($this->_db->query($sql)) {
		$this->login($email, $password);
		return true;
	}
	else
	{
		return false;
	}

}

    private function loadDetails($email)
    {

        $sql = "SELECT iD, email, registered FROM users WHERE email='". $email ."';";
        $query = $this->_db->query($sql);
        $userinfo = $this->_db->fetch_assoc($query);
        foreach ($userinfo as $key => $value)
        {
            $_SESSION['user'][$key] = $value;
        }

	if(isset($_SESSION['user']['email'])) 
	{
		return true;
	}
	else
	{
		return false;
	}

    }

    function login($email, $password, $sessions = true)
    {

        // Validation.
        
	$email = $this->_db->realescapestring($email);
	$email = strip_tags($email);
	$password = $this->_db->realescapestring($password);
	$password = strip_tags($password);

	// End validation.

	// Code the password to compare with that stored at registration.
        $password = md5($password);

	// Ask for email and password from MySQL.

        $sql = "SELECT email, password FROM users WHERE email='" . $email .
            "'  AND password='" . $password . "';";

	// See if the user data exists in the database.

        if ($this->_db->numrows($sql) > 0)
        {
		// I don't know why but I wanted to have the class know the email, on initialisation
            //$this->_email = $email; // Infact, scrap that now.
            
		// If I want to apply sessions, here they are.
		if($sessions) {
			// Set sessions for rest of user details..
			if($this->loadDetails($email)) {

				// Set sessions as loggedin, I did this here for security reasons.
				$_SESSION['user']['loggedin'] = true;
				// Return the function , everything's set.

				return true;

			}
			else
			{
				// If the loadDetails function returns false, return this function false also.
				return false;
			}
		} else {
			return true;
		}
        
	// If the user doesn't exist in the database...
        }
        else
        {
		//...return false.
            return false;

        }

    }


}
?>

 

I'm running it with this..

 

<?php

/**
* login.test.php
* @author matthew
* @copyright 2007
*/

require("init.php");
if(isset($_POST['email']))
{
if($user->login($_POST['email'], $_POST['password']))
{
header("location: login.test.php");
}
else
{
	echo "FATALERROR";
}
}
?>
<html>
<head>
<title>Registration test</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
email:<input type = "text" id="email" name="email" /><br />
password:<input type = "text" id="password" name="password" /><br />
<input type="submit" value="login" /></form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.