small login system

By log2 on May 16, 2005

This isn't a big fancy login system, but someone asked me to write them a small loging system and when I was done, I realized that it wasn't that big so I decided to put it on here...

There are 5 files, they are: cons.php, log.php, login.php, reg.php, and register.php
And you must create two tables in your MySQL database (Those will be at the end)

NOTE: I will try to release a better version soon on this site also, just post what you want improved!

<?
// Constants page, cons.php
define("DB_SERVER", "localhost"); // Server
define("DB_USER", "username"); // Username
define("DB_PASS", "userpass"); // User pass
define("DB_NAME", "database"); // Database
mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); // Connection
mysql_select_db(DB_NAME) or die(mysql_error()); // Selection of database
?>

<?
// Register Process, register.php

// Includes the Constants
include("cons.php");

// sets all the variables needed to register
$pass = $_GET['pass']; // set $pass as the first pass
$pass2 = $_GET['pass2']; // sets $pass2 as the re-entered pass
$name = $_GET['name']; // sets $name as the name chosen

// Processing the data
if ($name) { // if $name is set
$re = mysql_num_rows(mysql_query("SELECT * FROM user WHERE user='$name'"));
  if ($re >= 1) { // If the username exists
    $error = "* Username already in use"; // Set $error
    $err = 1; // set $err as 1
  }
  else { // user doesn't exist
    if ($pass == $pass2) { // If the passwords match
      $pass = md5($pass); // Encodes the pass
      mysql_query("INSERT INTO user (user, pass) VALUES ('$name','$pass')"); // Creates the user
      $reg = "Registration successful!"; // Sets $reg as a sucess message
      header("Location: log.php?reg=$reg"); // Re-locates to the login screen
    }
    else { // The passwords don't match
      $error = "* Passwords different!"; // Sets $error
      $err = 2; // Sets $err as 2
    }
  }
}
else { // If $name isn't set
  $error = "* Enter a username"; // sets $error
  $err = 1; // sets $err as 1
}
if ($err >= 1) { // if $err is greater than or equal to 1
header("Location: reg.php?error=$error&err=$err"); // re-locates to the registration page with the error!
}
?>

<?
// Register script, reg.php

// Includes the Constants
include("cons.php");

// The registration form
$error = $_GET['error'];
$err = $_GET['err'];
?>
<form action="register.php">
<input type="text" name="name"> Username <font size="-2" color="#FF0000">
<? 
if ($err == 1) { 
echo $error;
} 
?></font><br>
<input type="password" name="pass"> Password <font size="-2" color="#FF0000">
<? 
if ($err == 2) { 
echo $error;
} 
?></font><br>
<input type="password" name="pass2"> Re-type Password<br>
<input type="submit" value="Register!">
</form>

<? 
// This is the main part, log.php

// Constants
include("cons.php");
$ip = $_SERVER['REMOTE_ADDR']; // Gets the users IP address
$brow = $_SERVER['HTTP_USER_AGENT']; // Gets the users Browser
$reg = $_GET['reg']; // Sets the $reg variable

// Makes sure the user is logged in
$r = mysql_query("SELECT * FROM active_users WHERE ip='$ip' AND browser='$brow'");
$re = mysql_fetch_array($r);
$user = $re['name']; // Sets $user as the users name

// If the user exists
if ($user) {
  echo "Welcome, ".$user."<br>"; // Welcome message
  echo "<a href=\"login.php?out=yes&user=$user\">Logout</a>"; // Logout button
  echo "<br><br><br>";

  /****************************************************/
  /***** This shows how many users are active and *****/
  /*********** it shows their names too! **************/
  /****************************************************/

  $ro = mysql_query("SELECT * FROM active_users");
  $active = mysql_num_rows($ro); // sets $active as the number of active users
  echo "Currently active users: ".$active."<br>"; // lists the number of active users

  // Actually lists the users
  while ($ra = mysql_fetch_array($ro)) {
    $name = $ra['name']; // Sets $name as the users' names

    // Lists the users names
    if ($i != ($active - 1)) { // if $i doesn't equal $active minus 1
      echo $name." | ";
    }
    else {
      echo $name;
    }

  $i++; // increase temp variable
  }
  /******************************************/
  /********** End of active users! **********/
  /******************************************/
}
else {
if ($reg) {
echo $reg;
}
/********** Show the login form ************/
?>
<form action="login.php">
<input type="text" name="name"> Username<br>
<input type="password" name="pass"> Password<br>
<input type="submit" value="Login!">
</form>
<?
/*********** End of login form ************/
}
?>

<?
// This is the login part, login.php

// includes the constants
include("cons.php");
$name = $_GET['name']; // sets the username
$pass = $_GET['pass']; // sets the password
$r = mysql_query("SELECT * FROM user WHERE user='$name'");

// logout section
$logout = $_GET['out']; // makes sure their logging out
$user = $_GET['user']; // gets the username
if ($logout == "yes") {
mysql_query("DELETE FROM active_users WHERE name='$user'"); // Logs them out
}

// Main Login part
while ($ro = mysql_fetch_array($r)) {
$pass2 = $ro['pass']; // sets $pass2 as the actual password
  if (md5($pass) == $pass2) { // makes sure password entered is the same as $pass2
    $user = $_SESSION['username'] = $name; // starts the session
    $ip = $_SERVER['REMOTE_ADDR']; // Users ip address
    $brow = $_SERVER['HTTP_USER_AGENT'];
    $time = time(); // gets the time
    // Makes the user an active user!
    mysql_query("DELETE FROM active_users WHERE name='$user'");
    mysql_query("INSERT INTO active_users (name,timestamp,ip,browser) VALUES ('$user','$time','$ip','$brow')");
  }
}

// Returns the user after logging in.
header("Location: log.php");
?>

/***************************************************/
/*************** The two mysql tables *****************/
/***************************************************/

DROP TABLE IF EXISTS user;
CREATE TABLE IF NOT EXISTS `user` (
  id int(11) NOT NULL auto_increment,
  `user` varchar(32) NOT NULL default '',
  pass varchar(32) NOT NULL default '',
  PRIMARY KEY  (id)
) ENGINE=HEAP;

DROP TABLE IF EXISTS active_users;
CREATE TABLE IF NOT EXISTS active_users (
  id int(11) NOT NULL auto_increment,
  name varchar(32) NOT NULL default '',
  `timestamp` varchar(50) NOT NULL default '',
  ip varchar(20) NOT NULL default '',
  browser varchar(150) NOT NULL default '',
  PRIMARY KEY  (id)
) ENGINE=HEAP;

Comments

Sign in to comment.
F*U*R*B*Y*   -  Sep 01, 2006

well maybe getting free webhosting. and giving us a link from there...

 Respond  
log2   -  Jul 27, 2006

Ok well I\'ve got the file made up, but where do you want me to post it.. Hawk is there a certain section?

 Respond  
F*U*R*B*Y*   -  Jun 16, 2006

well if you look in the php scripting section, i did a tutorial on a php login system, use that and adjust it to your needs, i\'m attempting the pm system now

 Respond  
Pibb   -  Jun 16, 2006

I think that we need every user that finds enough time to post on this section saying \"http://www.drealms.aboho.com/dloads/Login.zip <- This link is dead\". Cause im sure that needs more clarification.

 Respond  
Hewitt   -  Jun 16, 2006

This login dont work for me...
Can some 1 put it all in diff files like it is on there
and make it a zip and upload it to your website and send me link please

 Respond  
Hewitt   -  May 14, 2006
F*U*R*B*Y*   -  Jan 30, 2006

log2 why don\'t you just post it considering the link is dead :)

 Respond  
F*U*R*B*Y*   -  Jan 27, 2006

link is dead :(

 Respond  
Kittypaws   -  Jul 16, 2005

Link = Dead

 Respond  
log2   -  Jul 03, 2005

Like I said Kittypaws, you can download it from my site: http://www.drealms.aboho.com/dloads/Login.zip

 Respond  
Kittypaws   -  Jul 03, 2005

post it.. id like to tak a look at it if still available anonymous

 Respond  
Blade0   -  Jun 22, 2005

Yer i want it if uv got it

 Respond  
EBP   -  Jun 03, 2005

GJ

 Respond  
log2   -  May 18, 2005

ok here\'s a direct link to it: http://www.drealms.aboho.com/dloads/Login.zip

 Respond  
enexif   -  May 18, 2005

You should probably upload it to your server and just send the link... I wouldn\'t mind looking at it..

 Respond  
DavorsLounge   -  May 18, 2005

hey are you interested in helping me out with some php ? if so email me davor@davorslounge.com and ill get back to ya :)

 Respond  
log2   -  May 17, 2005

that was me by the way...

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.