Jump to content

Crazy Php/xml Problem


Fmdpa

Recommended Posts

I am absolutely stumped on this problem. I've been trying to solve it for hours and hours. The problem is that whenever I perform the following code, instead of appending this once like it should:

<rating>   <value>(1-5)</value>   <ip>127.0.0.1</ip>   <id>id</id></rating>

It appends it 21 times! Every time! I even switched my code to using SimpleXML instead of DOMDocument and it still did it! When I dump the XML code onto the page, it only shows it appended once. I am thoroughly confused. Anyway, here's SermonRatings.php:

<?phprequire_once 'global.php';class SermonRating {       public function add($rating, $sermon) {	   	    $domDoc = new DOMDocument();	    $domDoc->formatOutput = true;	   	    $doc = $domDoc->load('sermon_ratings.xml');	   	    $root = $domDoc->getElementsByTagName('ratings')->item(0);	   	    $rating_node = $domDoc->createElement('rating');	   	    $value = $domDoc->createElement('value');	    $value->appendChild($domDoc->createTextNode($rating));	   	    $ip = $domDoc->createElement('ip');	    $ip->appendChild($domDoc->createTextNode($_SERVER['REMOTE_ADDR']));	   	    $sermon_id = $domDoc->createElement('id');	    $sermon_id->appendChild($domDoc->createTextNode(xmlentities($sermon))); // xmlentities() defined in global.php; it is not the problem because this was happening before I added the function	   	    $rating_node->appendChild($value);	    $rating_node->appendChild($ip);	    $rating_node->appendChild($sermon_id);	   	    $root->appendChild($rating_node);	    $saved = $domDoc->save('sermon_ratings.xml');	   	    return $saved;    }   // ...other unrelated methods...}  $sr = new SermonRating;echo $sr->add(1, 'sermon.wma');

And here's sermon_ratings.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ratings [    <!ELEMENT ratings (rating*)>    <!ELEMENT rating (value , ip , id)>    <!ELEMENT value (#PCDATA)>    <!ELEMENT ip (#PCDATA)>    <!ELEMENT id (#PCDATA)>]><ratings>   </ratings>

After I visit sermonRatings.php and those last two lines of code trigger the method execution and the file is modified and saved, there are 21 child <rating> elements in the file. I'm totally confused.

Link to comment
Share on other sites

I can't see anything in your code that would do that. Maybe there's some other code that you thought irrelevant that's doing something (like calling the add() method 20 times) Anyways, if I can find some time I'll test that code myself unless somebody finds the solution sooner.

Link to comment
Share on other sites

Well it ends up the culprit was a single line of code in my global.php file:

header('Location: ' . $_SERVER['PHP_SELF']);

It related to my login system and it was only supposed to run once when the person first visited the page. But I wrote the condition in which it was encapsulated incorrectly, causing it to run each time the page loaded. Honestly, I don't know why the loop ever stopped, and why it always stopped at 21. It also baffles me that the add() function was triggered and run each time because, theoretically, the redirect should have happened in the program execution prior to executing the add() function.

Link to comment
Share on other sites

When the header is sent, the program continues executing. The redirect is done by the client. This is why location headers are always followed by exit; The browser probably quits redirecting after the 20th header that is sent to it with the same location.

Link to comment
Share on other sites

OK, that makes sense. Thanks for explaining that! It ended up that that line of code was the source of some other unrelated problems I was having. The feeling of satisfaction when problems are solved is almost worth having the problem. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...