1
votes

I created a simple login form that posts data to _self and then I tried to check the credentials in order to log the user in.

The authenticating script looks like that:

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

{
      $db =& JFactory::getDBO();

      $query = 'SELECT `id`'
            . ' FROM #__users'
            . ' WHERE username='.$_POST['username'];
      $db->setQuery( $query );
      $result = $db->loadResult();


        if (!$result) {
            echo 'User does not exist';
        }


        if($result && (USERNAME & PASS MATCH))
        {
           << LOGIN AND REDIRECT TO CERTAIN PAGE >>
        }
        else
        {
            echo 'Invalid username or password';
        }   


}

In my simple php scripts, I used to set a SESSION variable to true when username and password match and thats how I considered a user to be logged in.

Here, what actions should I do? Is there a field in the database that is set when user is logged in? In other words, I want to log the user in and then redirect to another page.

Any suggestions?

1
Joomla! has a complete user management system, so I'm not clear from your question what your purpose is. Are you trying to create an extension for Joomla! or something else? - Craig

1 Answers

1
votes

Once the user is logged in, you could do something like:

    if($result && (USERNAME & PASS MATCH))
    {
       $_SESSION['logged_in_user'] = $user;
       header("location: /loggedin.php");
    }

Then at the top of loggedin.php set an authentication check:

session_start(); // If 
if (!isset($_SESSION['logged_in_user'])) {
    // User is not logged in, so send user away.
    header("Location:/login");
    die();
}
// User is logged in; private code goes here.

This is a great tutorial for user login systems: http://en.wikibooks.org/wiki/PHP_Programming/User_login_systems

It goes into advanced user authentication and shows more examples than I have here.