Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts

    simple login system

    am being really cheeky here but has anyone got a simple login system using php and mysql. thats secure and safe and that i can use it for base to a new site i want to do .

    i have found a few on the web and also linked thru here but they overly complicated .

    its for mobile phones also so must be xhtml .

    i am still learning and using an open source script at the moment. but wish to start my own and need a base to start with . thanks in advance.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    There's a few good ones here
    http://www.deathmonkeyz.com/tutorials (go to the PHP section)
    They're videos so it's explained how it works too
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Whilst I'm in a good mood, here's one I've done not long ago, all the auth checks are within another class, but hey I'll show examples of how to use.

    Please make sure that you have MYSQLI extention installed either on your local testing machine, and your hosting server!

    I've got my database details from a class containing static methods/functions which return an instance of the MySQLi object.
    PHP Code:
    <?php

        
    /**
        * LoginCheck    - Checks to see if the users details (entered from login.php)
        * are correct
        */
        
    class LoginCheck
        
    {
            private 
    $_connection;
            private 
    $_email;
            private 
    $_password;
            private static 
    $_errors = array();
            
            
            public function 
    __construct()
            {
                include_once(
    "Inc/Const/Constants.php");
                
    $this->_connection Constants::GetDatabaseObject();
            }
            public function 
    __destruct()
            {
                
    $this->_connection->close();
                unset(
    $this);
            }
            
            
            
    /**
            * CheckFields        - Checks to see if the email and password from the super
            * global $_POST are in the correct validation format we're after ^^.
            *
            * @param $txtEmail        - The users email which they've entered in the login
            * form
            * @param $txtPassword    - The users password which they've entered in from the 
            * login form.
            * @return Boolean        - True if we've got the format we're after, false if
            * there's errors!
            */
            
    public function CheckFields(&$txtEmail, &$txtPassword)
            {
                include_once(
    "Inc/Const/Constants.php");
                if(isset(
    $txtPassword{5}) && eregi(Constants::GetRegularExpression("email"), $txtEmail))
                {
                    
    $this->_email      mysqli_real_escape_string($this->_connection$txtEmail);
                    
    $this->_password mysqli_real_escape_string($this->_connection
                                        
    Constants::GetReallyFunnyHash($txtPassword));
                    return (boolean)
    true;
                }
                
                
    self::$_errors["length"] = "Invalid email or password too short!";
                return (boolean)
    false;
            }
            
            
            
    /**
            * LookForMatchingUser     - Lets browse for any matches in the database!
            *
            * @return Boolean        - false if there's no matching data, if there
            * is matching data then the user is redirected.
            */
            
    public function LookForMatchingUser()
            {
                
    $sql   "SELECT `UserId` FROM `tbl_users` 
                            WHERE `Email` = '$this->_email' 
                            AND `Password` = '$this->_password';"
    ;
                
    $query $this->_connection->query($sql);
                
                if(
    $query->num_rows 0)
                {
                    
    $sqlData $query->fetch_object();
                    
    $query->free();
                    
                    
    $_SESSION["UserId"] = $sqlData->UserId;
                    
                    
    header("Location: profile.php");
                    exit();
                }
                
                
    self::$_errors["Invalid"] = "Username or password is incorrect!";
                return (boolean)
    false;
            }
            
            
            
    /**
            * Errors        - Any errors which have occured in the class such 
            * as bad validation, wrong username/password it's outputted here.
            */
            
    public static function Errors()
            {
                
    $output "";
                foreach((array)
    self::$_errors as $errName => $errValue)
                {
                    
    #$output .= $errName . " " . $errValue . "\n";
                    
    $output .= $errValue "\n";
                }
                return (string)
    $output;
            }
            
            
            
    /**
            * IsAlreadyLoggedIn - Checks to see if they're already logged in
            * if they are why see the login form? o_O
            */
            
    public static function IsAlreadyLoggedIn(&$session)
            {
                if(isset(
    $session["UserId"]))
                {
                    
    header("Location: index.php");
                    exit();
                }
            }
        }

    ?>
    Now to call/use the class just simple look at the below:

    PHP Code:
    <?php
        
    include_once("Inc/Authentication/AuthChecker.php");
        include_once(
    "Inc/Authentication/LoginCheck.php");
        include_once(
    "Inc/Authentication/LoggedInCheck.php");
        
        
    LoginCheck::IsAlreadyLoggedIn($_SESSION);
        
        if(isset(
    $_POST["btnSubmit"]))
        {
            
    $loginCheck = &new LoginCheck();
            
            if(
    $loginCheck->CheckFields($_POST["txtEmail"], $_POST["txtPassword"]))
            {
                
    $loginCheck->LookForMatchingUser();
            }
        }
    ?>
    Please note that there's some includes there which have different authentication classes scanning about

    Feel free to try/test and use.

    Shaun
    Last edited by Shauny_B; 02-22-2010 at 04:59 PM.

  • #4
    Regular Coder
    Join Date
    Dec 2008
    Posts
    133
    Thanks
    15
    Thanked 0 Times in 0 Posts
    This would be my favourite login system.
    Its quality and has a nice support website too.

    http://www.jp77.org/
    Last edited by CallumJohnson; 02-22-2010 at 05:18 PM.

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    thanks CallumJohnson i found that the other day and found it overly complicated for what i need it for. and too all the others who have replies i have found a tutorial on youtube . heres what i have so far

    PHP Code:
    <?php

    include("config.php");

    if(isset(
    $_POST["submit"])) {

    $username=mysql_escape_string($_POST["username"]);
    $password=mysql_escape_string(sha1($_POST["password"]));

    if(!empty(
    $username)&&!empty($password)){

    //$sql = mysql_query("INSERT INTO users SET username='".$username."', pass='".$pass."', birthday='".$year.$month.$day."', sex='".$usex."', location='".$uloc."', email='".$email."',news='".$news."', signature='".$info."', regdate='".time()."', validated='".$validated."', ipadd='".getip()."', browserm='".browser()."'");




    $sql mysql_query("INSERT INTO users SET username='".$username."', user_password='".$password."', user_regdate='".time()."'");
    echo
    "you are now registered";
    }
    }else{
    echo
    "you must enter a username and password!";


    echo
    '<form action="register.php" method="post">';
    echo
    'username:<input type="text" name="username"/></br>';
    echo
    'password:<input type="password" name="password"/></br>';
    echo
    '<input type="submit" name="submit" value="register"/>';
    echo
    '</form>';
    }
    ?>
    got the sessions to add still .
    Last edited by nclemale; 02-22-2010 at 06:25 PM.

  • #6
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    still having trouble . followed script to the T .

    but this part dont show the username it just says
    welcome,to the members only page!
    where it should say welcome username to the members only page .

    PHP Code:
    <?php
    include("config.php");
    session_start();
    if (
    $_SESSION["loggedin"] == true){
    echo
    "welcome".$_SESSION["username"].",to the members only page!";
    }else{
    echo
    "you are not allowed to view this page ";
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •