Jump to content

PHP XML data check help


YuriRF

Recommended Posts

hi,

 

Im trying to make a user/password check with a XML file( i know its not safe but its just for play) and i seem to be on the right direction. i can check user name and if the user name is correct the it checks the password and if the password its correct it keeps going if not it stops. my problem is. if the user name entered is not on the XML i want it to show user not found. i got it to do this but since it was inside a foreach it shows it like if there is 3 users it says it 3 times.

 

i want it to just say it once. i tried puting the code lines outside the foreach but it dosent work.

 

here is my code.

 

 

<?php

$username=$_POST['user'];

$password=$_POST['password'];

 

if(file_exists('userdb.xml')){

$xml= Simplexml_load_file('userdb.xml');

usercheck($xml,$username,$password);

}

 

function usercheck($xml2,$username2,$password3){

$i=-1;

foreach($xml2->userdata->user as $userin){

$i++;

 

if(strcmp($userin, $username2)==0){

$password2=$xml2->userdata->user[$i]['pas'];

 

if(strcmp($password2,$password3)==0){

echo 'correct password';

}else{

echo 'Wrong password';

}

}

}

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/136114-php-xml-data-check-help/
Share on other sites

NVM guys i fixed it. i just made a variable for checking. but still if u see something worng with the code please assist.

 

cheers,

 

CODE:

 

<?php

$username=$_POST['user'];

$password=$_POST['password'];

 

if(file_exists('userdb.xml')){

$xml= Simplexml_load_file('userdb.xml');

usercheck($xml,$username,$password);

}

 

function usercheck($xml2,$username2,$password3){

$i=-1;

$f=0;

foreach($xml2->userdata->user as $userin){

$i++;

 

if(strcmp($userin, $username2)==0){

$f=1;

$password2=$xml2->userdata->user[$i]['pas'];

 

if(strcmp($password2,$password3)==0){

echo 'correct password';

} else{

echo 'Wrong password';

}

}

}

if($f==0){

echo 'user not found';

}

}

 

?>

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.