Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Christian Lawrence
Christian Lawrence
3,941 Points

Creating User Accounts

Hello,

I have a simple website, mostly static HTML (php templates). I want to create very basic user account system, where they can login with a profile keep track of personal progress (e.g. collect accolades). There is no user to user interaction such as commenting.

Are there any frameworks or tutorials for this? I've had a Google but everything seems more complex than what I need.

2 Answers

The first question that comes to my mind is: how concerned are you about account privacy and website security?

If you are working on a website that you expect will have a few dozen users or more, then I'd really suggest looking up a guide on google on how to create a secure login system with countermeasures for bruteforce attacks, mysql injections, cross-site scripting, etc.

However, if your project is more of a testbed or just a thing to be used by you and your relatives, then you'll probably do fine with minimal security.

You should also ask yourself how valuable a successful exploit or intrusion to your system would be to a user. If you were storing credit card information, then you should maximize security as much as possible even if you only intend the site to be used by relatives. On the other hand, if all you're doing is a simple messaging system then even if you got thousands of users, it would probably be safe to say that none will bother attacking your site, even if it's easy to do so.

Now that that's said and out of the way, I'll go ahead and assume you're just looking for a simple solution with only basic security.

What you'd probably need to start with is create a database with the appropriate database tables, MySQL is definetly recommended for your type of use. If you are renting web space through a hosting company you should be able to set one up through their admin panel; if you are hosting it on your computer, you can download MySQL and run it on your computer as well. Either way, start with the database.

Once that's setup, go ahead and create a database table to store user-related data, such as username, email, password, etc. Next, create a login-page by making an html-form and have it send the data via POST to a separate php file which takes the input and checks if the username + password combination exists in the database - if it does, then create a SESSION and send the user to the next page, if not, tell them that they need to try again. Make sure that you make all pages that require login first check to see if the SESSION data is set (aka, the user is logged in), otherwise redirect them to the login page.

Finally, you can create a database table for profile data, for each user so that you can store this "personal progress" you speak of. You might be able to put this data in the user-table you created previously, but it's not recommended unless you know you will ever only have limited profile-data per user to store.

This is probably a rough description of how your system could be like. Do some googling though, and try more specific searches such as "session data php" or "login forms php" as you take on each small step, rather than trying to find the ultimate all-in-1 guide. So don't try googling something too broad such as "user account system".

Good luck!

Joe Bruno
Joe Bruno
35,909 Points

Christian,

As Christian Andersson said, security is central, especially depending on the project. If you are storing credit cards...don't, use Stripe instead. To get you started, check out this repo:

https://github.com/panique/huge/

Best of luck!