Page 1 of 1

SimpleXML Problem

Posted: Fri Jul 27, 2007 1:12 pm
by toasty2
I'm making a XML-based user system, I am having problems with my function for adding a user...It seems to replace the last user with the one I try to add, here's my code:

Code: Select all

function add_user($username, $password)
{
	global $users;
	if(!user_exists($username))
	{
		$newuser = $users->addChild('user', '');
		$newuser->addChild('username',$username);
		$newuser->addChild('password',hash('sha512', base64_encode($password)));
	}
}
($users is the XML file, loaded with simplexml_load_file())
The user file is similar to this:

Code: Select all

<users>
<user>
<username>abc</username>
<password>(password)</password>
</user>
<user>
<username>testuser123</username>
<password>(password)</password>
</user>
</users>

Posted: Fri Jul 27, 2007 2:35 pm
by Ambush Commander
Quick note: base64_encode'ing the password is unnecessary if you're hashing it.

It shouldn't be overwriting the nodes. Have you tried calling the function in isolation?

Posted: Fri Jul 27, 2007 2:37 pm
by toasty2
I know I don't have to base64 encode.

What do you mean?

Posted: Fri Jul 27, 2007 2:43 pm
by superdezign
Does the error only occur when you run that function? What, exactly, is happening in user_exists?

Posted: Fri Jul 27, 2007 2:55 pm
by toasty2

Code: Select all

function user_exists($usr)
{
	global $users;
	if(in_array($usr,(array)$users->user))
	{
		return true;
	}
	else
	{
		return false;
	}
}
It checks if the specified user is already in the XML file.

Posted: Fri Jul 27, 2007 3:04 pm
by superdezign
And if you run the script without adding a new user, the XML file does not change?

Posted: Fri Jul 27, 2007 9:15 pm
by toasty2
It does change, but instead of adding a user it replaces the last user with the one I try to add.

Posted: Fri Jul 27, 2007 9:42 pm
by superdezign
toasty2 wrote:It does change, but instead of adding a user it replaces the last user with the one I try to add.
But I mean if you simply comment out the line where you call the add_user() function, does the XML file stay the same?

Posted: Sat Jul 28, 2007 10:58 am
by toasty2
Gah, silly mistake...I wasn't saving the file, but I still don't get why when I list the users it doesn't show one added each time instead it shows the last one replaced.

(Problem Solved)