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;
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.
Like I said Kittypaws, you can download it from my site: http://www.drealms.aboho.com/dloads/Login.zip