views:

53

answers:

3

Hi,

I have been trying to output XML with php but encountered a strange(!) error in Internet Explorer.

The expected xml output is this:(simplified)

<root>
<match_id>12</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>453</fan_id>
</root>

I am producing this output with the following php code:

echo "<?xml version='1.0' encoding='utf-8' ?>
<root>
<match_id>"; echo $match->getId(); echo "</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>".$_SESSION['user_id']."</fan_id>
</root>";

In firefox, the output is same as expected. However, in IE, the output is this:

<?xml version="1.0" encoding="utf-8" ?> 
 <root>
  <match_id>0</match_id> 
  <stadium_id>43</stadium_id> 
  <tribune_id>2</tribune_id> 
  <fan_id /> 
  </root>

This is a really annoying error. I have set the php header for xml output, and changed lots of other things but could not make it work.

the $match->getId() part is just returning an integer but IE always shows this value as 0. If I set fan_id and match_id with hands in other words write it explicitly, ie shows the values correctly.

By the way, i am using this xml output in flash (as3) and this also shows the same result with IE.

What am i doing wrong?

any help is appreciated...

A: 

What browser is used has no effect on how your PHP executes (because it's executed on server, not in browser). Notice that in the second example your fan_id is also empty, which indicates something's wrong with your session setup. Investigate this.

Having said all that: did you consider using simplexml() to output XML from PHP? It's much more fun to use than echoing tags.

Mchl
+1  A: 

This looks like it's due to a session difference - the IE session isn't storing the user id. Similarly, $match->getId() is actually 0 - I imagine you'd get a similar result using Chrome or Safari or a web browser on any other computer.

One other thing: Flash may not be sending the PHP session cookie to the server on the request - which would match the IE behavior / no valid session.

pygorex1
Yeah, this problem is solved by considering the session issues. I have been using firefox from the beginning and at somewhere, sessions are set in firefox. However, in IE, session values are not set hence the variables which depend on session values are shown as broken. Having working for long hours, sometimes other eyes can see the problem easily.Thank you pygorex1.
isa
+1  A: 

Try:

  header( "content-type: application/xml; charset=ISO-8859-15" );   

OR

$doc = new DOMDocument;

$root = $doc->createElement('root');  
$doc->appendChild($root);

$match_id = $doc->createElement('match_id', $match->getId());
$root->appendChild($match_id);

$stadium_id = $doc->createElement('stadium_id', '43');
$root->appendChild($stadium_id);

$tribune_id = $doc->createElement('tribune_id', '2');
$root->appendChild($tribune_id, '2');

$fan_id = $doc->createElement('fan_id', $_SESSION['user_id']);
$root->appendChild($fan_id);

echo $doc->saveXML();

//$doc->save('file.xml'); // if you want to write to file 
Soliman