0

I have check many sample codes but still struggling to make mine work properly. I am trying to generate an XML output from my PHP script.

login.php

<?php

    mysql_connect("localhost", "root", "") or die("cannot connect"); 
    mysql_select_db("ConquestOfLancaster") or die("cannot select DB");

    $username = $_REQUEST['username'];
    $password = sha1($_REQUEST['password']);

    $username = mysql_real_escape_string($username);

    $domDoc = new DOMDocument;
    $rootElt = $domDoc->createElement('root');
    $rootNode = $domDoc->appendChild($rootElt);

    $sql = "SELECT user_id, user_name FROM tbl_user WHERE user_name='$username' and user_pass='$password'";
    $result = mysql_query($sql);

    $count=mysql_num_rows($result);

    if($count > 0){

        //echo("welcome");
        $rootAtr = $domDoc->createAttribute('success');
        $rootAtrVal = $domDoc->createTextNode('TRUE');
        $rootAtr->AppendChild($rootAtrVal);
        $rootElt->AppendChild($rootAtr);

        $subElt = $domDoc->createElement('username');
        $subNode = $rootNode->appendChild($subElt);

        $textNode = $domDoc->createTextNode($username);
        $subNode->appendChild($textNode);

        echo htmlentities($domDoc->saveXML());
    } else{

        //echo("not exist");
        $rootAtr = $domDoc->createAttribute('success');
        $rootAtrVal = $domDoc->createTextNode('FALSE');
        $rootAtr->AppendChild($rootAtrVal);
        $rootElt->AppendChild($rootAtr);

        echo htmlentities($domDoc->saveXML());
    }

?>

and the output on browser is

<?xml version="1.0"?> <root success="TRUE"><username>user1</username></root>

My major problem is that my iOS app that interact with this PHP can't parse this result. What I am doing wrong?

4
  • First thing that is very wrong is to use depricated mysql_* command set. Use PDO or mysqli Commented Dec 2, 2012 at 17:19
  • Would that changed the way that XML generated? I mean it is an important mistake from my side? Commented Dec 2, 2012 at 17:22
  • Would not change xml but it is bad idea to use dated unsupported technologies in a new code. ca1.php.net/manual/en/function.mysql-connect.php Commented Dec 2, 2012 at 17:24
  • It won't, but with current code, your application is vulnerable to SQL injection. Switching to mysqli/PDO and using prepared statements will help you prevent that. Commented Dec 2, 2012 at 17:26

1 Answer 1

1

Did you try to take out htmlentities?

As what you send is :

&lt;xml version ...

not

<?xml version="1.0"?> <root success="TRUE"><username>user1</username></root> 

as you think

Sign up to request clarification or add additional context in comments.

3 Comments

There is a way to print it out as real XML output? I have removed htmlentities and now nothing on the screen. I am new in PHP coding and still struggling. Sorry for silly questions..
You should get user1 on the screen, if you did not send proper headers it is correct output, you can always try to view page source. And just to be sure you replaced echo htmlentities($domDoc->saveXML()); with echo $domDoc->saveXML(); in both places in your if statement.
Woohoo!!! I have changed what you suggested and add, header('Content-Type: application/xml'); Now, looks like XML output :) However, on the top says, "This XML file does not appear to have any style information associated with it. The document tree is shown below." Needs to worry?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.