Jump to content

Simple Login and Register Script


bcooperz

Recommended Posts

Hey I would just like to release a simple login/register script that will work just fine and has some nice systems in it.

 

The Login. (I will post the code then below tell you what you need to do to get it to work with MYSQL DATABASE)

Create a file and call it login with the suffix .php so if you have file extensions showing on your computer it will look like "login.php" then put this code inside of it.

<?php session_start(); ?>
<?php
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string"); // i.e PHP >= v4.3.0
if($new_enough_php){ // PHP v4.3.0 or higher
if ($magic_quotes_active){ $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
}else{ //Before PHP v4.3.0
//if magic quotes aren't already on then add slahes manually
if(!$magic_quotes_active){ $value = addslashes($value); }
// if magic quotes are active then the slashes already exist
}
return $value;
}

function redirect_to($location = NULL){
	if($location != NULL){
	header("Location: {$location}");
	exit;
}
}
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","yourpassword");
define("DB_NAME","yourdatabasename");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$connection){
die("Database Connection Failed: " . mysql_error());
}
$db_select = mysql_select_db("bcooperz", $connection);
if(!$db_select){
die("Connection to database failed: " . mysql_error());
}
?>
<?php
if(isset($_SESSION['user_id'])){
redirect_to("staff.php");
}
?>
<?php
if (isset($_POST['submit'])){
$errors = array();

// Perform validations on the form
$required_fields = array('username', 'password');
foreach($required_fields as $fieldname){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){
$errors[] = $fieldname;
}
}

$field_with_lengths = array('username' => 30, 'password' => 30);
foreach($field_with_lengths as $fieldname => $maxlength) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname; }
}

$username = trim(mysql_prep($_POST['username']));
$password = trim(mysql_prep($_POST['password']));
$hashed_password = sha1($password);

if (empty($errors)){
// Checks database to see if username and password exist their
$query = "SELECT id, username FROM users WHERE username='$username' AND hashed_password='$hashed_password' LIMIT 1";
$result_set = mysql_query($query, $connection);
if(!$result_set){
die("Database Query Failed: " . mysql_error());
}
if (mysql_num_rows($result_set) == 1) {
// The Username and Password have been found in the database and the user is verified
// Only 1 Match
$found_user = mysql_fetch_array($result_set);
$_SESSION['user_id'] = $found_user['id'];
$_SESSION['username'] = $found_user['username'];
redirect_to("staff.php");
}else{
// Username and Password was not found in the database.
$message = "Username/Password Combination Incorrect.<br/>Please make sure your caps lock key is off and try again.";
echo $message;
}
}else{
$count = count($errors);
if($count == 1){
echo "Their Was {$count} Error In The Form" . "<br />";
print_r(implode(", ", $errors));
}else{
echo "Their Was {$count} Error's In The Form" . "<br />";
echo "<b>";
print_r(implode(", ", $errors));
echo "</b>";
}
}
}else{
// The Form Has Not Been Submitted
if(isset($_GET['logout']) && $_GET['logout'] == 1){
echo "You Are Now Logged Out";
}
if(isset($_GET['nowlogged']) && $_GET['nowlogged'] == 1){
echo "You Need to Login to reach this page.";
}
$username = "";
$password = "";
}
?>
<html>
<head>
<title>Register</title>
</head>
<body>

<form action="login.php" method="post">
Username : <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /><br />
Password : <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /><br /><br />
<input type="submit" name="submit" value="Login" /><br />
</form>
<p>Haven't got an account? register <a href="register.php">here!</a></p>
</body>
</html>

Now once you have a file called "login.php" with the above code inside of it you will need to goto your mysql database and create a database with a table that has 3 fields in the following format.

- id - int(11) - Auto increment

- username - varchar(50)

- hashed_password - varchar(40)

 

Now search for this in the login.php code

define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","yourpassword");
define("DB_NAME","yourdatabasename");

And This:

$db_select = mysql_select_db("bcooperz", $connection);

And change these to your settings.

 

Once you have done all this create a new file called register with the suffix .php as well so if you have file extensions turned on it will look like "register.php"

And add this code inside it:

<?php
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string"); // i.e PHP >= v4.3.0
if($new_enough_php){ // PHP v4.3.0 or higher
if ($magic_quotes_active){ $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
}else{ //Before PHP v4.3.0
//if magic quotes aren't already on then add slahes manually
if(!$magic_quotes_active){ $value = addslashes($value); }
// if magic quotes are active then the slashes already exist
}
return $value;
}

function redirect_to($location = NULL){
	if($location != NULL){
	header("Location: {$location}");
	exit;
}
}
?>
<?php
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","maxcooper");
define("DB_NAME","bcooperz");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$connection){
die("Database Connection Failed: " . mysql_error());
}
$db_select = mysql_select_db("bcooperz", $connection);
if(!$db_select){
die("Connection to database failed: " . mysql_error());
}
?>

<?php
if(isset($_POST['submit'])){

$username = trim(mysql_prep($_POST['username']));
$password = trim(mysql_prep($_POST['password']));
$hashed_password = sha1($password);
$confirmpass=$_POST['confirmpass'];
$query2 = "SELECT * FROM users WHERE username='$username'";
$result2 = mysql_query($query2);
$counted=mysql_num_rows($result2);

$errors = array();

// Perform validations on the form
$required_fields = array('username', 'password', 'confirmpass');
foreach($required_fields as $fieldname){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){
$errors[] = $fieldname;
}
}
if($confirmpass!=$_POST['password']){
$errors[] = "passdifference";
}
if($counted > 0){
$errors[] = "User Already Created";
}

$field_with_lengths = array('username' => 30, 'password' => 30);
foreach($field_with_lengths as $fieldname => $maxlength) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname; }
}


/* The Form Has Been Submitted */
if (empty($errors)){
$query = "INSERT INTO users (username,hashed_password) VALUES ('{$username}', '{$hashed_password}')";
$result = mysql_query($query, $connection);
if($result){
echo "User Successfully Created";
}else{
echo "The User Could Not Be Created" . "<br />";
echo mysql_error();
}
}else{
$count = count($errors);
if($count == 1){
echo "Their Was {$count} Error In The Form" . "<br />";
print_r(implode(", ", $errors));
}else{
echo "Their Was {$count} Error's In The Form" . "<br />";
echo "<b>";
print_r(implode(", ", $errors));
echo "</b>";
}
}
}else{
/* The Form Has Not Yet Been Submitted */
$username = "";
$password = "";
}
?>
<html>
<head>
<title>Register</title>
</head>
<body>

<form action="register.php" method="post">
Username : <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /><br />
Password : <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /><br />
Confirm Password: <input type="password" name="confirmpass" maxlength="30" value="" /><br /><br />
<input type="submit" name="submit" value="Register" /><br />
</form>
<p>Already have a account? login here <a href="login.php">here!</a></p>
</body>
</html>

Once you have done that and you have a file called "register.php" you will need to perform the final step which will be changing the database details once again on the second file ("register.php").

 

Thanks, Bcooperz. Please tell me if this works :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.