Get Joomla session value in wordpress - joomla

I have a joomla Website. I have also installed a wordpress in the root folder of my joomla website. ie joomla/wordpress . I am not able to get the session value set in joomla in wordpress. How do i get the session value set in joomla in wordpress and vice versa.

Joomla has its own session handling, so the only way is to use the Joomla functions like this:
define( '_JEXEC', true );
define('JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
To get Joomla user id:
$user =& JFactory::getUser();
$user_id = $user->get('id');
and to get user session id use:
$session = & JFactory::getSession();
$session_id = $session->getId();

Related

Joomla 3.3 session and securimage captcha

I am building a form for my module and the final step is to pass a captcha check for security. I have downloaded the latest version of securimage. The captcha displays correctly and new captchas can be generated. However, after the form is posted, the advised technique on checking that the captcha was entered correctly never returns true.
index.php:
<form method="post" action="tmpl/form/contact.php" name="contactform" id="contactform">
<img id="captcha" src="securimage/securimage_show.php" alt="CAPTCHA Image" />
<a style="text-decoration: none; font-size: 10px; margin-top: -10px; " href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">Refresh</a>
/**input here **/ name="captcha_code"
contact.php:
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();
$user = JFactory::getUser();
$session = JFactory::getSession();
jimport( 'joomla.application.module.helper' );
$db = JFactory::getDBO();
$db->setQuery("SELECT params FROM #__modules WHERE module = 'module'");
$module = $db->loadObject();
$moduleParams = new JRegistry();
$moduleParams->loadString($module->params);
include_once $_SERVER['DOCUMENT_ROOT'] . 'securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
echo '<div class="error_message">' . $errorcaptchainvalid . '</div>';
exit;
}
After searching for what the problem might be, I found this possibility from http://www.phpcaptcha.org/faq/:
If the variable names are the same this may be due to a problem with
the PHP session that is used to keep track of the user and what their
code is. It is possible that the session was not started or more
likely that the session name being used in securimage_show.php differs
from the session name used in the script that does the code
validation. It is common for other software platforms (forums, content
management systems, form processors etc.) to use a session name other
than the PHP default. If this is the case, you must determine what
session name is used by the software and use the same session name in
securimage_show.php and securimage_play.php.
A non-default session name can be passed to Securimage so it can share
a session with another software platform with the following code:
$img->session_name = 'your_session_name';
This would explain why I'm having no luck with my captcha. I have tried editing the above files but I don't know how sessions are set up in Joomla! or how to get around this problem. Is the problem likely to be the session variables?
securimage_show.php:
require_once dirname(__FILE__) . '/securimage.php';
$img = new Securimage();
if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);
$img->show();
securimage_play.php
require_once dirname(__FILE__) . '/securimage.php';
$options = array(
'use_database' => true,
'database_name' => '',
'database_user' => '',
'database_driver' => Securimage::SI_DRIVER_MYSQL
);
$img = new Securimage();
if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);
$img->outputAudioFile();
Your halfway there - the problem is the way Joomla handles sessions. Instead of placing the form in an article, try embedding form through an iframe and it should work fine.

Migrate users from foomla 1.5 to joomla 2.5

in my joomla 1.5 password hash by md5+salt, they look like this:
6e3bb23b32702ee94ebc18e2a059fe4a:wxIZ6BNkAFpQk4ggeRvoOIrgbWerif0x
but on joomla 2.5 it is:
$P$DTJoqPcMQgUhl6cPrzzBOTFIFHQaNf1
HOW can i change hash algorithm in joomla 2.5?
Try this,
The older algorithm will works fine with 2.5 , only the length is changed
The New code is :
<?php
jimport('joomla.user.helper');
$password = "test";
JUserHelper::hashPassword($password);
?>
Older Version Code:
jimport('joomla.user.helper');
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password_choose, $salt);
$password = $crypt.':'.$salt;
When you are working with external php script load the Joomla library to that file,
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();
Hope it make sense..

Getting Joomla! 1.5 username when not under Joomla! (outside script)

I am using joomla 1.5.26 .
I can fetch the username and userid within the joomla file structure by
$user =& JFactory::getUser(); $user->username;.
Now I have a file which is neither under template folder nor under module folder nor under component folder. This is simply under root folder.
Now how can I fetch the logged in username?
Try this one create a new file on home page and access the username in this way
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
require_once( JPATH_BASE .'/includes/defines.php' );
require_once( JPATH_BASE .'/includes/framework.php' );
$mainframe =& JFactory::getApplication('site');
$user =& JFactory::getUser();
$user->username;
You cannot get/access any information if your file is not a type of Joomla! plugins.

Joomla 2.5 Get User Data from External Script

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).

session set in .php file for jumi - joomla

I run into a problem using session in .php file i attached in jumi
How do i set a session in in that page? when i use :
//this define and require I use from reading the other papers
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(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');
$session = &JFactory::getSession();
if(isset($_GET['id'])){
var_dump($id= $_GET['id[i]']);
} else {echo "No session ";}
// code connect to db
// render out the items
//
foreach($rows as $i=>$row){
$id[$i] = $row['rid'];
$name[$i] = $row['rname'];
$view .= '<tr>
<td>'.$id[$i].'</td>
<td>'.$name[$i].'</td>';
?>
}
<p><?php echo $view.'</tr></table>'; ?> </p>
......
It doesn't let me find the sub-page of id=1 that I clicked .
What is the better way to deal with this kind of thing?
thank you.
You have a syntax error try this before you go any further:
</php
//this define and require I use from reading the other papers
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(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');
$session = &JFactory::getSession();
if(isset($_GET['id'])){
var_dump($id= $_GET['id[i]']);
} else {
echo "No session ";
}
// code connect to db
// render out the items
//
foreach($rows as $i=>$row){
$id[$i] = $row['rid'];
$name[$i] = $row['rname'];
$view .= '<tr>
<td>'.$id[$i].'</td>
<td>'.$name[$i].'</td>';
}
?>
<p><?php echo $view.'</tr></table>'; ?></p>
Looking at the way Jumi includes PHP files, you should start with:
defined('_JEXEC') or die('Restricted access');
This will prevent the PHP file from being executed via a direct HTTP request (if you look at the example blogger file included with Jumi you will see this line). The define statements you have, initialise globals that Joomla! code uses to make sure a request has entered through the right path.
Apart from that, as mentioned by #travega you close of the PHP with ?> before you close the foreach()

Resources