ID:               27418
 Comment by:       adam at trachtenberg dot com
 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:

To parphrase Arthur C Clarke: You get different answers 

because of magic. Well, advanced PHP technology that is 

indistinguishable from magic.



The current solution to your problem is to cast the 

variable before passing it to the function, like this:



utf8_decode((string) $user->login)



It would be better if you didn't need to do this, but 

there's no clean solution to this problem right now. 

We're working on it.


Previous Comments:
------------------------------------------------------------------------

[2004-03-02 08:10:51] wb at pro-net dot co dot uk

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?

------------------------------------------------------------------------

[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

Reply via email to