Jump to content

Suggestions or improvement/optimization??


Genesis730

Recommended Posts

I have this site I just got all the error checking to work. It doesn't use a database yet but I was wondering as far what I have now, is there any simpler way to accomplish what I'm doing or does anybody have any suggestion on how to improve what I have?

 

feel free to just copy all the pages and try it yourself :P and let me know if there is anything i forgot

 

Thanks so much in advance

-Genesis730

 

*** INDEX.PHP ***

<?PHP
session_start(); // Start our session
include("display_forms.php"); 
include("functions.php");
include("errors.php");
if(isset($_POST['quickLogin'])){
    $username = $_POST['quickUser'];
    $password = $_POST['quickPass'];
    $rememberMe = $_POST['rememberMe'];
    $_SESSION['result'] = checkLogin($username, $password, $rememberMe);
    if($_SESSION['result'][0] == NULL && $_SESSION['result'][1] == NULL){
        // Login Successful! Set session variables
        $_SESSION['loggedIn'] = true;
        $_SESSION['username'] = $username;
    } else {
        // Something went wrong, display errors
    }
}


if($_SESSION['loggedIn']){
    echo "<br /><div align='right'>Welcome back ".$username;
    echo "<br /><br />[ <a href='functions.php?logout=1'>Logout</a> ]</div>";
} else {
    form_quickLogin();
}
?>

 

*** DISPLAY_FORMS.PHP ***

 


<?PHP
    function form_quickLogin() {
        $username = $_POST['quickUser'];
        $password = $_POST['quickPass'];
        $rememberMe = isset($_POST['rememberMe']) ? " checked" : "";
        echo "<form action='index.php' method='POST' >
        <table cellspacing='0' cellpadding='0' border='0' align='right'>
        <tr>
        <td align='right'>Username </td><td align='left'><input type='text' class='input' name='quickUser' value='$username' maxlength='30'></td>			
        </tr><tr>
        <td colspan='2' align='right'>";
            if ($_SESSION['result'][0] != NULL){
                $error = $_SESSION['result'][0];
                echo "<font color='#FF0000'>$error</font>";
            } else {
                echo " ";
            }
        echo "</td>
        </tr><tr>
        <td align='right'>Password </td><td align='left'><input type='password' class='input' name='quickPass' value='$password' maxlength='30'></td>
        </tr><tr>
        <td colspan='2' align='right'>";
            if ($_SESSION['result'][1] != NULL){
                $error = $_SESSION['result'][1];
                echo "<font color='#FF0000'>$error</font>";
            } else {
                echo " ";
            }
        echo "</td>
        </tr>";
        if(REMEMBER_ME) {
            echo "<tr><td colspan='2' align='right'><br />Remember Me<input type='checkbox' class='checkbox' name='rememberMe' $rememberMe><input type='hidden' name='quickLogin' value='1'>
            <input type='submit' class='submit' value='Login'></td>
            </tr><tr>
            <td colspan='2' align='right'><br />[ <a href='forgotpass.php'>Forgot Pass</a> ] - [ <a href='register.php'>Register</a> ]</td>
            </tr>
            </table></form>";
        } else {
            echo "<tr><td colspan='2' align='right'><br />
            <input type='submit' name='quickLogin' class='submit' value='Login'></td>
            </tr>
            </table></form>";
        }
    }
    unset($_SESSION['result']);
?>

 

*** CONSTANTS.PHP ***

<?PHP

define("DB_SERVER", "");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "");


define("COOKIE_EXPIRE", 60*60*24*100);  //100 days by default
define("COOKIE_PATH", "/");  //Avaible in whole domain


define("REMEMBER_ME", false);


define("REGISTER_EMAIL_2X", false);
define("REGISTER_PASSWORD_2X", true);

?>

 

*** FUNCTIONS.PHP ***

 

<?PHP
session_start();

function checkLogin($username,$password,$rememberMe){
    $username = "username~~#$*~~".sanitize($username);
    $password = "password~~#$*~~".$password;
    $rememberMe = "rememberMe~~#$*~~".$rememberMe;
    $result = errorCheck($username,$password,$rememberMe);
    return $result;
}

function sanitize($var) {
    $var = mysql_real_escape_string($var);
    return $var;
}

function desanitize($var) {
    $var = stripslashes($var);
    return $var;
}


// Logout
if($_GET['logout']== "1"){
    unset($_SESSION['loggedIn']);
    unset($_SESSION['username']);
    unset($_SESSION);
    session_destroy();
    header("Location: index.php");
}
?>

 

 

*** ERRORS.PHP ***

<?PHP
function errorCheck() {
    $error = array();
    $numargs = func_num_args();
    $arg_list = func_get_args();            
    for ($i = 0; $i < $numargs; $i++) {
        $colonPosition = strpos($arg_list[$i], '~~#$*~~') ;
        $field = substr( $arg_list[$i], 0, $colonPosition);                
        $value = substr( $arg_list[$i], $colonPosition +7);        
        if($field == "username"){
            $result[0] = checkUsername($value);
        }
        if($field == "password"){
            $result[1] = checkPassword($value);
        }
        if($field == "on"){
            // WORKAROUND field == on if "Remember Me" is set. Set cookies accordingly! 
        }
    }
    return $result;
}

function checkUsername($username) {
    $field = "username";
    if(strlen(trim($username)) == 0) { // Username not entered
        $error[username] = "Username Blank"; // Resulting error message
        return $error[username];
    } elseif(strlen(trim($username)) < 5) { // Username minimum length
        $error['username'] = "Username Too Short"; // Resulting error message     
        return $error[username];
    } elseif(strlen(trim($username)) > 32) { // Username maximum length
        $error['username'] = "Username Too Long"; // Resulting error message
        return $error[username];
    }
}

function checkPassword($password) {
    $field = "password";
    if(strlen(trim($password)) == 0) { // Password not entered
        $error[password] = "Password Blank"; // Resulting error message
        return $error[password];
    } elseif(strlen(trim($password)) < 5) { // Password minimum length
        $error['password'] = "Password Too Short"; // Resulting error message
        return $error[password];
    } elseif(strlen(trim($password)) > 32) { // Password maximum length
        $error['password'] = "Password Too Long"; // Resulting error message
        return $error[password];
    }
}

function checkEmail($email) {
    $field = "emailaddress";
    
}

?>

Link to comment
https://forums.phpfreaks.com/topic/240843-suggestions-or-improvementoptimization/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.