We have loads of blogs and tutorials to help fellow people who are wanting to know or learn about web design, from the novice to the expert, we aim to help all!
Welcome back the the second part of the tutorial, if you have not followed the first part i suggest you do or you will not know what is going on.
In this part we will be finishing the login page. Making sure that the sessions are set and setting cookies.
First thing is first lets make sure that if there are any errors it will show, for instance if someone doesnt input a password it will tell us.
I know we have setup this within the php, but this is only storing up to now. Now we need to output any errors if there are any.
We do this by going to the loginwarning div, within it we doing this:
<?php
if(count($errors) > 0){
foreach($errors as $error){
echo $error . "<br />";
}
} else {
echo "Encrypted Area!";
}
?>
Basically this is counting the errors variable with the function count() and if the number of errors within the errors array is greater than 0 we will do a foreach loop.
Then we get the errors array and ask it to get all errors and seperate with the php loop of $errors as $error, then echo them out.
Now under the database section did in part 1 which is:
//find out if user and password are present
$query = "SELECT * FROM users WHERE username='".mysql_real_escape_string($uname)."' AND password='".mysql_real_escape_string($passencrypt)."'";
$result = mysql_query($query) OR die(mysql_error());
You will need to input this
$result_num = mysql_num_rows($result);
if($result_num > 0){
} else {
//tell there is no username etc
$errors[] = "Your username or password are incorrect";
}
Then we need to know if the username and password was correct which is done by the $result_num = mysql_num_rows function. This basically says if the username and password are correct then there is a row of a value 1 or more.
That is why the if result_num is greater than 0, the greater sign is > in php.
Else if the result num is not greater than 0 we will produce an error within the errors array again.
Now we need to start fetching some data from the database to get the id, username and firstname. We do this by doing this within the {} before the else on the result_num section as follows:
if($result_num > 0){
while($row = mysql_fetch_array($result)){
$idsess = stripslashes($row["id"]);
$firstnamesess = stripslashes($row["firstname"]);
$username = stripslashes($row["username"]);
$_SESSION["SESS_USERID"] = $idsess;
$_SESSION["SESS_USERFIRSTNAME"] = $firstnamesess;
$_SESSION["SESS_USERNAME"] = $username;
setcookie("userloggedin", $username);
setcookie("userloggedin", $username, time()+43200); // expires in 1 hour
//success lets login to page
returnheader("users-area.php");
}
} else {
//tell there is no username etc
$errors[] = "Your username or password are incorrect";
}
So the way we did this is:
We created a while loop which stated while there is a table in the database for the query already given give the id and firstname and put it into the variables idsess, firstnamesess and username.
We then input that data into sessions by desclaring the session names and inputting the vairables for them.
We also create a id session and do the same.
After that we need to setup some cookies to manage the time people are allowed to be logged in etc. We do this by setcookie function and we allocate a time.
After this we then we make a set a header function to redirect to login page.
But before this will work we need to go to the very first line and under the <? we add:
session_start();
//redirect function
function returnheader($location){
$returnheader = header("location: $location");
return $returnheader;
}
We started the session which is a must if dealing with sessions, without this the session variables will not work.
We then create a function called return headers which redirects to somewhere.
You dfeine you are creating a function by entering the word function, you then name the function which we have called it returnheader and then set an arguement of $location.
We then set a variable to header function which is a global function and put the arguement where the url would normally go.
We then return the variable.
That concludes the 2nd part to this tutorial. The next and final section will be available tomorrow.
Video will appear here once Youtube have finished processing it!
This website is a dofollow blog, spammy comments will not be accepted!