Page 1 of 1

Comparing strings from XML file to POST

Posted: Sun Oct 10, 2010 11:59 am
by sunegtheoverlord
Hi everyone,

This one has got me tearing my hair out.

I've got a form the outputs to the same page to collect username and password. i then want to compare this input with the username and password in an XML file.

i can echo the values from the POSTs and the XML file and they look exactly the same on screen but when i try to compare them they will not give out a match.

i've been using the Zend framework (for the first time i may add).

Code: Select all

<div id="welcome">
    <h1>Please login</h1>
<form method="post" action ="login.php">
<table align="center">
	<tr>
		<td align="right">
		username:
		</td>
		<td align="left">
		<input type="text" name="username">
		</td>
	</tr>
	<tr>
		<td align="right">
		password:
		</td>
		<td align="left">
		<input type="text" name="password">
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
		<input type="submit" name="submit">
</table>
	

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

// read XML config file
$user = new Zend_Config_Xml('user.xml');

$a = $user->username;
$b = $user->password;

//check if username and password match the xml file
if($_POST['username'] == $a && $_POST['password'] == $b){
	echo "Welcome " . $user->firstname . " " . $user->lastname;
}
else {
	echo "Either Username or password has been entered incorrectly";
}

echo "<br>POST - " . $_POST['username'];
echo "<br>xml - " . $user->username;

echo "<br>POST - " . $_POST['password'];
echo "<br>xml - " . $user->password;


?>
	
Somebody please put me out of my misery!!!!

Re: Comparing strings from XML file to POST

Posted: Sun Oct 10, 2010 12:02 pm
by John Cartwright
//cocks gun

You probably need to cast your username/password to string, otherwise your comparing a string to an object.

Code: Select all

(string)$user->username == $_POST['username']
Otherwise, use var_dump() on both variables to determine their exact contents.

Re: Comparing strings from XML file to POST

Posted: Sun Oct 10, 2010 12:31 pm
by sunegtheoverlord
Hi John,

Thanks for your help, i feel i'm getting there now.

I tried the string option but that didn't work. when i do var_dumps on the xml data i get something like:-

string(11) " jblogg "

as you can see the actual value i want to compare is only 6 chars long and the var_dump says we've got 11.

So i can see wy its not working.

now i suppose my question is how do i format the xml data so i only get the characters?

S

Re: Comparing strings from XML file to POST

Posted: Sun Oct 10, 2010 12:40 pm
by John Cartwright
It looks like your data is padded with spaces. I would first look at the xml file to make sure there are not any surrounding spaces, i.e,

<username> Jcart </username>
vs.
<username>Jcart</username>

Otherwise, you could try passing your xml values through trim() to remove padded spaces.

Re: Comparing strings from XML file to POST

Posted: Sun Oct 10, 2010 12:48 pm
by sunegtheoverlord
Hi John,

Thankyou, Thankyou, Thankyou.

There were leading and following spaces in the xml file, perhaps because i used tab?

It works now thank god. time for a glass of wine....

S