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.
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
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 section.
We start off with the form tag with the method id type and action
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
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
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
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.
Now we will connect to our mysql database you need to have one for this login script most hosting sites include this
Thats the end of the login script!
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
to logout make a <a href="link">link text</a> link and link it to a page with the code
And thats it below are the complete codes for the login prompt and login script
login.php
login.func.php
if you need any help just post dont pm me,
MAcar
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` (
`id` int(65) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
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 section.
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!
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 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