1
    <?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/mainpage' )); //mainpage is my joomla folder.

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();

 $user =& JFactory::getUser();
  $_SESSION['username'] = $user->get('name'); 
  if ($user->get('guest') == 1) {echo"blabla";} else { echo "Hi ".$_SESSION['username']."! Welcome to our great website!";}
?>

This will result blabla in the browser, I actually want to have the 'Hi "MyName" Welcome to our great website!'. I create this file outside joomla folder.. the name of joomla folder is "mainpage". the $_SESSION['username'] is empty. I need to get current joomla username or name..

Please Help...

Thanks,

Leo

1
  • I've not used Joomla but try adding session_start(); at the top of the script (after <?php but before the define)? Commented Apr 29, 2013 at 13:12

2 Answers 2

2
<?php 

    //defined('_JEXEC') or die;
    define( '_JEXEC', 1 );
    define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' )); //mainpage is my joomla folder.

    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::getUser();
    echo 'Welcome : ' . $session->get( 'name', 'empty' ). ',';
?>
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure why you even use an external php script. You can include the code within your template (or wherever you need it) just fine.

As for why it fails: You create your very own instance of the application within your script. Outside the scope of Joomla, like if it is a separate installation. In this separate "Joomla", there is of course no user logged in, as the user logged in to your regular Joomla site.

What exactly are you trying to achieve?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.