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
toasty2
Forum Contributor
Posts: 361 Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA
Post
by toasty2 » Fri Jul 27, 2007 1:12 pm
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>
Ambush Commander
DevNet Master
Posts: 3698 Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US
Post
by Ambush Commander » Fri Jul 27, 2007 2:35 pm
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 » Fri Jul 27, 2007 2:37 pm
I know I don't have to base64 encode.
What do you mean?
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Jul 27, 2007 2:43 pm
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 » Fri Jul 27, 2007 2:55 pm
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.
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Jul 27, 2007 3:04 pm
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 » Fri Jul 27, 2007 9:15 pm
It does change, but instead of adding a user it replaces the last user with the one I try to add.
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Jul 27, 2007 9:42 pm
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 » Sat Jul 28, 2007 10:58 am
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)