Joomla! Discussion Forums



It is currently Tue Nov 24, 2009 11:17 pm (All times are UTC )

 




Post new topic Reply to topic  [ 18 posts ] 
Author Message
Posted: Sun Jun 22, 2008 10:38 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Fri Apr 06, 2007 7:13 pm
Posts: 24
Hello all the Joomla wizards,

I was wondering if you could help me.

I am writing some PHP pages outside Joomla (version 1.0.12) and would like to password-protect them using Joomla's login system.

The idea is very simple. Just check if Joomla's login cookie or session is available and valid. If so, the PHP page is displayed. If not, user is redirected to Joomla's login page.

So it would look something like this

<?php
if (isset($_SESSION["name"]))
{ echo "Welcome " . $_SESSION["name"] . "!<br />"; }
else
{ header ("location: http://www.mydomain.com/joomla/index.php"); }
?>

I have searched the web and the Joomla 1.0.x developers manual, but could not find what I need to know, namely:
1. which cookie contains the session ID (that could be compared to the session ID value in jos_session db table)
2. how to extract the session ID from the cookie

If I can obtain the session ID, I know how to compare it to the jos_session table.

Could you please advise how to do the above 2 steps (codes please)?

Thanks very much.


Top
  E-mail  
 
Posted: Sat Jun 28, 2008 7:26 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Jun 21, 2006 2:50 am
Posts: 147
I cannot remember off the top of my head, but if you use your friend print_r you can see what is in the session and other variables like this


Code:
<?php
print_r($_REQUEST);
?>


You could even just throw that in your template and watch the variables change as you log in and out and navigate to see what other information is available to work with for your bridge.

Although, if I recall the session is unique to the site and server serving it, I'm not sure if its maintained when you leave the site, never used session's under such a circumstance.

A better solution would be to throw those PHP pages into Joomla if possible either as a component which dosn't use any of Joomla's functions or inside some wrapper.

_________________
My articles and components: http://www.WebDesignHero.com


Top
  E-mail  
 
Posted: Tue Jul 15, 2008 8:48 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Tue Jul 15, 2008 8:43 pm
Posts: 4
I am also for looking for a way to verify if the user is logged in. Basically, I am building a component that will use this a lot. Is there not a function you call in Joomla to verify that the user is logged in?


Top
  E-mail  
 
Posted: Wed Jul 16, 2008 2:20 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Jun 21, 2006 2:50 am
Posts: 147
hotelag wrote:
Is there not a function you call in Joomla to verify that the user is logged in?


You do this by testing on the $my variable with a if statement.

I have a tutorial at my website: http://www.webdesignhero.com/index.php? ... &Itemid=13

The original question was to get the information from outside of Joomla.

_________________
My articles and components: http://www.WebDesignHero.com


Top
  E-mail  
 
Posted: Thu Jul 17, 2008 6:13 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Fri Jul 04, 2008 11:25 am
Posts: 5
Hi,
I'm using joomla on localhost right now. I wanted an information.
How to check that a user is logged out at any stage in the application.I mean to refer to situations where users doesnt logout.what if he abandons the system and consequently leads to session expiring.How to know this.please tell


Top
  E-mail  
 
Posted: Thu Jul 17, 2008 9:48 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Jun 21, 2006 2:50 am
Posts: 147
Hello, first please look at my tutorial.

The logged in status is only in the session variable $my. If $my->id is set, then the user is logged in and has an active session. There is no way to really check if the user is inactive, but still has an active session. In HTTP, it asynchronous communication, so there is no a constant heart beat being recorded by the server, unless you coded something in AJAX possibly, but I think this would be a waste.

Perhaps you are asking something different, but either the user is logged in with an active session or the user does not have an active session, I do not see a difference between an time-out session and logging out on purpose.

_________________
My articles and components: http://www.WebDesignHero.com


Top
  E-mail  
 
Posted: Fri Oct 03, 2008 8:27 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Tue Jul 24, 2007 1:33 am
Posts: 33
Any idea if this is possible in Joomla 1.5.7?


Top
   
 
Posted: Sun Oct 12, 2008 5:06 pm 
User avatar
Joomla! Explorer
Joomla! Explorer
Offline

Joined: Wed May 17, 2006 4:15 pm
Posts: 285
Code:
include('configuration.php');
include('includes/joomla.php');


insert that to your external file.

and $my->id will give you the active user id.

function writeUser()
{
global $my;

echo $my->id;
}

_________________
http://www.turkcebilgi.net/
http://www.sozlukturkce.com/


Top
  E-mail  
 
Posted: Mon Nov 17, 2008 3:17 am 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Fri Nov 14, 2008 3:32 pm
Posts: 9
That does not work. It returns "Restricted access.".

_________________
http://www.videowhisper.com


Top
  E-mail  
 
Posted: Sun Feb 01, 2009 6:48 am 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Wed Jun 21, 2006 2:50 am
Posts: 147
In 1.5, you have a user object now.

JUSER
(http://api.joomla.org/Joomla-Framework/User/JUser.html)

You would call a new instance of JUSER, I believe by can create an instance and then access this information, read through the API page.

I just found this tutorial at LeBlanc's website: http://www.jlleblanc.com/joomla/Tutoria ... la!_1%115/ . It's pretty good explanation for anyone who wants a basic solution to the above concepts.

_________________
My articles and components: http://www.WebDesignHero.com


Top
  E-mail  
 
Posted: Tue Feb 10, 2009 10:26 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Mon Dec 22, 2008 10:35 pm
Posts: 32
I'm also getting a restricted access error.


Top
  E-mail  
 
Posted: Mon Mar 09, 2009 3:17 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Fri Aug 19, 2005 12:28 pm
Posts: 113
To overcome the restricted access message you need to define this constant as shown:

define( '_JEXEC', 1 );

That will let the Joomla scripts run, but I am still not sure the session info will be picked up with the code others have posted here.

_________________
JReviews - Joomla CCK and Reviews - http://www.reviewsforjoomla.com


Top
  E-mail  
 
Posted: Mon Mar 09, 2009 3:43 pm 
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Fri Aug 19, 2005 12:28 pm
Posts: 113
The solution for this is on another post:

viewtopic.php?p=1284623#p1284623

To summarize, this is what I did to make it work.

In my Joomla template index.php I added this:

Code:
// Make session data available outside of Joomla
if(!isset($_COOKIE['jsid'])){
    $user = & JFactory::getUser();       
    $temp_session = $_SESSION; // backup all session data
    session_write_close();
    ini_set("session.save_handler","files"); // set session saved hadler on file
    session_start();
    $_SESSION = (array)$temp_session['__default']['user']; // data that another php file need to know
    session_write_close();
    ini_set("session.save_handler","user"); // put back session saved handler on database
    $jd = new JSessionStorageDatabase();
    $jd->register(); // set required parameters
    session_start(); // restart //
    $_SESSION = $temp_session; // restore last session data
    $e = session_id();
    setcookie("jsid", $e, time()+3600,'/');
}


Outside of Joomla, to read the user session data:

Code:
// Read Joomla session data
if(isset($_COOKIE["jsid"]) && $_COOKIE["jsid"]!=""){
    session_id($_COOKIE["jsid"]);
    session_start();
    $user = $_SESSION;
} else {
    $user = array(); 
}
?>   


If the user object is not cast to an array in the template code, then it doesn't work correctly:

$_SESSION = (array)$temp_session['__default']['user'];

It is important to do this and then read the info as:

$user['name']....

_________________
JReviews - Joomla CCK and Reviews - http://www.reviewsforjoomla.com


Top
  E-mail  
 
Posted: Mon Mar 09, 2009 6:34 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Mon Dec 22, 2008 10:35 pm
Posts: 32
I see that, however, how about reading session from an external file i.e. an non joomla page?

Thanks!


Top
  E-mail  
 
Posted: Mon Jun 15, 2009 3:20 am 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Thu May 10, 2007 3:57 pm
Posts: 2
Greeting,

The solution seems to work for me but I am getting a empty array in the external php file.

Code:
Array ( [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [usertype] => [block] => [sendEmail] => 0 [gid] => 0 [registerDate] => [lastvisitDate] => [activation] => [params] => [aid] => 0 [guest] => 1 [_params] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => JParameter [_raw] => [_xml] => [_elements] => Array ( ) [_elementPath] => Array ( [0] => /home/snake/public_html/sya/libraries/joomla/html/parameter/element ) [_defaultNameSpace] => _default [_registry] => Array ( [_default] => Array ( [data] => stdClass Object ( ) ) ) [_errors] => Array ( ) ) [_errorMsg] => [_errors] => Array ( ) )



Any help would be great.
Thanks in advance.


Top
   
 
Posted: Mon Jun 15, 2009 5:12 am 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Thu May 10, 2007 3:57 pm
Posts: 2
Ok I think that I solve the problem. Maybe is something about how session and cookies work that I do not know.

In the moment in which I create the cookie the SESION array is empty. After that I login, but the when i try to get SESSION array from the external php file it is empty.

So I have to create the cookie when a user is login
Code:
$user = & JFactory::getUser();     

if ($user->get('id')!=0){
if(!isset($_COOKIE['jsid'])){
    $temp_session = $_SESSION; // backup all session data
    session_write_close();
    ini_set("session.save_handler","files"); // set session saved hadler on file
    session_start();
    $_SESSION = (array)$temp_session['__default']['user']; // data that another php file need to know
    session_write_close();
    ini_set("session.save_handler","user"); // put back session saved handler on database
    $jd = new JSessionStorageDatabase();
    $jd->register(); // set required parameters
    session_start(); // restart //
    $_SESSION = $temp_session; // restore last session data
    $e = session_id();
    setcookie("jsid", $e, time()+3600,'/');
   
}
}

In this way I get what I want. But if I logout from Joomla and I get the SESSION array from the external php file I get the the user data as I was login.

It is a bit confusing, If some one could explain a bit about this would be great. Thanks


Top
   
 
Posted: Wed Jul 22, 2009 3:42 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Wed Jan 21, 2009 8:32 am
Posts: 8
This last code by argenisleon doesn't work for me - any further ideas...

All session data seems to have disappeared. this cookie's there but nothing comes out of the session when I call it like this:
// Read Joomla session data
if(isset($_COOKIE["jsid"]) && $_COOKIE["jsid"]!=""){
session_id($_COOKIE["jsid"]);
session_start();
$user = $_SESSION;
} else {
$user = array();
}
?>


Top
  E-mail  
 
Posted: Mon Oct 12, 2009 11:41 pm 
User avatar
Joomla! Intern
Joomla! Intern
Offline

Joined: Sun Dec 17, 2006 11:13 am
Posts: 55
Hi,

Please check if this SOLVE:

---------------------------------------------
$user =& JFactory::getUser();

if($user->id)
{
//do user logged in stuff
}
else
{
//do user not logged in stuff
}

------------------------------------------


Top
   
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ] 

Quick reply

 



Who is online

Users browsing this forum: Yahoo [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group