Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

PHP Login page!

login form login

  • Please log in to reply
3 replies to this topic

#1 Epatron


    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 14 August 2011 - 04:13 AM

PHP Log-In Page!
Tutorial by: Epatron

  • Connection to Mysql database
  • Register page
  • Log-In page
  • Introductions
  • HTML Form
  • PHP Code
  • Explaining the PHP
  • Editing the index.php!
  • Adding Logout code!
Connection to Mysql db:
To create connect.php file with connection mysql database, check this
Register page:
This tutorial actually continues of my previous tutorial "PHP Register Page", if you haven't read it, check this.

Log-In Page:
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:
<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!"/>

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!
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!
header('Location: index.php'); //Will lead to index.php!
else {
echo "Wrong username or password!";

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:
session_start(); //Starts the session.
<head><title>My Register Page</title></head>
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>
<!--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!"/>
echo 'Welcome dear, ' . $_SESSION["username"] . ' ! :)' . '<a href="logout.php">Log Me Out!</a>'; //Shows this if you ARE logged in!

Posted Image
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!
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!

Any errors / questions?
Add reply below or send me a PM!
  • 0

#2 Vaielab


    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 14 August 2011 - 07:29 AM

For more security you should use

SELECT * FROM users WHERE binary username='".$_POST["username"]."' and binary password='".$_POST["password"]."'"

If you are using a collation that isn't case sensitive this will make it case sensitive.
And you should always check your data for sql injection with mysql_real_escape_string
  • 1

#3 John


    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:44 AM

Epatron, as mentioned in other threads, please keep security in mind when writing your tutorials. It is important to help people by writing tutorials, but it is even more important to teach them to be safe.
  • 0

#4 Mikeemc


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 12 January 2012 - 12:45 PM

Hey, when I try to register, i fill out the form and click register, instead of going to register page on my browser, my computer comes up saying do i want to open register.php?
  • 0

Also tagged with one or more of these keywords: login form, login

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download