try to get session data from a joomla site with this code it work once but after logong out and in again get no more data.
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
print_r($SESSION);
$user = JFactory::getUser();
echo $user;
$username=$user->get('username'); // username text same as database table
$userId = $user->get( 'id' ); //id text same as database table
$useremail=$user->get('email'); // email text same as database table
echo "UserID is: ".$userId;
echo '</br>';
echo "UserName is :".$username;
echo '</br>';
echo "User Email is :".$useremail;
the output is
JUserUserID is: 0
UserName is :
User Email is :
it worked for 1 session after that nothing
Related
I have a script that checks if user is (super)admin and then grants access to that page.
It worked for couple years but now it stopped working. I'm getting a incorrect response text error message. When I comment out the script the thing works fine.
Here is the code:
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
$user = JFactory::getUser();
$userGroups = $user->getAuthorisedGroups();
if(in_array(7,$userGroups) or in_array(8,$userGroups)){
}else{
header( 'Location: /beheer-niet-toegestaan.html' ) ;
}
?>
I'm at a loss. Hope someone can help me.
// EDIT //
Hmm sorry for the incomplete question. After over an hour trying various code samples online i solved it by editing it to:
<?php
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
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();
$isAdmin = $user->get('isRoot');
if ($isAdmin == false) {
header( 'Location: /beheer-niet-toegestaan.html' ) ;
}
?>
I would like to get the Joomla user Id from the user name, I will be using this in an external script.
if you don't want to get it via Joomla, just use a SQL statement like this:
SELECT id FROM #__users WHERE username = 'someusername';
Or load Joomla and use something like this code:
if (!defined('_JEXEC')) {
define( '_JEXEC', 1 );
define ('JPATH_BASE', "c:\\wamp\\www\\oursite");
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
require_once ( JPATH_LIBRARIES.DS.'joomla'.DS.'import.php'); // Joomla library imports.
$app=& JFactory::getApplication('site');
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id');
$query->from($db->quoteName('#__users'));
$query->where($db->quoteName('username')." = ".$db->quote('someusername'));
$db->setQuery($query);
$user_id = $db->loadResult();
// Get a JUser object, if you want/need it
$user = JFactory::getUser($user_id);
I try to throw $_SESSION["avalue"] from request.php file to response.php file like bellow:
request.php:
<?php
$_SESSION["across"] = "across Session";
?>
<script type="text/javascript">
function requestExample(){
xmlhttp.open("POST", "../response.php?Name=Example", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>
...
.......
Response.php:
<?php
if(isset($_GET["Name"]))
{
$name = $_GET["Name"];
switch($name){
case "Example":
echo $_SESSION["across"];
break;
......
}
}
?>
I get error : Undefined index: across in .............../response.php
I got it by using code bellow. [Source http://docs.joomla.org/How_to_access_session_variables_set_by_an_external_script]
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__) );
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();
$user = $session->get( 'across' );
Thanks!
I need to get the information of the user that's currently logged in to Joomla from a program outside of Joomla itself. I upgraded from 1.5 to 2.5, and what I had before doesn't work anymore.
<?php
define( '_VALID_MOS', 1 );
include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );
$option="test";
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();
$my = $mainframe->getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
After some research, I've come up with this:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$my =& JFactory::getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
$joomla_username = $my->username;
It doesn't produce any errors but seems to work. However, the user object is empty. This script is in the same directory as the Joomla installation. What may be the problem? Thanks!
Sources:
http://www.cmsbloke.com/accessing-joomla-objects-from-an-external-script/
Taken from
http://docs.joomla.org/How_to_access_session_variables_set_by_an_external_script
Solution: Replace session_start(); in your external script with
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
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();
Be sure to change JPATH_BASE to suit your directory structure.
Replace the $_SESSION[ 'name' ] = "value"; in your external script with
$session =& JFactory::getSession();
$session->set('name', "value");
Now you can retrieve this session variable using:
$session =& JFactory::getSession();
echo $session->get('name');
It turns out, I had two problems:
My script was on www.example.com, while I was logged in under example.com, so the different domain was throwing off the session data.
I also wasn't initialising the application, which apparently is necessary.
Here are the results:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
$joomla_name = $user->name;
$joomla_email = $user->email;
$joomla_password = $user->password;
$joomla_username = $user->username;
I think people have had problems with this in Joomla 2.5 as things have changed. I haven't found a solution for the way you have requested but wrote this work around (excluding the password field though) that you may want to use.
define a connection to the database:
<?php
$host="localhost";
$username="DB_USERNAME";
$password="DB_PASSWORD";
$db_name="DB_NAME";
mysql_connect($host,$username,$password)or die(mysql_error());
mysql_select_db($db_name)or die(mysql_error());
?>
Echo create a query and echo the results in a table:
<table style="background:#FFF; color:#000;">
<?php
$query = "SELECT username,name,email FROM j25_users ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<tr><td>',$row["username"],'</td><td>',$row["name"],'</td><td>',$row["email"],'</td></tr>' ;
}
?>
</table>
You will need to change j25_users to the correct prefix of your database tables.
Update: Much better to import the framework to get the user object.
Hope this comes in handy for you. Regards.
If you want to skip using Joomla! functions, you can query the Joomla! database. Join tables js25_session and js25_users with the user id. This way, you can get every currently logged-in user and all guests on the website (and possibly the number of online users).
I have made a custom 'Account Creation' script so that users can login from my phone application.
What I want is to be able to change the responses from the server depending on their locale. So when I request a page I would add lang=en or lang=zh etc.
This works
http://mysite.com/phone/my_custom_account_creation.php?lang=en
Response:
<resource classification="error" code="Error (Code: 500)">
<message>Please enter your name:</message>
</resource>
This does not work:
http://mysite.com/phone/my_custom_account_creation.php?lang=zh
Response:
<resource classification="error" code="Error (Code: 500)">
<message>Please enter your name:</message>
</resource>
If I go into Joomla at set the default language to chinese, it works.
<resource classification="error" code="Error (Code: 500)">
<message>请输入您的姓名。</message>
</resource>
but
http://mysite.com/phone/my_custom_account_creation.php?lang=en
does not work, instead it continues to show the chinese version.
What might I be able to do here?
here is the code I am using for registration:
<?php
header ("content-type: text/xml");
/*
* Register a user from a phone interface using JAVA
*/
define( '_JEXEC', 1 );
//define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
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' );
//My functions
require_once('../shared_functions.php');
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
//Don't use tokens for registration
//JRequest::checkToken() or jexit( 'Invalid Token' );
$user = clone(JFactory::getUser());
$pathway = & $mainframe->getPathway();
$config = & JFactory::getConfig();
$authorize = & JFactory::getACL();
$document = & JFactory::getDocument();
$usersConfig = &JComponentHelper::getParams( 'com_users' );
if ($usersConfig->get('allowUserRegistration') == '0')
{
xmlError("Access Forbidden (Code 403)","Registration has been temporarily disabled");
//JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return;
}
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype)
{
$newUsertype = 'Registered';
}
if (!$user->bind( JRequest::get('post'), 'usertype' ))
{
xmlError("Error (Code: 500)",$user->getError());
return;
//JError::raiseError( 500, $user->getError());
}
$user->set('id', 0);
$user->set('usertype', '');
$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());
$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
{
jimport('joomla.user.helper');
$user->set('activation', md5( JUserHelper::genRandomPassword()) );
$user->set('block', '1');
}
if (!$user->save()) { // if the user is NOT saved...
xmlError("Error (Code: 500)",$user->getError());
//JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
//return false; // if you're in a method/function return false
}
xmlMessage("ok",CODE_ACCOUNT_CREATED,"Success");
?>
I got it working
$lang =& JFactory::getLanguage();
$lang->setLanguage( $_GET['lang'] );
$lang->load();
I found that the language had to be in full format though en-GB or zh-CN