I am that geek who is working in the programming field, in love with poetry, always appreciate good lyrics and addicted to loud music.
[PHP 5 + XML]: XML Login
A simple tutorial to show how PHP and XML can be use to store user info. For this tutorial, the main purpose is performing login action.
We begin by creating the xml file: (filename: login.xml)
-
-
<?xml version="1.0" encoding="ISO-8859-1"?>
-
-
<users>
-
-
<username>SHiDi</username>
-
<password>c822c1b63853ed273b89687ac505f9fa</password>
-
<fullname>Mohd Rashidi Bin Mohd Zin</fullname>
-
<active>1</active>
-
-
<username>John</username>
-
<password>0659c7992e268962384eb17fafe88364</password>
-
<fullname>John Doe III</fullname>
-
<active>0</active>
-
-
</users>
-
As you can see, this file stores all users information. Password stored are hashed with md5.
Next is the PHP action file, which handles all the action: (filename: login.php)
-
-
<?php
-
/**
-
* @author : Mohd Rashidi Bin Mohd Zin
-
* @since : Jun 8, 2008
-
*/
-
-
$file = "login.xml";
-
-
$username = $_POST[‘username’];
-
$password = $_POST[‘password’];
-
-
{
-
$xml = simplexml_load_file($file);
-
-
$count = 0;
-
foreach($xml->username as $currUser)
-
{
-
{
-
break;
-
}
-
-
$count++;
-
}
-
-
if ($xml->active[$count] == 1)
-
{
-
{
-
echo "Valid user logged in!";
-
}
-
-
else
-
{
-
echo "Password does not match. Come on! Try harder!";
-
}
-
}
-
-
else
-
{
-
echo "User is inactive. Choose another!";
-
}
-
}
-
-
else
-
{
-
echo "Critical Error: File not found!";
-
}
-
?>
-
This file will first ensure that required xml file is available. Then it will run through a loop finding the given username. Once found, it will match the password entered against the password stored. The variable count is to tell the application which data is selected.
Finally is the HTML file which contain the form: (filename: login.html)
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>PHP XML Login</title>
-
</head>
-
<body>
-
<form method="post" action="login.php">
-
Username: <input type="text" name="username">
-
<br />
-
Password: <input type="password" name="password">
-
<br />
-
<input type="submit" name="submit" value="Login">
-
</form>
-
</body>
-
</html>
-
Here, the password for SHiDi and John are google and abc123456 respectively.
Difficulty Encountered
As for now, the difficulty I found is that, the username entered must be case-sensitive. Compare if data retrieved from database, it would be case-insensitive. If you have any idea or suggestion on how to overcome this, please feel free to share at the comment section. Or you can email me your suggestion.
Happy coding
Possibly related posts (generated by Yet Another Related Posts Plugin):
- [PHP + jQuery ThickBox]: AJAX Login
- [PHP + MySQL + Javascript]: Dynamic AJAX Form
- PHP + MySQL + cURL: Single Sign On With Multiple Domains
- PHP + MySQL + jQuery: Deleting multiple selections
- [PHP 5 + jQuery]: JSON Cross Domain
Tags: login, php, tutorial, xml




D,
btw,ape kebaikan store login kat dlm XML punye file?
Mcm mane ko handle security dari direct xcess ke XML file tu sendri?
Aku tak sure kenape ko buat login dlm XML,adekah ko nk menunjukkan salah satu alternatif storan data atau ade kebaikan lain dlm kaedah ni..
reply kat mail aku balik
Hi there,
I have been looking for a simple tutorial like this one, and I must say you have explained things very well! But when I run my version of the login, I always get a “user is inactive” message, even if the active status for the user is “1″. My xml document structure is a bit different… I put the username, password and active status in between … tags, is that what is causing the problem? Sorry if my question is silly, I’m new to php.
Hey, nevermind my previous post. I followed your structure for storing the user details, and everything is working fine. Thank you for a great, yet simple tutorial! Cheers mate!
I’m always get the msg “user is inactive”. How can i solve it?
I’m always getting the msg which pervious discussed. How can i solve it?
Hi, im hitting a couple of probs.
1) says user inactive when its set to 1
2) if there is a successful log in – nothing happens. Where is the redirect code to the content?
Solution for the “user is inactive” error.
Hi all, I read the error you have found, and so tested the code myself to find that I too had the problem and so searched out the solution.
Its a tricky one to spot, but most of you will have copied and pasted the code, herein lies your problem. Simply manually type the “‘” marks in $username = ['username']; and $password = ['$password'];
Problem solved!
For moi.
If you use the above solution that should fix your first problem.
For the second it depends on what you want to happen when a user is logged in. If you want the user to be directed to another page, type:
header(“Location: http://www.domain.uk/file.html“);
in place of:
echo “Valid user logged in!”;
If you do this I would suggest setting the new page to receive a variable that confirms the user is logged in, so that users can just go directly to the page using the URL.
Enjoy
Hi,
. I changed the whole thing,but its not working.
Whatever I do.. I keep getting the same error
Tried and tested here and all working fine, just 1 question, how do i once i have re-directed a user logged on to whateverblahblah.html, show the logged on users name in a Welcome statement…..ie user logs on correctly and then the redirected page that shows…Welcome John Doe etc??
I tried this in my .php file,
$xml = simplexml_load_file(‘$file’);
var_dump($xml);
and the output is bool(false).I am not sure what is stopping the file from being loaded. I even tried to load it as a string with the help of this, but still no luck.
$xml = simplexml_load_string(file_get_contents(‘login.xml’));
I tried with hard coding the URL,still no luck. Am I missing something here. I am sorry, I am new to PHP.
Regards
V.C.Madhu
This is a very nice tutorial. I added a function where it logs the date and time for each user each time they login. I hope someone finds it useful.
Add this function before (not sure if it matters) your code
function log_login($username){
$file = fopen(“logged_in.txt”, “a+”);
if ($file != null){
$time = localtime(time(), true);
$str = sprintf(“User \”$username\” logged in on %s at (%d:%d:%d). \r\n”,
date(“Y-m-d”),
$time["tm_hour"],
$time["tm_min"],
$time["tm_sec"],
$username);
fwrite($file, $str);
fclose($file);
}
}
and call it log_login($username); on login success