This tutorial goes over the concept of a log in system with MySQL with sessions. (The sources are provided after the jump)
One of my viewers sent me a message on youtube requesting this great example
Dear kloplop321,
I really want to make a php, mysql based game. I have an idea about how I am going to do it. I would just really love some help with the SESSION login. There are tutorials on the internet but I find that they don’t show the full code or stuff like that. So if you would please create a tutorial on it I would apprecitate it. Thanks.
This tutorial naturally has the sources provided.
The following videos(2) go over the concept of a login system in PHP while using sessions(to maintain the login status) and MySQL(a database that holds the information). I first go over how to make
- the login page
- the table in the database
- a user through PHPMyAdmin [the next tutorial will likely go over registering users]
- code how to validate that user and if they are validated
- if so, set the session information
- if acceptable, they can go to a “members only” page [which only allows identified people in]
- a way to log out
Again: this tutorial is pretty much a walk-through in concept of a mysql login system.
Part 2 (the most important) and the sources after the jump.
And now the sources.
include.php
<?php
session_start();//start the session
//now to do the MySQL connection.
$link = mysql_connect('localhost', 'phpuser', 'phppass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//WE NEED to select the database!
mysql_select_db("tutorials_login");
?>
index.php
<?php
include("include.php");
//see if the person is trying to log in now..
if(isset($_REQUEST['sub'])){
$username = trim($_REQUEST['username']);
$password = trim($_REQUEST['password']);
//make it safe to see if in the table
$username = mysql_real_escape_string($username);
$password = md5($password);
//time to query
$sql = "SELECT * FROM `users` WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql);
$exists = false;
$userid = -1;
while($row = mysql_fetch_array($result)){
$exists = true;
$userid = (int)$row[0];
break;//get out of the while loop
}
if($exists){
$_SESSION['isin'] = 1;
$_SESSION['userid'] = $userid;
}else{
echo "You got something wrong, try again<br />\n";
//echo $sql;
}
}//chopsuey
//detect if logged in via variable in the session
if($_SESSION['isin'] == 1){
//we are logged in.
echo "Hey there, you seem to be logged in,
Would you like to go to the <a href=\"membersonly.php\">Members Only</a> area?<br /> Or would you want to
<a href=\"logout.php\">log out</a>?";
}else{
//show the login prompt
?>
<form action="?" method="post">
Username <input type="text" name="username" /><br />
Password <input type="password" name="password" /> <br />
<input type="submit" value="Log in" />
<input type="hidden" name="sub" value="1" />
</form>
<?php
}
?>
logout.php
<?php
session_start();
session_destroy();
header("location: index.php");
?>
membersonly.php
<?php
include("include.php");
if($_SESSION['isin'] == 1){
echo "Hey there ";
$sql = "SELECT * FROM users WHERE id = ".$_SESSION['userid'];
$result = mysql_query($sql);
$info = null;
while($row = mysql_fetch_array($result)){
$info = $row;
}
echo $info['username'];//hey there pickles
echo " Would you like to <a href=\"index.php\">go back</a>? ";
}else{
//you don't belong here, kick back to the index
header("location: index.php");
}
?>
Also a tid bit of information: I did this in Linux, and I recorded it in 1080p HD!
March 15th, 2010 at 7:55 pm
Very nice tutorial!
April 27th, 2010 at 3:37 pm
I’m currently still in the making of a website.
When will you post the registering part?
Out of everyones, your tutorials are the only ones that work and I WOULD LOVE to see the registering part.
Your tutorials are so easier to understand and very organized.
And I think the login system is the hardest part because it’s what your whole site is about..
Please email me back and let me know if you are going to make a tutorial for the registering part (:
I sure hope you are still giving out tutorials. Thanks so much<3
May 28th, 2010 at 12:21 pm
This tutorial was extremely distracting and ultimately useless. I’m sure the information is good enough, but your windows are transparent, your background changes every minute, and the camera follows the mouse around while we’re attempting to read your code… this was completely unwatchable.
May 28th, 2010 at 12:33 pm
I am sorry about that. I learned from that, thanks to commentators like you from youtube. I have been trying to minimize all distractions like that as I have been working on my future tutorials.
June 3rd, 2010 at 8:36 am
For some reason I keep getting an Error…
Notice: Undefined index: isin in C:\wamp\www\neighborhoodbusinessguide\index.php on line 30
June 3rd, 2010 at 8:51 am
It seems to be when I log out or are not logged in.
June 3rd, 2010 at 9:30 am
This usually happens when you have a variable in the SESSION that doesn’t exist right now
So like $_SESSION['username'], the username variable obviously will not be available when the user is not logged in. So, in order to address this issue,
you do
if(isset($_SESSION['username'])){
//do code here
}
this checks to see if the variable exists(is set) then you are safe to access it. This is only a notice, so it doesn’t matter much in the long run.
The reason why you are seeing notices is because in your php.ini (php settings file) the error reporting is most likely set to E_ALL, you can change it to E_ERROR and just have a filter so you don’t see the notices. I have the error reporting so it doesn’t show these notices.
As you can see by my tutorial, I am only seeing if the session variable, isin, is set to 1.
if($_SESSION['isin'] == 1){
Now, if the variable does not exist(undefined index, meaning isin doesn’t exist), then it will return false for that variable.
The computer now sees it as
if(false == 1)
logically false is 0, and 0 is not 1, so it does not execute the code that should happen when ‘isin’ is set to true or 1.
Does that help your understanding?
June 3rd, 2010 at 9:46 am
Yes it clear it up thanks
July 3rd, 2010 at 10:17 pm
[...] future tools for the blog administration that will be shown in future tutorials. You may refer to Tutorial 8(Login Concept) as we go though this [...]