XML Login System

Build a Login and Registration System with XML

Feb 12th in Screencasts by Tim Cooper

Building an entire membership system can be a tedious, and time-consuming task. Tim Cooper is going to show us how to build the ENTIRE thing in roughly thirty minutes. Rather than using MYSQL as our database, Tim will instead demonstrate an alternate approach: using an XML file. We'll be reviewing PHP, .htaccess files, sessions, and more!

PG

Author: Tim Cooper

Hi, I'm Tim Cooper, aka techietim. I am currently a high school student in Nova Scotia, Canada with a lot of free time on my hands. I have been doing PHP work for about 4 years and I'm not planning on stopping.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Gareth February 12th

    Interesting article: I will always would rather use MySQL though..

    ( Reply )
  2. PG

    ThaClown February 12th

    Pff… man you are fast!
    Alot to learn from this vid, I guess I should first finish the “Diving into PHP” series!

    Thanks alot man.

    ( Reply )
  3. PG

    M.A.Yoosuf February 12th

    ha, its great, but how fat its practical, because its like flat file system

    ( Reply )
  4. PG

    Groningen February 12th

    Damm nice tutorial, you also worked it out realy fine.

    Greetings from the netherlands

    ( Reply )
  5. PG

    Eduardo February 12th

    I thought nothing could be faster than the speed of light…. I was wrong! LOL

    ( Reply )
  6. PG

    Some Funky Dude February 12th

    I think this is a great tutorial on using the SimpleXMLElement object

    one thing I didn’t understand why preg_replace() was necessary on the username field but not the email input field

    ( Reply )
  7. PG

    DKumar M. February 12th

    Impressive Tut Cooper… Nice and fast !!

    ( Reply )
  8. PG

    Alessandro February 12th

    Thank you, maybe it’s not a perfect solution for a login system, but for some other data it can be very useful.

    ( Reply )
  9. PG

    Paul Davis February 12th

    This is BRILLIANT! O think, the best tutorial I’ve seen on NetTuts, for me anyway.

    Thanks Tim!

    ( Reply )
  10. PG

    insic February 12th

    very nice! well explained.

    ( Reply )
  11. PG

    Dean February 12th

    Is it just me or can we not jump ahead in the video?

    ( Reply )
  12. PG

    Jeff Hui February 12th

    Nice tutorial! :)

    I’ll have to agree that using an file to store your login information isn’t the best implementation for user auth. I wasn’t aware of PHP’s support for xml manipulation. thanks

    ( Reply )
  13. PG

    Patternhead February 12th

    Nice tut but I wonder how this would scale for systems with thousnads of users.

    ( Reply )
  14. PG

    Tim Cooper February 12th

    Thanks for the comments!

    @Yoosuf, @Alessandro, @Jeff, @Patternhead: You’ve raised some valid questions. This solution would not be great for a large traffic site, as you said. The objective of the tutorial was to familiarize the users about the XML functions that are in PHP.

    ( Reply )
  15. PG

    mokin February 12th

    Very nice tutorial! :)

    ( Reply )
  16. PG

    Matthijn February 12th

    Didnt see the movie to be honest, but I would never use a xml file to store my users in. A database is made for things like that, much faster.

    XML in my opinion is more on its place in RSS (so generated xml files, not static) and api’s.

    ( Reply )
  17. PG

    noam February 12th

    Thumbs up for the speed! i loved the tut!!

    every time i watch a screencast i fast forward boring stuff… but in this one i didnt have to do it even once!! i think we should name those casts differently… like QUICKCAST :D

    *thumbs up*

    ( Reply )
  18. PG

    Bharat February 12th

    Excellent tutorial! :)

    ( Reply )
  19. PG

    Ryan Hickman February 12th

    Iffy Post — SimpleXML doesnt work in all environments — If someone were using 1and1 they would be screwed.

    I think this should have provided all 3 ways to access XML in php (simplexml, domDocument, xmlparser) and it would have been much more userful as a post.

    I will give a thumbs up thou… its not bad.

    ( Reply )
  20. PG

    Nat February 12th

    Great Tut Tim. I agree with you: there are instances where apps based on flat xml data sources can be a simpler, more elegant solution than using a relational db – not necessarily for registration system – but cases do exist.

    And I agree with noam: the speed was refreshingly quick.

    ( Reply )
  21. PG

    Timothy February 12th

    Nice. Thanks

    ( Reply )
  22. PG

    Rik Girbes February 12th

    thank you very much, i very much like it!!

    im making a small cms system for my school (hehe xD im about the only one in my school that knows about programming stuff)

    and this was perfect for a small group of users so thanks again!

    *thumbs up* <<== everybothy says that so why should I?!..

    bb

    Rik

    ( Reply )
  23. PG

    Jason February 12th

    To address both sides of the field here.

    No this is absolutely not a good solution for a site with anything more than a handful of users (or other data). However, XML is both smaller and faster, in certain situations, than the similar data transfer and query process of a database especially for situations where you will be accessing rarely changing data over and over again.

    Right tools for the right job.

    ( Reply )
  24. PG

    Ryan Stubbs February 12th

    That was a really good tutorial. Might not be best for the login/registration but I’d never really understood how flat file systems were made until this. Thanks a lot!

    ( Reply )
  25. PG

    rachid February 12th

    Why creating an XML file for every user instead of putting them all in one file :
    it will be very helpful if you want to check if the email does not already exist, or may be to get some user statistics !

    login
    exemple@exemple.com
    1234

    login
    exemple@exemple.com
    1234

    nice tut !!

    ( Reply )
  26. PG

    Brenelz February 12th

    Yeah… excellent screencast! Wish I was as good at them as you.

    On the other hand, I do think a seperate xml file for each is not very manageable. You could either put them all in one file:

    Or even preferably use PHP/MySQL, but I know you wanted to show XML :)

    ( Reply )
  27. PG

    Tim Cooper February 12th

    Here is a modified version that uses a single XML file:

    http://techietim.ca/files/xml_user_system_singlefile.zip

    ( Reply )
  28. PG

    fugazi February 12th

    thank you so much for this amazing tutorial :)

    ( Reply )
  29. PG

    Valuediz February 12th

    One of the most useful tutorial on Nettuts!

    ( Reply )
  30. PG

    ericb February 12th

    man this tut is awesome! good job! tim! ei, jeffrey more on xml tutorials please!

    ( Reply )
  31. PG

    Merxhan February 12th

    Great Tutorial, good explanation, very useful.

    Thank you

    ( Reply )
  32. PG

    monaye February 12th

    Great tut!!
    You didn’t mention anything about security, and wondering how secure is this?
    just like the sql injection, is there xml injection??
    I am sure there should be prevention for the session jack.

    Anywhere great tut, and good for you that you’re still high school student. when i was high school…

    ( Reply )
  33. PG

    Tim Cooper February 12th

    @monaye: You can add more security if you desire. There’s only so much I can explain in 30 minutes ;)
    As for injection, I just realised I should have ran htmlentities on the user input. Without it, the user can insert their own XML nodes and cause problems.

    So please, htmlentities user input!

    ( Reply )
  34. PG

    Rashaud Teague February 12th

    Hmm… you can also use SQLite if you don’t want to use a large platform like MySQL, Oracle, MS SQL, or PostgresSQL

    That way you don’t have to stray to far from using SQL code…

    http://sqlite.org

    ( Reply )
  35. PG

    Jorge Bucaran February 12th

    Love fast paced tutorials like this one.

    ( Reply )
  36. PG

    Constantin TOVISI February 12th

    Interesting idea to be using XML for storage of the login information, though I think I’ll stick to mySQL for the momment.

    Nice tutorial anyways, and I’m looking forward to more of this screencasts.

    ( Reply )
  37. PG

    yassir February 12th

    xml -> IO problems , unsafe !! if someone knows the username he can get the password even with the md5 !
    and you can’t do roles easly or profile …

    i ll stick to ms sql ! even with small projects

    ( Reply )
  38. PG

    dev February 12th

    tht’s the second good thing to come out of nova scotia in recent times,

    1. The lovely Ellen Page
    2. Talented Tim Cooper

    ;-)

    nice tutorial

    ( Reply )
  39. PG

    yassir February 12th

    @Tim Cooper — what if two users want to login at the same time ? or what if your file is getting large ?

    ( Reply )
  40. PG

    Ben Blogged February 12th

    Great Tut… thanks

    ( Reply )
  41. PG

    David Singer February 12th

    When will people stop using MD5. It was broken years ago. Might as well just use plain text if your going to use unsalted MD5’s.

    p.s There is a label tag.

    ( Reply )
  42. PG

    Pix February 12th

    well this is a great introduction to xml function in php
    but i think would be better check if an email already exists instead of checking the username

    by the way nice job

    ( Reply )
  43. PG

    Eneza February 12th

    nicely done!!! Good Job

    ( Reply )
  44. PG

    sx February 12th

    better password method, stored different locations.

    $user_salt=sha1(microtime());
    $salt=”slfjlfljsdlfjsl;fjlsdfj;asdfj”;
    $password=”passwordsample”;
    md5($salt. $user_salt . $password);

    ( Reply )
  45. PG

    Clemens February 12th

    I’m not enirely sure about this, but shouldn’t you use === and !== to compare strings? Otherwise, you could cheat it with 0. If Strings are compared to Ints, they are converted to the first number, or 0 instead.

    Test this:
    < ?php
    $string = “password or name or anything”;
    $string2 = “password or name or anything”;
    $int = 0;
    if($string === $int) echo “I cheated ;-) ”;
    else echo “I failed to cheat :-( ”;
    echo “”;
    if($string === $string2) echo “Strings equal”;
    else echo “Strings not equal”;
    ?>

    ( Reply )
  46. PG

    Clemens February 12th

    I want to add that this states that I failed to cheat, whereas a comparison with == instead of === would allow me to cheat.

    ( Reply )
  47. PG

    Reader February 12th

    Shouldn’t you verify the user’s password instead of username? If this was made into something with an admin area, all youd have to do is edit the cookie to say ‘admin’

    ( Reply )
  48. PG

    rizq February 13th

    Good Post !!!

    ( Reply )
  49. PG

    Arnaud LEMERCIER February 13th

    It’s a very good vidéo but two user with the same name was not possible

    ( Reply )
  50. PG

    Andres F February 13th

    Tim, that was very impressive. Thanks!

    ( Reply )
  51. PG

    Jon Lisbon February 13th

    PHP developers – stop making the world worse with your terrible language. Stop reinventing the wheel by making yet another user registration system. Stop catering to newbie developers who don’t know any better because they don’t have enough experience to know to stay well clear of PHP. Stop using MD5; it’s been shown to be cryptographically weak. Stop trying to be clever by not using a database.

    I bet the average age of the readers here is about 20 and 98% of them use Windows, and have no formal training in computer science.

    This is like some weird developer honeypot.

    ( Reply )
  52. PG

    Tim Cooper February 13th

    @Reader: The only cookie PHP sets is one to hold the session id. It alone does not hold any data that you can edit, such as the username being used.

    How ever, I did not go into how to prevent session hijacking, which is a whole lesson altogether.

    ( Reply )
  53. PG

    Marko February 13th

    Bravooooo! MORE! MORE!

    ( Reply )
  54. PG

    Yiannis February 14th

    i have problem to watch your video

    ( Reply )
  55. PG

    Jam February 15th

    Though MySQL is obviously more secure, this would work well for a user system with data of little importance. That being said, this is quite a useful tutorial. Thanks. :)

    ( Reply )
  56. PG

    Dan February 15th

    Are you testing this locally on your machine? if so, what are you using?

    when my test try to run the asXML function i see the following error:

    “SimpleXMLElement::asXML(users/new.xml) [simplexmlelement.asxml]: failed to open stream: Permission denied”

    Dan

    ( Reply )
  57. PG

    Andrew February 15th

    Could someone please tell me how to make line brakes after each user registration ?

    Atm it writes all in one line xxxxxx

    So after each new xxxx need a new line brake!

    Thanks

    ( Reply )
  58. PG

    Tim Cooper February 16th

    @Dan:

    You’ll have to chmod your users folder to something that will allow your web server access to it.

    ( Reply )
  59. PG

    iDevelopThings February 16th

    What a great video.. Keep them coming!

    ( Reply )
  60. PG

    bestamius February 17th

    What a nice article, thank’s very much!

    ( Reply )
  61. PG

    Carlos L February 17th

    @Tim Cooper:

    How do you chmod [whatever]?

    I had these errors:

    Warning: SimpleXMLElement::asXML(users/juddcarlos.xml) [simplexmlelement.asxml]: failed to open stream: Permission denied in /Library/WebServer/Documents/register.php on line 28

    Warning: Cannot modify header information – headers already sent by (output started at /Library/WebServer/Documents/register.php:28) in /Library/WebServer/Documents/register.php on line 29

    Thanks! (And thanks to the OP!)

    ( Reply )
  62. PG

    opnlnvrn February 18th

    Your Message…

    ( Reply )
  63. PG

    UGr February 19th

    please visit my site for demo
    http://www.yougroup.in/manish/site_3

    ( Reply )
  64. PG

    Brian Barnett February 21st

    I agree this wouldn’t be the best for users and passwords, but everyone please understand that in simpler terms this is a great jump for beginners into XML using PHP, someone could use this to sort their CD collection or movies. Besides, why get an entire mySQL db for something that can be done with a single file? Nice quick tutorial Tim, people should think of the other applications of this tutorial before bashing on it.

    ( Reply )
  65. PG

    Justin February 22nd

    Good job. Easy to follow and very organized source.
    Thanks for the help.

    ( Reply )
  66. PG

    Benji Barash February 23rd

    Awesome tut Tim!

    Interesting variant on the regular use of MySQL for login/register systems :)

    Thanks!

    ( Reply )
  67. PG

    Ramon February 26th

    Gonna remake it to MySQL :)

    ( Reply )
  68. PG

    ALEX18 March 21st

    HI i’m alex.
    First i want to say that ….man! y are fast and VERRY PRO.
    Now i would like to ask something.
    i add this mootools slide llogin tool in my website 2 screenshots, avablle.
    Screen Shot1 – http://img14.imageshack.us/img14/1371/screengcm.jpg

    ScreenShot 2 – http://img14.imageshack.us/img14/9411/18492524.jpg
    __________________________________
    Now how cann i make it work?….i saw this tutorial is realy good and its the first that i rally founded. but i would like to make this to work …..how?
    PLEASE if someone whos a good codeing…designer…..,please help it’s my first template …and i;m using Dreamwaver cs4.
    PLease take a moment from y;re live and help me.
    i dont what tolet this unfinished cuz i payd alots of mony on Adobe Master.
    TNX
    My emails and Messanger.
    jamaika_rnb@yahoo.com
    18alx@live.com

    THIS IS THE BEST designing and helping on the hole google zone that i found.

    ( Reply )
  69. PG

    organicIT March 29th

    Tim,

    This TUT was outstanding. I am sure you meant for this to be only demonstration for XML interactivity and not for real world production use. Heck, you could have even mentioned it but we missed it you were so fast ; )

    Anyway you have a long prosperous career ahead of you. Good Job

    ( Reply )
  70. PG

    convertman March 30th

    Your Message…

    ( Reply )
  71. PG

    Zvonko March 31st

    Great tut, but I would worry about security. Anybody could see your XML files and thus see username/passwords.

    ( Reply )
    1. PG

      David June 1st

      Hey, I had the same thought when he first did that, but, as u could see in the video he added a .htaccess file saying deny from all, and he showed it could not be seen. But I still think that it can be breached though. I’ll try to figure out a way, and try to get back on that.

      ( Reply )
  72. PG

    Harman March 31st

    How do u post it on a website?

    ( Reply )
  73. PG

    kantslowdown April 7th

    this is great…thank you!

    ( Reply )
  74. PG

    DekuLink April 17th

    Wow! Great tutorial. Lots of stuff to learn in this. Great for simple scripts like a news script where you only expect you or two people to use it.

    ( Reply )
  75. PG

    Travis April 23rd

    @Tim

    Works on my local machine, when I login and test it on a live server I receive this fatal error:

    “Fatal error: Cannot instantiate non-existent class: simplexmlelement in login.php on line 7″

    Line 7 is :

    “$xml = new SimpleXMLElement(’users/’ . $username . ‘.xml’, 0, true);”

    Thanks!

    ( Reply )
  76. PG

    Alex Mansfield May 8th

    Interesting screencast. Thanks.

    ( Reply )
  77. PG

    bhawani May 15th

    i am getting problem while registration

    Fatal error: Call to undefined method SimpleXMLElement::addChild() in E:\demoprojects\xml_user_system\register.php on line 26

    ( Reply )
  78. PG

    ahmad May 31st

    Hey ! your too fast man ! I love that !

    Bookmarked this website ! will always come here

    keep going people ! love that

    ( Reply )
  79. PG

    David June 1st

    Very Very Nice!!!

    Very well explained, I understood so much in your video, and you speed is amazing.

    ( Reply )
  80. PG

    e11world June 16th

    Tim, you are the man. PHP is the way to go and this was a very nice, fast, “and detailed” tutorial. I think everyone who’s talking about security and other things should thank him for his time and effort into this instead of trying to make this sound incomplete. He obviously could’ve done this in many better ways (including using a database) but wanted to show everyone another solution.
    Great Job Tim! Thank you

    ( Reply )
  81. PG

    AnnaMarie July 14th

    Your Message…

    ( Reply )
  82. PG

    SophiaNirsam July 27th

    Your Message…

    ( Reply )
  83. PG

    John August 3rd

    Firstly. let me say thanks.

    I just used this for a client.

    The client wants me to make it so that more fields (name, company name, telephone number) are added to the registration page and then he wants this info to be emailed to the client and to himself.

    Could you please show me how I would do this?

    Thanks again. Great website. Great tutorials.

    ( Reply )
  84. PG

    Rob August 3rd

    Ooohhhh very nice. I will definitely use this. Maybe not for user management but for user content like profiles, etc… Excellent, thank you.

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    August 3rd