I am working on a website which I would like to require users to have an account for. If they need an account, users go to the registration page, where they put in their name, username, and password. That data is then put into a mysql database, and the user can then use their username and password to login on the login page. Without being logged in, the user is not able to enter the main section of the site.
The idea is to be able to have the user register a username, password and full name, and then for them to be able to login using the username and password.
I would like to use the same interface as the pages I linked to above for it.
I would really appreciate some help with this.
Edit: Thanks for the help! It is working great
For this example I'm going to leave out prepared statements, but you'll need to do some research on SQL-injection prevention.
First you need a form for the user to use to login. Here is a basic one that will be on a page called NewUser.html:
You can of course add other fields such as email address, etc- but I'm keeping it simple.
Now let's go to the AddUser.php page:
So the user has now been created, password has been hashed with a salt and inserted into DB... seriously don't forget SQL-injection.
Now you'll have a form that is very similar to the NewUser.html form for logging in, but it won't require the password to be entered twice. Let's say that login form sends the user to a page called login.php:
Just a tip, if you want to add access levels you can store a place in the database with an access number (ex: 1, 2, 3) and then upon successfully logging in you would assign another $_SESSION that represents their access level and gives them access to certain sections you allow.
Now when they navigate to other pages on your site their session will be verified like this:
ExamplePage.php
Just get in the habit of starting a session on every page where access is only allowed by those who are logged in. Sessions are remembered from page to page.
Don't forget to give them a logout page which will destroy the session: logout.php
Create files with each of the following names (all .php files). You know that once you start a session you use the
session_start()
before the<!DOCTYPE html>
. You should put the following line of code before each php document in your website:See the bottom of my answer for the contents of 'session-renewal.php'.
In your MySQL table (in my examples I call the table
users
) you want five slots for your users. I have copied this code from my script where I user emails for the users instead of usernames, but you can swap everything out. Yourid
column should auto_increment.register-form.php:
register-user.php:
login-form.php:
login-user.php:
database-function.php:
session-renewal.php:
Hopefully that helps. Let me know if you have any other questions. I hope I didn't leave any of my personal settings in there...!