Dianna
Dianna

Reputation: 411

Why doesn't my header() function redirect?

I have the following script that recieves an xml file and reads it with file_get_contents(). I want to redirect this xml to a file called "register" but it doesn't work and I have no idea why.

 <?php
     $xml_post=file_get_contents('php://input');
     $xml=simplexml_load_string($xml_post);
     if($xml->action=='register')
       {
        header('Location: http://proiectis.host22.com/register.php');
        exit;
      }

 ?>

I would appreciate your help.

This is my xml:

  <xml version="1.0">

     <action>register</action>

     <parameters>

          <name>Ionel Popescu</name>

          <username>Ionel P</username>

      <email>[email protected]</email>

      <password>abdef01</password>

    </parameters>

    </xml>

Upvotes: 2

Views: 198

Answers (3)

Johannes Egger
Johannes Egger

Reputation: 4041

I think there is a misunderstanding in the XML document. You have no XML header in your document but you use the <xml>-element as the root element. Try this instead:

<?xml version="1.0"?> <!-- This is a typical xml header -->
<result> <!-- An xml document must contain exactly 1 root element -->
  <action>register</action>
  <parameters>
    <name>Ionel Popescu</name>
    <username>Ionel P</username>
    <email>[email protected]</email>
    <password>abdef01</password>
  </parameters>
</result>

The original PHP code should work now.

Upvotes: 1

Ruben Nagoga
Ruben Nagoga

Reputation: 2218

$xml->action returns SimpleXMLElement instead of string which you expected. So use this code:

 <?php
     $xml_post=file_get_contents('php://input');
     $xml=simplexml_load_string($xml_post);
     if((string)$xml->action == 'register')
       {
        header('Location: http://proiectis.host22.com/register.php');
        exit;
      }

 ?>

Upvotes: 0

flowfree
flowfree

Reputation: 16462

Try:

if ((string) $xml->action === 'register') {
  // code
}

And you don't need the </xml> in your xml file.

Upvotes: 2

Related Questions