Lost Password?

  #1 (permalink)  
Old 01-06-2007, 12:51 PM
Jaan's Avatar   
Jaan Jaan is offline
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 804
Last Blog:
Wadio Media Layout Com...
Rep Power: 14
Jaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the rough
Send a message via MSN to Jaan
Default Simple Register-Login-Logoff System

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

users.sql

SQL Code:
  1. CREATE TABLE `users` (
  2. `id` INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  3. `username` VARCHAR( 15 ) NOT NULL ,
  4. `password` VARCHAR( 15 ) NOT NULL ,
  5. `email` VARCHAR( 50 ) NOT NULL
  6. )

index.php

PHP Code:
<?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 Code:
<?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 Code:
<?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 Code:
<?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 To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
__________________


Cheap & Professional Web Design | Need help? Send a PM

Last edited by Jaan; 10-15-2007 at 11:59 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-06-2007, 11:27 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,736
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

very nice code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-07-2007, 07:27 AM
Jaan's Avatar   
Jaan Jaan is offline
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 804
Last Blog:
Wadio Media Layout Com...
Rep Power: 14
Jaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the rough
Send a message via MSN to Jaan
Default

thanks!
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-08-2007, 01:56 AM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-09-2007, 08:02 AM
Jaan's Avatar   
Jaan Jaan is offline
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 804
Last Blog:
Wadio Media Layout Com...
Rep Power: 14
Jaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the rough
Send a message via MSN to Jaan
Default

*Fixed one bug and i added index.php to*
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-11-2007, 02:21 PM
clookid's Avatar   
clookid clookid is offline
Programmer
 
Join Date: Jan 2007
Posts: 148
Rep Power: 7
clookid is on a distinguished road
Default

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-11-2007, 03:12 PM
Jaan's Avatar   
Jaan Jaan is offline
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 804
Last Blog:
Wadio Media Layout Com...
Rep Power: 14
Jaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the rough
Send a message via MSN to Jaan
Default

good for your friend then..
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-11-2007, 08:26 PM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

Quote:
Originally Posted by clookid View Post
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 ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-13-2007, 02:49 PM
justin justin is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 43
Rep Power: 7
justin is on a distinguished road
Default

This seems pretty good, Ive tried to make my own login system but I just can't... I dont know why. Thanks for sharing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-26-2007, 05:22 AM
-=Ghost Warrior=- -=Ghost Warrior=- is offline
Newbie
 
Join Date: Aug 2007
Posts: 2
Rep Power: 0
-=Ghost Warrior=- is on a distinguished road
Default

thanks bro for the help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Foru