Fiji Web Design
Welcome, Guest
Please Login or Register.    Lost Password?
Session variables not passing through (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Session variables not passing through
#1117
empirebattles (User)
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Session variables not passing through 2 Years, 1 Month ago Karma: 0  
Hello,
I have begun using the PHP Pages component, and all works well, until i attempt to pass my $_SESSION variables outside of joomla to a normal php page.
To discover/test this, i made a basic page through this component, and set the variable
session_start();
$_SESSION['mytestvar']='testable';
then, on another page, that was not run through joomla, i had the variable echoed back to me:
session_start();
echo $_SESSION['mytestvar'];
The echo on the separate page returned nothing, while an echo i did on another php page run through joomla returned/echoed 'testable'. Is this a bug, or part of the functionality of this component? Any more information you need, i will be more than happy to provide. thank you
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/19 10:48 By empirebattles.
  The administrator has disabled public write access.
#1118
admin (Admin)
Admin
Posts: 475
graph
User Online Now Click here to see the profile of this user
Re:Session variables not passing through 2 Years, 1 Month ago Karma: 10  
You'll need to use the session handler that Joomla uses. Joomla saves session data to the database by default, but PHP uses files by default. So you're accessing the wrong medium for the data.

Here is how to load Joomla in a standalone app, which is probably the easiest way. The important part is the path to Joomla, which must be set in JPATH_BASE.


// Set flag that this is a parent file
define( '_JEXEC', 1 );

// replace this with your Joomla path
define('JPATH_BASE', '/path/to/joomla/');

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$App =& JFactory::getApplication('site');

// now you can use Joomla functions

// session
$Session =& JFactory::getSession();

// $Session->set('example', 'remember me');
// $Session->get('example');

// or directly via
$_SESSION;

// $_SESSION['example'] = 'remember me';
// $_SESSION['example'];

// user - a good alternative (see JUser::setParam())
$User =& JFactory::getUser();

//$User->setParam('example', 'remember me');
//$User->getParam('example');
You can also just load the session files in libraries/joomla/session/ if you don't want the overhead of the whole Joomla framework (about 3 Mb). A bit more involved. The other thing is the domain must be the same so cookies can be read. Even www.example.com is not the same as example.com in this case. You can set the session cookies to be read across sub domains, but I would not recommend it as it is not a secure practice (It opens XSS from any subdomain). See: http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/19 16:38 By admin.
  The administrator has disabled public write access.
#1119
empirebattles (User)
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Session variables not passing through 2 Years, 1 Month ago Karma: 0  
well, it may be easier to explain slightly what i'm aiming to do. I have several session variables that are established (with $_SESSION) through joomla. Then i have a transitional/loading page which variables are re-assigned, and then the user is directed to a page that is NOT joomla (but the session variables are still required to proceed)... I'm not all that in-depth into joomla, so i'll try to figure this out as i type... I will assume that my site is simply 'example.com', and joomla is installed on the root directory.



<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );

// replace this with your Joomla path
define('JPATH_BASE', '');

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$App =& JFactory::getApplication('site');

// now you can use Joomla functions

// session
$Session =& JFactory::getSession();

// $Session->set('example', 'remember me');
// $Session->get('example');

// or directly via
$_SESSION;

// $_SESSION['example'] = 'remember me';
// $_SESSION['example'];

// user - a good alternative (see JUser::setParam())
$User =& JFactory::getUser();

//$User->setParam('example', 'remember me');
//$User->getParam('example');
?>
with that on a .php page, it returns an error message: Restricted accessRestricted access Fatal error: Class 'JFactory' not found in /*directory of file* on line 15 Again, i'm not all that familiar with joomla structure, so this is probably a simple fix. Thanks again
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/19 17:54 By empirebattles.
  The administrator has disabled public write access.
#1120
admin (Admin)
Admin
Posts: 475
graph
User Online Now Click here to see the profile of this user
Re:Session variables not passing through 2 Years, 1 Month ago Karma: 10  
You need the correct path to your Joomla installation and place it in

define('JPATH_BASE', '');
If you don't know the absolute path, use a path relative to your application. eg:
/var/html/joomla/
/var/html/myapp/
Then Joomla would be in:
define('JPATH_BASE', '../joomla/');
relative to /var/html/myapp/
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/20 01:35 By admin.
  The administrator has disabled public write access.
#1121
empirebattles (User)
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Session variables not passing through 2 Years, 1 Month ago Karma: 0  
Ah... so its the root directory from the server path. I see now.

So, assuming my name on the server was user 'examp', the path i used was:

define('JPATH_BASE','/home/examp/public_html/');
It worked perfectly Thank you for your support *edit*. Now, if i understand this correctly, this changes where the $_SESSION variables are being received from (the joomla database instead of the server files). Is it possible to change back to normal server session files within the same script? (by somehow changing the directory back to the server files). Basically, set $holdvar=$_SESSION['joomlavar']; and then down the page, be able to set $_SESSION['normalvar']=$holdvar; , essentially passing the session variables into 'open water', so that the joomla framework doesn't have to be opened every time i want to call those variables. (as a work around right now, i am using cookies, but those are far too easy to edit).
 
Report to moderator   Logged Logged  
 
Last Edit: 2009/12/20 06:46 By empirebattles. Reason: Add more explination to what I am attempting to do
  The administrator has disabled public write access.
#1122
admin (Admin)
Admin
Posts: 475
graph
User Online Now Click here to see the profile of this user
Re:Session variables not passing through 2 Years, 1 Month ago Karma: 10  
All the Joomla session data is in the database. So if you revert to files, there is nothing to read there.

You can however bypass having to load the full Joomla framework. To do that you have to just include the Joomla session handler in libraries/joomla/session/ and assign this as the session handler for your session.

I won't go into that as it is quite time consuming and would involve a lot of code/debugging on your part.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Joomla Downloads

DownloadsDownload our free Joomla! Components, Modules and Plugins.