Use a $_SESSION variable in Javascript

I needed to pass a SESSION variable to a Javascript function. The SESSION variable contained a customer ID that I would pass to an AJAX routine. Here's how:

getPictures(<?php echo $_SESSION['customer_id'] ?>, albumid);

This works because php is server-side and will put the varible into the Javascript code and then pass it to the PC for further processing.

SESSION variable not working in php

When a $_SESSION variable wont save it is usually because session_start() command is not at the top of your php code.

If you have the session_start() command then the problem is likely with the data. Another symptom of this is the error message

Warning: Unknown: Node no longer exists in Unknown on line 0

SESSION variables store simple data types like integers strings and arrays. Complex data types must be cast specifically or serialized (see below).

Here is a snippet of my code

session_start();
// calling a website to get XML data
$customer_token = file_get_contents($url);
$dom->loadXML($customer_token);
$s = simplexml_import_dom($dom);
// token is stored as an XML object, not text
$token = $s->token[0];
// the $token variable must be cast as a string.
$_SESSION['customer_token'] = (string)$token;

This fixed the problem of the $_SESSION variable not saving and it got rid of the warning message.

Object state can be stored in a session by using the serialize() function. serialize() will write the objects data into an array which then can be stored in a $_SESSION supergloblal. unserialize() can be used to restore the state of an object

JomSocial ERROR there was an error uploading this file to the server

Joomla ERROR installing JomSocial

"there was an error uploading this file to the server"

Installing the JomSocial extension on Joomla! 1.5.21 generated the vague message that there was an error uploading the file to the server. This was caused because my hosting provider had too small of a limit on upload_file_size.

Here is the solution

  1. Go into Joomla! administrator and click on Help/System Info.
  2. Click on the PHP Information tab.
  3. Scroll way down to the Core section and find upload_max_filesize.
  4. This number must be larger than com_community_pro_1.8.11.zip. My value was 2M.
  5. Go to your root directory where Joomla! is installed and look for the php.ini and open it.
  6. Find upload_max_filesize and change the number. I just added a zero so that it looked like this: upload_max_filesize = 20M.
  7. I saved the file and went back to Joomla.
  8. I went to Extensions Install/Uninstall and installed it again. SUCCESS!

 

 

Adding Flex 3.0 swf to Joomla 1.6

Adding Flex 3.0 using MySQL to Joomla 1.6

I redesigned a web application to take advantage of Joomla 1.6. One of the key features of the old web site was its use of interactive charts that were created in Adobe Flex 3.0. These charts used their own MySQL DB to query data that they would display.

Name Description
joom16 Joomla 1.6 Database
shread legacy database containing chart data

This is how I included them into Joomla.

Create the environment

alt

Within Joomla root directory, I created a new directory to hold my custom code and I named it ./user. In here is where all my custom non-joomla code exists for the website. For this tutorial we will be focusing on the ./shread directory. I don't know if creating a directory of my own is what Joomla recommends, I suspect it isn't. But I was unable to find a Joomla approved answer to this question so I just created my own.

I copied my Flex .swf files to the ./shread directory. The Action Script code for each of these files makes a call to the ./scripts/ directory where .php files are located that contain code that does the following.

  1. Connects to the shread database
  2. Makes a SQL query to get the data
  3. Passes the result back to the .swf via HTTPService

How this is done in ActionScript is beyond the scope of this tutorial.

I ran into some problems connecting to the database. Because my code is outside of Joomla, I was altunable to use the recommended Joomla calls to JFactory. So, I had to use regular php mysqli_connect statements. I used to mysqli_connect statements, one connected to shread, and the other connected to joom16.

These steps were placed in the file named connect_to_mysql.php.

Another file named checkuserlingin.php contained code to determine the joomla user.

Find logged in user info from outside of Joomla 1.6

<?php
session_start();

//makes connection to databases
include_once "connect_to_mysql.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("localhost","username","passwd","joom16") or die ("could not connect to shooterr_joomla");
//query login info from Joomla
    $sql = "SELECT * FROM jos_users WHERE id='".$id."'"; 
    $result = mysqli_query($myConnJ, $sql) or die('ERROR: jos_user query failed for id = $id ');
    $row = mysqli_fetch_array($result);

// user data I need for charts is saved in session vars
    $username = $row['username'];             session_register('username');
    $_SESSION['username'] = $username;
    
    $email = $row['email'];
    session_register('email');
    $_SESSION['email'] = $email;
 
  //I save have a custom field in Jomsocial that I also need   
    $sql = "SELECT * FROM jos_community_fields_values WHERE user_id='".$id."' AND field_id='16'";
    $result = mysqli_query($myConnJ, $sql) or die('ERROR: jos_community_fields_values failed for id = $id ');
    $row = mysqli_fetch_array($result);
    
    $primary_club = $row['value'];
    session_register('primary_club');
    $_SESSION['primary_club'] = $primary_club;

}
?>

I have Jomsocial 2.2 also installed on this website. I did some testing and found that a common cookie named "activeProfile" contains the user_id of the jos_sessions table.

Executing the Flex swf file from inside Joomla 1.6

I created a Joomla Menu Item with type = Iframe Wrapper. In the URL I used my website root with the path to the .swf that matches the directory tree:

 http:www.mydomain.com/user/shread/shooterStageScores.swf

Professional software development

 Professional programmer specializing in PHP Javascript AJAX computer graphics

Professional website development is just an email away. Specializing in beautiful web pages that are easy for you to keep up to date and grow as your business grows.

Your web site is just the first step in marketing your business on the Internet. Maybe you already have a beautiful website but no visitors. That is because your website must be created so search engines will find it. This is called Search Engine Optimization (SEO). A pretty website is not enough. 

Many organizations rely on custom computer applications that I have developed and have benefited from my expertise. So, whether it is php, Javascript, web site design or java programming; you've come to the right place .

 

Please contact me to get started.

Social Icons