SimpleXML Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

SimpleXML Problem

Post 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>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I know I don't have to base64 encode.

What do you mean?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Does the error only occur when you run that function? What, exactly, is happening in user_exists?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

And if you run the script without adding a new user, the XML file does not change?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

It does change, but instead of adding a user it replaces the last user with the one I try to add.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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)
Post Reply