ID: 27418 User updated by: wb at pro-net dot co dot uk Reported By: wb at pro-net dot co dot uk Status: Open Bug Type: *XML functions Operating System: FreeBSD, WindowsXP PHP Version: 5.0.0b4 (beta4) New Comment:
I hav ebeen reading through the PHP-DEV archive and found message: http://marc.theaimsgroup.com/?l=php-dev&m=107807524506690&w= Which contains... >>>> foreach($xml->user as $user){ >>>> if (utf8_decode($user->login) == $login && >>>> utf8_decode($user->password) == $password) { >>>> // valid users >>>> } >>>> } >>>> >>>> Both seem like they should work, but neither do. This infact does work in the example below with the php5 versions that i have tried. Just thought that i would point it out even if i am not quite doing things correctly (what should eb the correct way?). My main issue/question for this bug is how do i traverse over multiple site elements for each user? And why does is go from being [site] => Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) ) To: $user->site is: Array ( [0] => simplexml_element Object ( ) [1] => simplexml_element Object ( ) ) depending on how you call it? Previous Comments: ------------------------------------------------------------------------ [2004-02-27 09:43:44] wb at pro-net dot co dot uk Description: ------------ When using simpleXML you are unable to fetch some information. The information is displayed with print_r() but you can't get it it directly. Use the example provided as an example. Reproduce code: --------------- <?php header('Content-type: text/plain'); $xmlstr = '<?xml version="1.0"?> <access> <user> <login>user1</login> <password>letMeIn</password> <site>www.pro-net.co.uk</site> <site>www.example.com</site> </user> <user> <login>user2</login> <password>myPassword</password> <site>www.pro-net.co.uk</site> </user> </access>'; $nl = "\r\n"; $xml = simplexml_load_string($xmlstr); print('Current PHP version is : '.phpversion().$nl.$nl); print('$xml is:'.$nl); print_r($xml); print($nl.$nl); // Test authentication to get the correct user information $login = 'user1'; $password = 'letMeIn'; print($nl.$nl.'Trying to authenticate "'.$login.'" with password "'.$password.'".'.$nl.$nl); $isAuth = false; foreach($xml->user as $user){ if(utf8_decode($user->login) == $login && utf8_decode($user->password) == $password){ $isAuth = true; break; } } if(!$isAuth){ print($nl.$nl.'Invalid User.'.$nl); die(); } // So lets output the variables to see what we have... print('$user is:'.$nl); print_r($user); print($nl.$nl); print('$user->site is:'.$nl); print_r($user->site); print($nl.$nl); print('$user->site[0] is:'.$nl); print_r($user->site[0]); print($nl.$nl); ?> Expected result: ---------------- $xml is: simplexml_element Object ( [user] => Array ( [0] => simplexml_element Object ( [login] => user1 [password] => letMeIn [site] => Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) ) [1] => simplexml_element Object ( [login] => user2 [password] => myPassword [site] => www.pro-net.co.uk ) ) ) Trying to authenticate "user1" with password "letMeIn". $user is: simplexml_element Object ( [login] => user1 [password] => letMeIn [site] => Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) ) $user->site is: Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) $user->site[0] is: www.pro-net.co.uk Actual result: -------------- $xml is: simplexml_element Object ( [user] => Array ( [0] => simplexml_element Object ( [login] => user1 [password] => letMeIn [site] => Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) ) [1] => simplexml_element Object ( [login] => user2 [password] => myPassword [site] => www.pro-net.co.uk ) ) ) Trying to authenticate "user1" with password "letMeIn". $user is: simplexml_element Object ( [login] => user1 [password] => letMeIn [site] => Array ( [0] => www.pro-net.co.uk [1] => www.example.com ) ) $user->site is: Array ( [0] => simplexml_element Object ( ) [1] => simplexml_element Object ( ) ) $user->site[0] is: simplexml_element Object ( ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27418&edit=1