Home
Joomla login session

Joomla is a great and the very powerful CMS software currently available on the market. It is open source software which means you can use it however you like. Joomla offer a great flexibility that you could do almost anything with it, ranging from online shopping cart to professional website. Because it is so powerful many people are now using it and sometimes they want to customize their login. 

In this article I will show you how to create you own login in Joomla instead of using the standard Jooma login. Joomla manage their own session variables so you must learn how use Joomla session and not the standard PHP $_SESSION. 

There are two files in the tutorial, one file is the login form where user will use this form to enter in the credential detail and the other one is to process the submission.

 You will also need to learn how to use Joomla session to store user’s login details in the session variable so that it can be used site wide while the user is browsing your site.

 In order to use Joomla classes you must first initialize Joomla framework.

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

Once the frame work is initialised you can start using Joomla session

Here is how to get session variable:

$session =& JFactory::getSession();
$myUser = $session->get( 'myUser', 'empty' );

And here is how to set session variable:

$session =& JFactory::getSession();
$myUser = $session->get( 'myUser', 'empty' );


LOGIN FORM

<?
php 

define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root

define( 'DS', DIRECTORY_SEPARATOR );

 

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );

require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

 

$mainframe =& JFactory::getApplication('site');

$mainframe->initialise();

 

?>

<html>

    <body>

 

    <?php

 

        $session =& JFactory::getSession();

        $myUser = $session->get( 'myUser', 'empty' );

 

        if($myUser != 'admin')

        {   

    ?>

 

    <H1>ITJungles</H1>

    <form name="login" method="post" action="loginChk.php">

        <table>

            <tr>

                <td>Username</td><td><input type="text" name="txtUserid" id="txtUserid" size="14"></td>

            </tr>

            <tr>               <td>Password</td><td><input type="password" name="txtPassword" id="txtPassword" size="14"></td>

            </tr>

            <tr>

                <td colspan="2" align="right"><input type="submit" name="btnSubmit" text="Submit" value="Submit"/></td>

            </tr>

        </table>

    </form>

    <?php

        }

        else

        {

            echo 'You have logged in';

        }

    ?>

    </body>

</html>

 

 

LOGINCHK.PHP FILE
 

<?php

 

define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root

define( 'DS', DIRECTORY_SEPARATOR );

 

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );

require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

 

$mainframe =& JFactory::getApplication('site');

$mainframe->initialise();

 

$session =& JFactory::getSession();

$myUser = $session->get( 'myUser', 'empty' );

 

if($myUser != 'test_user')

{

 

    if( !empty($_POST["txtUserid"]) && !empty($_POST["txtPassword"]))

    {

        $username = $_POST["txtUserid"];

        $password = $_POST["txtPassword"];

 

        $_host="localhost";

        $_user="your_user";

        $_pwd="your_password";

        $_db="your_database";

        $handle = mysql_connect($_host,$_user,$_pwd);

 

        if ($handle) { $errorCode=""; }

        else { $errorCode="Can't Open Database ".$_db." ]";}

 

        mysql_select_db($_db);       

        $strQuery = "SELECT * FROM iammode WHERE username='". $username ."' AND password='" . MD5($password) . "'";

 

 

        $intQID = mysql_db_query($_db, $strQuery);

        $QueryResult = mysql_fetch_assoc($intQID);

        $currUserID = $QueryResult["id"];

        $currUserName = $QueryResult["username"];

        $currPassword = $QueryResult["password"];  

        mysql_close($handle);       

 

        //echo $currUserName . ' >>> ' . '' . '    test here. >>' . md5($password);

 

        if( $currUserID == 1)

        {

            $session->set( 'myUser', $currUserName );

 

            echo "Logged in successful as " . $currUserName . ".";           

 

        }

        else

        {

            echo "Logged in failed.";

        }               

 

        echo '<br/><br/> <h2><a href="http://www.iammode.com">Go to IAMMODE.COM</a></h2>';

    }

}

else

{

    echo 'logged in.';

}

 

?>



Comments (4)
  • John  - Joomla login session
    Hi

    Please can send me example of joomla login session for joomla 1.0

    Thanks

    John
  • parag  - Joomla
    Dear sir.
    i m trying to understand coding of joomla. I m new in Joomla and also php here i saw index.php and there a lot of new concept.
    I m trying to understand them but didn't got the anything.
    So please refer me something by which i can learn internal coding of Joomla1.5

    thank you very much
  • fsds  - you sample is crap go bobo
    :angry-red:
  • Anonymous
    How to access Joomla 1.6 session in another script?
    This method doesn't work in Joomla 1.6
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

"