Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Simple Register-Login-Logoff System

registration login logoff registering

  • This topic is locked This topic is locked
75 replies to this topic

#1 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 06 January 2007 - 10:51 AM

In this tutorial, we will be making a register, login and logoff system in PHP.

 

First make a database and upload those tables into your database:

users.sql
 

CREATE TABLE `users` (

`id` INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`username` VARCHAR( 15 ) NOT NULL ,

`password` VARCHAR( 15 ) NOT NULL ,

`email` VARCHAR( 50 ) NOT NULL

)

index.php

<?php

//This will start a session
session_start();

$username = $_SESSION['username'];
$password = $_SESSION['password'];

//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}


?>

 

 

Now let's make a register.php file

<?php

//This function will display the registration form
function register_form(){

$date = date('D, M, Y');
echo "<form action='?act=register' method='post'>"
."Username: <input type='text' name='username' size='30'><br>"
."Password: <input type='password' name='password' size='30'><br>"
."Confirm your password: <input type='password' name='password_conf' size='30'><br>"
."Email: <input type='text' name='email' size='30'><br>"
."<input type='hidden' name='date' value='$date'>"
."<input type='submit' value='Register'>"
."</form>";

}

//This function will register users data
function register(){

//Connecting to database
$connect = mysql_connect("host", "username", "password");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("database", $connect);
if(!$select_db){
die(mysql_error());
}

//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];

//Here we will check do we have all inputs filled

if(empty($username)){
die("Please enter your username!<br>");
}

if(empty($password)){
die("Please enter your password!<br>");
}

if(empty($pass_conf)){
die("Please confirm your password!<br>");
}

if(empty($email)){
die("Please enter your email!");
}

//Let's check if this username is already in use

$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

//Now if email is already in use

$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

//Now display errors

if($do_user_check > 0){
die("Username is already in use!<br>");
}

if($do_email_check > 0){
die("Email is already in use!");
}

//Now let's check does passwords match

if($password != $pass_conf){
die("Passwords don't match!");
}


//If everything is okay let's register this user

$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}

echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";

}

switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?>

 

Now let's make a login page, login.php:

<?php
session_start();

//This displays your login form
function index(){

echo "<form action='?act=login' method='post'>"
."Username: <input type='text' name='username' size='30'><br>"
."Password: <input type='password' name='password' size='30'><br>"
."<input type='submit' value='Login'>"
."</form>";

}

//This function will find and checks if your data is correct
function login(){

//Collect your info from login form
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];


//Connecting to database
$connect = mysql_connect("host", "username", "password");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("database", $connect);
if(!$select_db){
die(mysql_error());
}

//Find if entered data is correct

$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];

$select_user = mysql_query("SELECT * FROM users WHERE id='$id'");
$row2 = mysql_fetch_array($select_user);
$user = $row2['username'];

if($username != $user){
die("Username is wrong!");
}


$pass_check = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id'");
$row3 = mysql_fetch_array($pass_check);
$email = $row3['email'];
$select_pass = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id' AND email='$email'");
$row4 = mysql_fetch_array($select_pass);
$real_password = $row4['password'];

if($password != $real_password){
die("Your password is wrong!");
}



//Now if everything is correct let's finish his/her/its login

session_register("username", $username);
session_register("password", $password);

echo "Welcome, ".$username." please continue on our <a href=index.php>Index</a>";




}

switch($act){

default;
index();
break;

case "login";
login();
break;

}
?>

 

And now.. logout.php

<?php
session_start();

//This function will destroy your session
session_destroy();
echo "You are now logged out! <a href=index.php>Index</a> or <a href=login.php>Login</a>";

?>


I hope it helped.. have fun!

Attached Files


Edited by Roger, 29 September 2014 - 04:48 PM.

  • 0

#2 John

John

    CC Mentor

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

Posted 06 January 2007 - 09:27 PM

very nice code
  • 2

#3 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 07 January 2007 - 05:27 AM

thanks!
  • 0

#4 xtraze

xtraze

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 872 posts

Posted 07 January 2007 - 11:56 PM

Really useful but I need a way to protect some pages where only logged in users can access.

And I am using Joomla and IPB, is there anyway to link both of these, I tried to find a bridge, but couldn't. Maybe using this code can solve the problem but, need more modifications, if you can help, help me on the IPB Joomla issue.
  • 0

#5 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 09 January 2007 - 06:02 AM

*Fixed one bug and i added index.php to*
  • 0

#6 clookid

clookid

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 123 posts

Posted 11 January 2007 - 12:21 PM

Simple? Lol! My friend has made one that was a lot simpler than this... Most of it was HTML though... Nevertheless, this probably works better!
  • 0

#7 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 11 January 2007 - 01:12 PM

good for your friend then..
  • 0

#8 xtraze

xtraze

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 872 posts

Posted 11 January 2007 - 06:26 PM

Simple? Lol! My friend has made one that was a lot simpler than this... Most of it was HTML though... Nevertheless, this probably works better!

If it's so good, why not share it lol.
be cool, and don't stop by giving cloo's kid.
And is it using a simple textfile to store login data ?
  • 0

#9 -=Ghost Warrior=-

-=Ghost Warrior=-

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 26 August 2007 - 02:22 AM

:D thanks bro for the help
  • 0

#10 -=Ghost Warrior=-

-=Ghost Warrior=-

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 August 2007 - 07:26 AM

thanks bro
  • 0

#11 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 06 October 2007 - 04:30 AM

you're welcome :)
  • 0

#12 robert elsdon

robert elsdon

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 11 October 2007 - 06:51 AM

Thanks!! :D
  • 0





Also tagged with one or more of these keywords: registration, login, logoff, registering