Wednesday, February 8, 2012

PHP Login page!

1.     Connection to Mysql database
2.     Register page
3.     Log-In page
§  Introductions
§  HTML Form
§  PHP Code
§  Explaining the PHP
§  Editing the index.php!
§  Adding Logout code!
Log-In Page:
Introductions:
Before start reading, please notice that I'm from Finland
At this tutorial we're going to do log-in form, with php code what will check if the user exist and creates a session.
Before this tutorial, you need to do register form too! So check my other tutorial for that!
I'll start by making the HTML form!
HTML Form:
HTML Code:
<form action="login.php" method="post">
<h1>Log in</h1><br/>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="Log in!"/>
</form>
I presume that you can understand html forms well, so I won't explain code above, just check the form action
When the "Log in!" button is pressed it will go to that page, so check that its right!
PHP Code:
Create file named: login.php, form will lead us to this file!
Log In code is easier than register, we will only need to check if username and password matches from database!
Code:
<?php
include ("connect.php");

// users is table what we created at the register tutorial!

if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='".$_POST["username"]."' and password='".$_POST["password"]."'"))==1){
session_register("username"); //If matches creates session!
$_SESSION['username']=$_POST["username"];
header('Location: index.php'); //Will lead to index.php!
}
else {
   echo "Wrong username or password!";
}
?>
Explaining:
I'm not going to explain it very detailed, I presume you understand that, because of my previous tutorial "PHP Register Page"!
We're checking the username and password from our database table called users! We're using mysql_query function,which you learned at the register tutorial! And if the user and pass match php code will create session for us! As you might think, we can't see our session
So Now we need to add simple code to our index.php!
Editing the index.php:
HTML Code:
<?php
session_start(); //Starts the session.
?>
<html>
<head><title>My Register Page</title></head>
<?php
if(!isset($_SESSION['username'])) { //If you're NOT logged in: (Notice the !isset) :
?>
<!--Our register page, from PHP Register tutorial!-->
<body><form action="register.php" method="post">
<h2>Register:</h2><br /><br />
<b>User Name:</b><br />
<input type="text" name="username" /><br />
<b>Password:</b><br />
<input type="password" name="password" /><br />
<b>Re-type Password:</b><br/>
<input type="password" name="repassword" /><br />
<input type="submit" value="Register!" /></form>
<br/>
<!--Our log-in form from this tutorial:-->
<form action="login.php" method="post">
<h1>Log in</h1><br/>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="Log in!"/>
</form>
<?php
}else{
echo 'Welcome dear, ' .  $_SESSION["username"] . ' ! :)' . '<a href="logout.php">Log Me Out!</a>'; //Shows this if you ARE logged in!
}
?>
</body></html>


Go ahead and test your index.php! Cool, huh?
But now your thinking of that Log Me Out! link huh? Well luckily its easy to code:
Adding the log-out code:
Create file named logout.php! And code the code below there!
Code:
<?php
session_start();
session_destroy(); //Destroys the current session!
header('Location: index.php');
?>
And now try to hit logout button!
You are mastering the REGISTER and LOG IN codes with PHP! Congratulations!

No comments:

Post a Comment