Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP mysql Login Tutorial By MAcar V1.0
#1
Welcome to my login tutorial in this tut you will learn the basics of a php login then if your good at css you can make it look nice and fancy. Thumbsup

At the bottem of this post will be a complete text example for the pages and a download
Requirements:
a site with php and mysql
in your database make table and use the following script to set it up
change it where its says table name to whatever you want

PHP Code:
CREATE TABLE `TABLE NAME` (
`
idint(65NOT NULL auto_increment,
`
usernamevarchar(65NOT NULL default '',
`
passwordvarchar(65NOT NULL default '',
PRIMARY KEY (`id`)
TYPE=MyISAM AUTO_INCREMENT=

Step One: The login prompt.

Obviously this is what your users will use to login. You will need to know html for this or just copy and paste at the end of this sectionSmile.

We start off with the form tag with the method id type and action

PHP Code:
<form id="login prompt" method="post" action="login.func.php"

the "id" can be anything you want 'method' must be post or it wont work and 'action' is what every you will name the login script file in this case its 'login.func.php'

Now we create the text feilds and submit button

PHP Code:
<?php
session_start
();.//forgot to add this...
?>
<form id="login prompt" method="post" action="login.func.php">
Username<br /><input name="loginusername" type="text" id="loginusername">
Password<br /><input name="loginpassword" type="password" id="loginpassword">
<input type="submit" value="Login" id="submit" /> 

the 'loginusername' and 'loginpassword' cant be change because they will have to match up to the login script later but if you do change them replace all the 'loginusername' and 'loginpassword' with what every you change it to.

Closed the form tag and we get this code

PHP Code:
<form id="login prompt" method="post" action="login.func.php">
Username<br /><input name="loginusername" type="text" id="loginusername">
Password<br /><input name="loginpassword" type="password" id="loginpassword">
<
input type="submit" value="Login" id="submit" />
</
form


Just insert that code into your login page and you have your self a login prompt. Now to make the "login.func.php" page. Note make sure your login page is .php not .htm or .html .

Also you need to add this code to the first line of your login page and all your pages

PHP Code:
<?php
session_start
();
?>


Step Two: The Script

This is the part where we start to code in php and a bit of mysql.

First lets start out with the session_start(); code this code keeps the information of the login going if you forget to add this you page will not work.

PHP Code:
<?php
session_start
();
?>


Now we will connect to our mysql database you need to have one for this login script most hosting sites include this

PHP Code:
<?php
session_start
();
?>
<?php
//change everything below here
$host="mysql.example.com";//your mysql host
$username="user123";..your mysql username
$password
="password123";//your mysql password
$db_name="my_database";//your mysql database name
$tbl_name="member_login"// Table name
//stop changing things!

// Connect to server and select databse.
mysql_connect("$host""$username""$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");//this code connects to the database nothing is changed here

//now we get the username and passowrd from the form if you change the login username and loginpassword part of the form then change it here to the same thing

$loginusername=$_POST['loginusername'];
$loginpassword=$_POST['loginpassword'];

// to protect from sql injection we use these
$loginusername stripslashes($loginusername);
$loginpassword stripslashes($loginpassword);
$loginusername mysql_real_escape_string($loginusername);
$loginpassword mysql_real_escape_string($loginpassword);

// and now we check the database for the username and password entered
// nothing needs to be changes
$sql="SELECT * FROM $tbl_name WHERE username='$loginusername' and password='$loginpassword'";
$result=mysql_query($sql);
//now we check for matches
$count=mysql_num_rows($result);
//if the login info is correct then the script should only find one row that has the username and password the same as what was entered

//and the following checks exactly that
if($count==1){
session_register("loginusername");
session_register("loginpassword");
header("location:index.php");//this is the page you want your login to goto so change the index.php part
}
else {
header("location:loginerror.php");//this iswhere you put the page you want to goto if their login is wrong
}
?>

Thats the end of the login script!Blackhat

Now to make sure the user logged in to view a page you musit use the following code

this code goes on the line after the

PHP Code:
<?php
session_start
();
?>



PHP Code:
<?php
if(session_is_registered(loginusername)){//once agian if you changed the loginusername on yours chage it to what you made it
?>


after that is your page codes
then all the way at the end you put


<?php
}else{
header("location:login.php");//the login.php is your login page not the script
}
?>

to logout make a <a href="link">link text</a> link and link it to a page with the code


PHP Code:
<?php
session_start
();
session_destroy();
header("location:index.php");//the index.php is where you want them to go after they logout
?>


And thats it Blackhat below are the complete codes for the login prompt and login script

login.php

PHP Code:
<?php
session_start
();
?>
<form id="login prompt" method="post" action="login.func.php">
Username<br /><input name="loginusername" type="text" id="loginusername">
Password<br /><input name="loginpassword" type="password" id="loginpassword">
<input type="submit" value="Login" id="submit" />
</form> 


login.func.php


PHP Code:
<?php
session_start
();
?>
<?php
//change everything below here
$host="mysql.example.com";//your mysql host
$username="user123";..your mysql username
$password
="password123";//your mysql password
$db_name="my_database";//your mysql database name
$tbl_name="member_login"// Table name
//stop changing things!

// Connect to server and select databse.
mysql_connect("$host""$username""$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");//this code connects to the database nothing is changed here

//now we get the username and passowrd from the form if you change the login username and loginpassword part of the form then change it here to the same thing

$loginusername=$_POST['loginusername'];
$loginpassword=$_POST['loginpassword'];

// to protect from sql injection we use these
$loginusername stripslashes($loginusername);
$loginpassword stripslashes($loginpassword);
$loginusername mysql_real_escape_string($loginusername);
$loginpassword mysql_real_escape_string($loginpassword);

// and now we check the database for the username and password entered
// nothing needs to be changes
$sql="SELECT * FROM $tbl_name WHERE username='$loginusername' and password='$loginpassword'";
$result=mysql_query($sql);
//now we check for matches
$count=mysql_num_rows($result);
//if the login info is correct then the script should only find one row that has the username and password the same as what was entered

//and the following checks exactly that
if($count==1){
session_register("loginusername");
session_register("loginpassword");
header("location:index.php");//this is the page you want your login to goto so change the index.php part
}
else {
header("location:loginerror.php");//this iswhere you put the page you want to goto if their login is wrong
}
?>


if you need any help just post dont pm me,

MAcar

Blackhat
Reply


Messages In This Thread
PHP mysql Login Tutorial By MAcar V1.0 - by MAcar - 10-07-2009, 04:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 953 07-27-2020, 11:26 PM
Last Post: tk-hassan
  [PHP] Very Basic Login Page BreShiE 17 8,000 07-11-2013, 05:57 AM
Last Post: 1n9i9c7om ツ
  [help] Improve login script Montana" 1 1,749 03-18-2013, 12:59 PM
Last Post: Haxalot
  Don't need to login Strafeness 5 2,062 01-21-2012, 08:28 AM
Last Post: AceInfinity
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,689 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 3 Guest(s)