Jump to content

Recommended Posts

I have all my errors turned on in php.ini and i even have error_reporting(E_ALL); in my script, however it only displays an error message on the odd refresh of the page, now and then.. does my php have attitude or is this fixable? lol

Okay basically I'm just testing my register method in my user.class.  Here is my user.class

(I am a relatively new PHP programmer so please excuse my bad coding? I lose confidence very easily lol)

<?php
class user 
{
// Database object .
private $_db;
// Report object.
private $_report;
// User details.
private $_details = array();
// User authority.
private $_auth = 0;

function __construct($db, $report)
{
	$this->_db = $db;
	$this->_report = $report;

	// Set variables if necessary.
	if($this->checkAuthority())
	{
		$this->_details = $_SESSION['x'];
		$this->_auth = $_SESSION['auth'];
	}
	else
	{
		$this->_auth = 0;
	}

}

private function checkAuthority()
{
	if(isset($_SESSION['auth']))
	{
		if($_SESSION['auth'] == 1 || $_SESSION['auth'] == 2)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

public function logout()
{
	unset($_SESSION['x']);
	if($_SESSION['x'] == NULL)
	{
		$this->_report->addNote('Logout successful');
	}
	else
	{
		$this->_report->addError('Logout unsuccessful');
	}
}

public function register($details,$auth = 1)
{
	if(!is_array($details))
	{
		$this->_report->addError('Parameter passed incorrectly.');
		return false;
	}
	// Clean variables.
	// Notice the & means it's a reference, not a copy.
	foreach($details as $k=>&$v)
	{
		$v = $this->_db->clean($v);
		if($v == NULL || $v == "")
		{
			$this->_report->addError('Field ' . $k . ' is missing.');
		}
	}
	// Hash password with MD5.
	$details['password'] = md5($details['password']);
	// Make the joindate, and also make it comply to MySQL's format.
	$details['joindate'] = time();
	/*
	Making the birthdate

	This will require three drop down boxes: birthday, birthmonth and birthyear.
	*/
	$details['birthdate'] = strtotime($details['birthmonth'] . '/' . $details['birthday'] . '/' . $details['birthyear']);

	unset($details['birthday']);
	unset($details['birthmonth']);
	unset($details['birthyear']);

	// Add 'auth' to the array.
	$details['auth'] = $auth;

	// Create SQL to insert data.
	$sql = "INSERT INTO members ('email', 'password', 'username', 'joindate', 'birthdate', 'auth') VALUES(";
	$sql .= "'" . $details['email'] . "', ";
	$sql .= "'" . $details['password'] . "', ";
	$sql .= "'" . $details['username'] . "', ";
	$sql .= "'" . $details['joindate'] . "', ";
	$sql .= "'" . $details['birthdate'] . "');";
	// ..do so.
	$qry = $this->_db->query($sql);
	// Log user in.
	$this->login($details['email'], $details['password']);

	return true;
}

public function login($email, $password) 
{
	if(!($email && $password))
	{
		return false;
	}

	// Clean up variables.
	// May have a validation class later in the game : - ).
	$email = $this->_db->clean($email);
	$password = $this->_db->clean($password);		
	$password = md5($password);

	// Create SQL to find member information.
	$sql = "SELECT * FROM members WHERE email = '" . $email . "';";
	// Query the database with the SQL.
	$qry = $this->_db->query($sql);

	// Check to see if the user exists.
	if($this->_db->numrows() < 1)
	{
		// The user doesn't exist.
		$this->_report->addError('User does not exist');
		return false;
	}

	// Load user details.
	$this->_details = $this->_db->fetch($qry);

	// Check if the user's password is correct.
	if($this->_details['password'] != $password)
	{
		// The user's password is incorrect
		$this->_report->addError('Incorrect password');
		return false;
	}

	// Unset the password because it's not safe to have it in the enviroment
	// longer than it is needed.
	unset($this->_details['password']);

	// Create sessions.
	// Loop through every key and value assigning it to a session, except password and auth.
	// $_SESSION['x'] is the array in which all of the user details are stored.
	foreach($this->_details as $k=>$v)
	{
		if($k != 'auth')
		{
			$_SESSION['x'][$k] = $v;
		}
	}

	// Set authority digit.
	$_SESSION['auth'] = $this->_details['auth'];

	// /*
	$this->_report->addNote($_SESSION['auth']);
	// */
	return true;
}
}
?>

 

Here's where I call it

<?php
require("inc.php");

$man = array (
'email' => '[email protected]',
'password' => 'moe',
'username' => 'matthewhaworth',
'birthday' => '23',
'birthmonth' => '08',
'birthyear' => '1990');
$user->register($man);
$report->debug();
?>

Unless you post the error message, no one knows what type of problem you are having or where it is taking place in the code.

 

It just doesn't want to give me an error message any more.. I realised there was a problem with the SQL, I wasn't passing it 'auth'.  I put little echo 'Im here'; markers throughout my code and it follows the method through thoroughly... but just doesn't but the information into the db

Query unsuccessful:
INSERT INTO members('email', 'password', 'username', 'joindate', 'birthdate', 'auth')VALUES('[email protected]', '8ab752bb333186f0b1e6a35125ff0995', 'thomas', '1215835593', '651366000', '1');
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''email', 'password', 'username', 'joindate', 'birthdate', 'auth')VALUES('matthew' at line 1

 

I found an error message, I can take it from here. Thanks guys.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.