This is tutorial is sequential of previous PHP tutorial. If you have not check that one please follow that tutorial before starting this one.
Contents of this tutorial
- Create a file.
- Full code of “loginController.php”.
- Important parts explained.
1. Create a file :
Under “chat_system” folder create another folder name “controller” as in this folder we will store server side controller scripts. Inside this folder create a file name “loginController.php”. Inside this file we will write PHP scripts which can use to control user login and registration.
File Structure :
2. Full code of “loginController.php”:
In purpose of this tutorial you can see whole code below, then I will explain each part of this code below so, you can relate how each part is connected.
You can select the whole code by double click anywhere between the code.
3. The important thing you need to know (explained):
include is used to include contents of other file PHP file to another PHP file. If you are familiar with C or C++ then you can see include as #include, both work in the same manner. In this case “config.php” file stores database configuration details which we need in this file, that’s why we are including “config.php” into “loginController.php” .
if(isset($_POST['username'])and isset($_POST['password'])) :
isset() method is used to check that parameters are set or not. isset() will return FALSE if testing a variable that has been set to NULL. Here we are checking any value is set to ‘username’ and ‘password’ or not. If no values are set than if condition will fail and the page will redirect to “login.php”.
the switch statement is a condition checking statement. It’s similar to a serious of IF statement
on the same expression. switch statement offers more clean coding when dealing which checking different values with the same variable.
Here we used a switch statement to check which type of action is requesting “login.php”. When we are submitting the form to the login page before that we are changing the action of that form.
As we can see on part 2 “action=registration”, this how we setting action in “login.php”. Now in the controller, we are searching for that action checking its value. This how two pages are connected with each other.
If registration button is clicked the case ‘registration’ will be true and registration part is performed.
$query=$db->prepare("INSERT INTO admin SET username=?,password=?"):
$db->prepare() is used to prepare MySQL statement before executing it. In the prepared statement, we are preparing an insert query on admin. If this query executed it will search for username and password values when those values are passed it will insert a record at admin table.
$query->execute() will execute the query which has been prepared by $db->prepare(). As we discussed before as we use ‘?’ in our query, it will wait for username and password to pass. That’s why when we are executing that query we are providing username and password values given by “login.php”.
If the query is executed perfectly then it will return 1. This value will be stored at $run.
If everything is ok and user registered successfully then the user will be redirected to login.php.
the session is a unique way to store information which can be used on multiple pages. To use a session we have to start a session first. session_start() checks if a session is already started and if none of started it starts one.
Here we are creating a session username which will store the value of username. When creating a session it always follow this syntax,
$_SESSION[‘session name’]= value to be stored
$query->rowCount() returns how many rows are present in the recordset. In 46 no line its used to count any rows is selected or not. If any rows are selected after query execution that means username and password are valid.
This is tutorial ends here. It’s only created to help you learn basic of PHP login process if you want to learn more about it tell me comments.
I would like to hear from you, tell me in comments below if you have any queries or feedback. Thank you.