Well a friend Bloody isle wanted me a post on how to do this Correctly so please take your time this will be a long Tutorial..
Spoiler for mysql:
Step 1 open phpmyadmin and create a database called phplogin:
Step 2: Make a Table called users
Set the Number of fields to three.
Then fill your fields out like this must be exactly alike.
Phew pat your self on the back the boring part is over now lets get into source code.
Now i reccomend using notepad ++ to edit php but notepad will do fine i guess.
Open up notepad and make a file called login.php This will be used to login to your database.
on the first line write
Code:
<php?
This will let your server know to start parseing php.
Now hit enter 2 times and type
Code:
session_start();
this will start a session to keep your user logged in.
now hit enter and add these two variables
this will declare the usernames coming from the login form i will show you how to make after this.
hit enter 1ce or twice and add
Code:
if ($username&&$password)
{
this is saying if the username and password are entered to run this statement under that add this
Code:
$connect = mysql_connect("localhost","user","password") or die("Couldnt Connect to the Database?");
basically that will try to connect to the database and if it cant connect it will kill the script and let you know it cannot connect. edit those fields to your mysql information than hit enter once more and add
Code:
mysql_select_db("phplogin") or die("Couldn't find Database");
This is going to select your database pretty self explanatory...
Code:
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
this is going to check the database for your information..
under that add...
Code:
$numrows = mysql_num_rows($query);
Now hit enter twice and add
Code:
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
basically it declares username and password in the database. now hit enter.
Add this code now
Code:
if ($username==$dbusername&&$password==$dbpassword)
{
this says that if the username and password entered entered match the ones in the database it will run the method under it.
So under that add:
Code:
echo "You have successfully logged in, <a href='member.php'>Click</a> here to enter the Member Page.";
$_SESSION['username']=$dbusername;
}
Basically that will start a session to keep you logged in then link you to the member page i am going to show you how to make.
Now under that add
Code:
else
echo "Incorrect Username or Password They are Case Senesetive!";
}
which is basically saying otherwise if the password is incorrect let them know..
under that add these error messages
Code:
else
die("That Username Doesnt Exist");
}
else
die("Please enter a username and Password");
and the final part of this file stop parseing the php
else
die("You must be logged in to View this Page.");
Stop Parseing Php
Code:
?>
Finally the logout.php
Code:
<?php
after calling php start a session
Code:
session_start();
Now Destroy it
Code:
session_destroy();
Echo the Logout
Code:
echo "You've been Logged out, <a href='index.php'>Click here</a> to return.";
End Parseing
Code:
?>
Now i will be adding a Register.php soon but until then follow the steps below to add a user.
Open Phpmyadmin
open your phplogin database
click the insert button near the userstable
leave the id field blank
fill in your username
fill in your password
Save.
UPDATE: Register.php will be here very soon i had an error but i just fixed it so i will be posting it soon
[Only registered and activated users can see links. ]