SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple login using jquery and mysql

    Hi Folks,

    Are there any examples on how to setup a very simple login form using mysql and jqeury.

    Once the details are entered and correct the rest of my site would be accessible.

    Can anyeone point me oin the right direction?

    Alot of the examples I've seen are too comlpicated for what i need.

    Many thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hello again,

    AFAIK this cannot work in JavaScript alone, as JavaScript is a client-side technology, which can simply be turned off, thus rendering your login useless.
    If I remember rightly, you're using Joomla, right?
    Isn't this functionality already built in?

    Either way, you're better off going with a server-side language like PHP:
    http://www.google.com/search?q=php+simple+login

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    On a side note I always try to move users away from AJAX logins as you need SSL to truly lock down the connection between the client and server since man in the middle attacks are easy to accomplish with forms like this
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  4. #4
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Off Topic:

    On a side note I always try to move users away from AJAX logins as you need SSL to truly lock down the connection between the client and server since man in the middle attacks are easy to accomplish with forms like this
    Thanks very much for the rely. I'd to get something setup for for testing and learning.


    How about something like this at the top of my index page:

    PHP Code:
    <?php
    if( $_SESSION['auth'] != ) {
        require( 
    'login.php' );
    break;

    }
    ?>
    Login.php

    PHP Code:

    <?php
    $name 
    $_POST['name'];
    $pass $_POST['pass'];

    if( isset(
    $name) || isset($pass) )
    {
        if( empty(
    $name) ) {
         

      var 
    fancyContent = ('<div class="header"><p>please enter a name</p></div>');
      $.
    fancybox({ contentfancyContent });



        }
        if( empty(
    $pass) ) {
      

      var 
    fancyContent = ('<div class="header"><p>please enter a password</p></div>');
      $.
    fancybox({ contentfancyContent });



        }


        if( 
    $name == "correct" && $pass == "correct" )
        {
            
    // Authentication successful - Set session
            
    session_start();
            
    $_SESSION['auth'] = 1;
            
    setcookie("username"$_POST['name'], time()+(84600*30));



      var 
    fancyContent = ('<div class="header"><p>Access granted</p></div>');
      $.
    fancybox({ contentfancyContent });



        }
        else {

      var 
    fancyContent = ('<div class="header"><p>Incorrect username or password!"</p></div>');
      $.
    fancybox({ contentfancyContent });


        }
    }


    // If no submission, display login form
    else {


    include(
    'loginform.php')
    break;

    }
    ?>

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    The code you posted looks ok and you seem to be moving in the right direction.

    I just found a simple login script on my PC.
    I didn't write it myself, rather it came from Stack Overflow (unfortunately, I cannot find the link any more).
    Anyway, this works ok, so maybe you can have a look at it and get a couple of ideas.

    You need to create this file:

    access.php

    PHP Code:
    <?php
    //put sha1() encrypted password here - example is 'hello'
    $password 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d';

    session_start();
    if (!isset(
    $_SESSION['loggedIn'])) {
        
    $_SESSION['loggedIn'] = false;
    }

    if (isset(
    $_POST['password'])) {
        if (
    sha1($_POST['password']) == $password) {
            
    $_SESSION['loggedIn'] = true;
        } else {
            die (
    'Incorrect password');
        }


    if (!
    $_SESSION['loggedIn']): ?>

    <html><head><title>Login</title></head>
      <body>
        <p>You need to login</p>
        <form method="post">
          Password: <input type="password" name="password"> <br />
          <input type="submit" name="submit" value="Login">
        </form>
      </body>
    </html>

    <?php
    exit();
    endif;
    ?>
    And on every password protected page, you should put:

    PHP Code:
    <?php
    require('access.php');
    ?>
    Hope that helps.

  6. #6
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo,

    Thats superb!

    Can I tweak it to include a username with Windows Authentication too?

    How about something like:

    PHP Code:
    // define session variables
    if (!isset($_SESSION['loggedIn'])) {
        
    $_SESSION['loggedIn'] = false;
    }

    // windows username
    if (!isset($_SERVER['REMOTE_USER'])) {
        
    $_SERVER['REMOTE_USER'] = '';


    }

    // form post username
    if (!isset($_SESSION['user'])) {
        
    $_SESSION['user'] = '';
    }




    if (isset(
    $_POST['password']) && isset($_POST['username'])) {

        
    // check username and password
        
    if (($_POST['password']) == 'test' && ($_POST['username']) != '') {
        
        
    // set session variables
            
    $_SESSION['loggedIn'] = true;

            
    // check user has windows authentication
            
    if ($_SERVER['REMOTE_USER'] != '')

                   
    $_SESSION['user'] = $_SERVER['REMOTE_USER'];

             }else{

                
    // if not populate with posted username

            
    $_SESSION['user'] = $_POST['username'];
        
             }
        
        } 
        
        
        }else {
            die (
    'Incorrect details');
        } 

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by mattastic View Post
    Can I tweak it to include a username with Windows Authentication too?
    Can you elaborate on that a little?
    How would you be authenticating the user?

  8. #8
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Can you elaborate on that a little?
    How would you be authenticating the user?
    Thanks for the reply.

    I'd like people on the network to be authenticated automatically, using the username they login to windows with.

    People who access the site anonimously I'd like to be able to enter their own username.

    I can use the login name as a Primary key in my DB that way to pull of records.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Are you using LDAP?

  10. #10
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not no.

    Would it be possible to pick up the windows login using PHP and use that?

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    If it is, Microsoft would like to have a word with you. As would every virus writer in history.

    You used to be able to get the login name... never the password.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  12. #12
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi,

    Quote Originally Posted by mattastic View Post
    Would it be possible to pick up the windows login using PHP and use that?
    as StarLion says, user name yes, password no.
    Could you not do something based on the IP range and User name?
    (Presuming you have fixed IPs)


Bookmarks

Posting Permissions

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