No account yet?

Links

Home arrow Articles arrow Programming arrow How to identify the currently logged in user in Joomla from within a wrapper
How to identify the currently logged in user in Joomla from within a wrapper PDF Print E-mail
Tag it:
Delicious
blogmarks
Stumble
Furl it!
Digg
YahooMyWeb
Technorati
User Rating: / 8
PoorBest 

It is common knowledge that you can access the current user information from within the joomla framework i.e within a component or a module by using the global variable $my

But in situations where you need to access the current user object from within a wrapper to dispaly personalized content or perform database transactions for the current user a different approach is needed.

You can use a session variable to keep track of the user from within wrapper.php. I have found this method to be unreliable and have opted to use a different approach. The approach involves creating a database table and tracking the PHPSESSID session ID variable against the current user object from within the wrapper class.

Read on to see how its done...

First of you will need to access the wrapper.php file located in /components/com_wrapper/wrapper.php

 Since the wrapper.php file is a part of the framework we can access the user object through global $my

We are going to be looking at the function

Now we will use $phpSessionId and $userId with a mysql database table.

First Create a table with the following name and fields

Now within the same function showWrap we are going to create a database connection and then we are going to insert a row in the table with the userId and the sessionId every time a user session is created. Since each session is unique, the table will give us the ability to access the current userId depending on the current sessionId.

Now to access this information from our wrapper module we will need the ability to write PHP code directly in the WYSIWYG editor and have the server interpret the code as PHP and not just text. To do this we will need a third party mambot that automatically converts any PHP code to executable PHP code for dynamic functionality.

 We are going to use a mambot called DirectPHP, which can be downloaded here . Note that this is for Joomla 1.0, you can find a 1.5 version on the author's site . Install the mambot and make sure that is is published.

The only nuance is to make sure that the PHP code is formatted as a paragraph, within the WYSIWYG editor of your choice.

From within the wrapper, the first line should be a connection to the database and a retrieval of the record based on the session ID, which is constant. 

This is how your typical wrapper file should begin:

Now you have the userId and you can use it to track the currently logged in user. Note that this example has been tested to work in Joomla 1.0. You can also introduce constraints in your SQL syntax to select a distinct value from the database to speed up execution time. The reason being that every time your user accesses the wrapper, an entry is made in the table. So eventually you will have a table full of duplicate session and userId values. To prevent this from happening you can introduce a constraint in the wrapper.php file that will check the database for the sessionId first. If the session id exists then no new information should be written to the table.

Here is what your showWrap() function should look, taking the duplicate PHPSESSID issue into account.

Comments
Search
Only registered users can write comments!
mallias1   |Registered |2009-05-19 15:45:43
can anyone tell me if this works with joomla 1,5?
the wrapper.php is a bit
different ...
any help appreciated
admin  - Yes   |SAdministrator |2009-05-19 15:53:09
avatar I would reccomend creating a component out of it.
That is the best way to have
access to variables that are a part of the my.
I will try to post a tut on
here.
syntheticintel  - it should work   |SAdministrator |2008-06-28 13:55:52
avatar This method should also work in J1.5 give it a try and let me know if you run
into issues.
syntheticintel  - Ill let you know what I find   |SAdministrator |2008-06-26 17:55:07
avatar I'll look into the 1.5 code and see how the wrapper is implemented. I'll get
back to you on that.
dprice   |Registered |2008-06-26 18:46:01
thanks syntheticintel - any help at this point would be great
dprice  - re: Not too sure   |Registered |2008-06-26 17:50:12
syntheticintel wrote:
Have you tried this solution out in J1.5?
Did it work?
Can you post your
wrapper.php file if applicable?


I haven't tried it in J! 1.5 - still trying to get my head around all the
changes in J!1.5.

I'm desperate at this point to get it to work.
ready to pay someone who has a good command of J!1.5 to implement a
hack.
syntheticintel  - Not too sure   |SAdministrator |2008-06-26 17:44:26
avatar Have you tried this solution out in J1.5?
Did it work?
Can you post your
wrapper.php file if applicable?
dprice  - joomla 1.5 solution   |Registered |2008-06-26 17:30:22
how would I go about achieving the same solution in joomla 1.5?
Anonymous   |68.19.28.xxx |2008-06-20 09:10:45
the problem is the session along with my->id is inserted into the database. How
would I go about removing that value, in the same way that it is removed in the
jos_session table when a user logs out
syntheticintel  - A possible solution   |SAdministrator |2008-06-19 14:01:16
avatar You could try to force the cookie to expire by saying.

setcookie
("PHPSESSID", "", time() - 3600);

I think this would have to go
in the com_login module right in the code that is called when the logout button
is pressed.
It could be cb_login if you were using community builder.
I
haven't tried this out, but I don't see why it wouldn't work.
GM  - Session Doesnt Die   |65.13.7.xxx |2008-06-18 17:11:44
Works great, but session never dies at logout so if someone else logs in on the
same computer after you, they can edit your profile... or maybe Im doing
something wrong? The jos_session throws the user out of the db as soon as you
click log out. This one does not...Still I am using it and hoping to fix it.
syntheticintel  - fixed the session issue.   |SAdministrator |2008-06-28 13:59:58
avatar under components/com_comprofiler/comprofiler.php

Look for a function called


function logout()

In that function make a database call to delete all records
in the sessionTracker table that correspond to the current userId

In there
issue a SQL command to delete all rows from sessionTracker for the current user.


$strSql = 'delete from sessionTracker where
userId="'.$my->id.'"';

mysql_query($strSq l);
Mark  - Joomla Versus Wordpress   |59.96.88.xxx |2008-06-09 23:38:01
That was good - detailed step by step.
But makes me wonder if you need to do a
whole lot of things for any facility you need in Joomla? Wordpress comes with a
host of plugins...
MindRivet
syntheticintel  - True but   |SAdministrator |2008-06-10 11:40:11
avatar Joomla has an extended set of features that can take a site to a higher level as
far as building a vibrant community with custom features is concerned.
Wordpress
is a great tool for blog like situations, but creating a social networking site
in wordpress would be very difficult.
Last Updated ( Sunday, 11 January 2009 )
 
< Prev