I've produced a custom PHP script which uploads information posted with a form to some MySQL database. I must place the consumer id from Joomla from the user who posted the MySQL data.

To get this done I've read that Joomla creates a session cookie while a person is drenched into joomla. My hope would be to have the ability to identify the matching snacks after which copy the joomla user id increase the worthiness in to the custom mysql database.

Here's what I've to date. It's not functioning correctly, however it should provide you with a concept of the method of my problem.

<?php
session_start();

//makes connection to databases
include_once "mysql-connect.php";       
if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];   //I save the id in a session variable   
saveUserData($id);
// activeProfile is the name of the cookie used by Joomla/Jomsocial
} elseif (isset($_COOKIE['activeProfile'])) { 
$id = $_COOKIE['activeProfile'];
saveUserData($id);
} else {
$return_msg = "not_logged_in";
}

function saveUserData($id) {
// this is where I save the id in a session variable
$_SESSION['id'] = $id;   

$myConnJ = mysqli_connect("hostname","username","password","databasename") 
or die ("could not connect to joomla");
//query login info from Joomla
$sql = "SELECT * FROM jos_session WHERE id='".$id."'"; 
$result = mysqli_query($myConnJ, $sql) 
or die('ERROR: jos_user query failed for id = $id');
$row = mysqli_fetch_array($result);

echo $row;
// user data I need for charts is saved in session vars
$jom_id = $row['id'];           
$_SESSION['jom_id'] = $jom_id;

}
?>

I've attempted a number of echos to obtain the data to show but there is nothing working. Ideally $jom_id will be the user id in the session data which i could place in to the custom database. I believe this needs some development around the approach to verifying the matched up snacks.

Yet another factor, I had been reading through on similar issues and there is the suggestion to make use of built-in Joomla instructions to gain access to the Joomla session data. For those who have insight onto the way i can use these instructions, please tell me. Here are a few good examples from the code:

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

Of course, appreciate any direction that you could provide. It's greatly appreciated.

Maybe I get me wrong what you're asking, for that retrieval of user id is fairly simple in Joomla. To be able to obtain the information in the presently drenched user you simply acces the JFactory::getUser() method. You don't need to make use of periods:

$user = &JFactory::getUser();

You have an item that contains all of the infos you'll need:

  1. id - The initial, statistical user id. Make use of this when referencing the consumer record in other database tables.
  2. title - The title from the user.
  3. username - The login/screen title from the user.
  4. email - The e-mail address from the user.
  5. password - The encoded version from the user's password
  6. password_obvious - Set towards the user's password only when it's being transformed. Otherwise, remains blank.
  7. usertype - The role from the user within Joomla!.
  8. gid - Set towards the user's group id, which matches the usertype.
  9. block - Set to '1' once the user is placed to 'blocked' in Joomla!.
  10. registerDate - Set towards the date once the user was initially registered.
  11. lastvisitDate - Set towards the date the consumer last visited the website.
  12. guest - When the user isn't drenched in, this variable is going to be set to '1'. Another variables is going to be unset or default values.

You will get the id like $id = $user->id;

I required the above mentioned informations in joomla! wiki: Accessing the current user object
Api documentation from the JUser class: JUser.