Now that we've got an idea of what we need, lets put words to code. First thing's first, we need to
connect to the database, because we will be adding a user to the member table. Instead of rewriting the
same code over and over for each script to connect, I just put that code in a file, and require it in each
script I need to use it, like this:
*Note: If you do not know what needs to be included in this file to make a proper connection
to the database, then view this code sample: Database Connection
We're going to be using sessions for the username, login status, etc. We will get to this later, but in
our page_header code, sessions have already been started by executing the session_start(); function. In our
login script, we'll the create session variables and assign values to them. For login status, we'll apply
that to: $_SESSION['logged_in']. The value will be zero if not logged in by default, and one if logged in.
Lets check if the visitor is logged in, if they are - then they have already registered, and do not need to
be able to visit the register page again. With that being said, we can just redirect them to the home page.
Observe code below:
Notice the open } else { at the end. This is because, if the user is not logged in, we're going to carry
on with the script. The first thing to assume is that the submit button has been pressed. Basically, going
from the end to the start, because the very end of the script will be to display the form. So, lets go
though some more code.
USERNAME
This says, if submit button was pressed to execute all code below. Then we check if any value is in
username field, if not - kill script and alert the error.
Next thing I chose to do was to declare unwanted characters, and scan the username to see if the potential
member chose any of these for their desired username. Below is the code:
$junk is an array of desired invalid characters. In $len, I check the length of the entered username.
Then, I basically strip out the invalid characters from the entered username. And finally, I check if the
length of $len (starting length) is not equal to the new length (stripping characters out if they
exist). If they're not equal, characters were stripped, and I kill the script, and inform the visitor
of the error.
Subject: "Wherever you want"
Date: Mar 01 2008 at 3:47 pm
You can put that wherever you want to determine if someone is logged in, whatever fits your design. If you do it on the body, make sure your header code defines the logged in status by setting a value to the variable.
Hello.. I dont know where all this code's go .. can you tell me where do i need to put the codes like the database conection what i need to name that file and the other code's that you are posting where they go?? please help me