Show DevBest [REL][PHP] SupaScripts: Login

Status
Not open for further replies.

brsy

nah mang
May 12, 2011
1,530
272
This is a simple PHP script to login. All you need is a database connection, table called 'users' with the columns 'username', and 'password'. You also will need an HTML form. All the main PHP is coded for you already. Everything is already commented, so you can easily maneuver around and edit things as you wish.

PHP:
<?php[/FONT]
[FONT=Arial]/*****************************************[/FONT]
[FONT=Arial]* Script Name:  SupaScripts: Login[/FONT]
[FONT=Arial]* Created by:    _DucklingX[/FONT]
[FONT=Arial]* Created on:    August 3rd, 10:03 PM EST[/FONT]
[FONT=Arial]*****************************************/[/FONT]
[FONT=Arial]function clean($string) {[/FONT]
[FONT=Arial]    return mysql_real_escape_string(htmlentities($string)); # Prevents MySQL Injections #[/FONT]
[FONT=Arial]}[/FONT]
 
 
[FONT=Arial]function loginUser($username, $password) {[/FONT]
[FONT=Arial]    if(isset($_POST['username'] && isset($_POST['password']) { # If both values are set #[/FONT]
[FONT=Arial]        $username = clean($username); # Clean the param to prevent SQL injection #[/FONT]
[FONT=Arial]        $password = clean($password); # Clean the param to prevent SQL injection # [/FONT]
[FONT=Arial]        $query = mysql_query("SELECT * FROM users WHERE username = '" . $username . "'"); # Set the query #[/FONT]
[FONT=Arial]        $row = mysql_fetch_array($query); # Fetch the array from the database #[/FONT]
[FONT=Arial]        if($row['password'] != $password) { # If database password doesnt equal password typed in... #[/FONT]
[FONT=Arial]            die("The password you have entered does not match our records."); # Return an errror #[/FONT]
[FONT=Arial]        }[/FONT]
[FONT=Arial]        elseif() { # If there are no errors... #[/FONT]
[FONT=Arial]            session_start(); # Start the session #[/FONT]
[FONT=Arial]            $_SESSION['username'] = $username; # Set the username value, within the $_SESSION variable #[/FONT]
[FONT=Arial]        }[/FONT]
[FONT=Arial][FONT=Arial]    }[/FONT]
[FONT=Arial][FONT=Arial]}[/FONT]
 
[FONT=Arial]# Usage... #[/FONT]
[FONT=Arial]loginUser($_POST['username'], $_POST['password']);[/FONT]
 
[FONT=Arial]?>
[/PHP][/FONT]
 

GarettM

Posting Freak
Aug 5, 2010
833
136
PHP:
<?php
/*****************************************
* Script Name:  SupaScripts: Login
* Created by:    _DucklingX
* Created on:    August 3rd, 10:03 PM EST
*****************************************/
function clean($string) {
    return mysql_real_escape_string(htmlentities($string)); # Prevents MySQL Injections #
}
 
function loginUser($user,$pass) {
    if(!empty($user) or !empty($pass)) {
      $username = clean($user);
      $password = md5($pass);
     
      $sql_query  = mysql_query("SELECT * FROM users WHERE username='".$username." LIMIT 1");
      $sql_result = mysql_fetch_array($sql_query);
     
      if(mysql_num_rows($sql_query) > 0)
      {
        if($sql_result['password'] != $password)
        {
            print("The password you have entered does not match our records.");
        } else {
            header("Location: loggedin.php");
            $_SESSION['username'] = $username;
        }
      } 
    }
}
 
 
# How to use $
if(isset($_POST['password']))
{
    loginUser($_POST['username'], $_POST['password']);
}
// display html under php ending ?>
 
?>

Fixed His Script and this will work with uber or phoenix
 
Status
Not open for further replies.

Users who are viewing this thread

Top