Re:Global / session variables (1 viewing) (1) Guest
Favoured: 0
|
|
|
TOPIC: Re:Global / session variables
|
waggers (User)
Fresh Boarder
Posts: 1
|
|
Global / session variables 4 Years, 6 Months ago
|
Karma: 0
|
|
I don't know whether this is a general Joomla problem or something specific to directPHP but I'm hoping you can help. I'm having problems setting session variables in included PHP files.
I'm trying to set up a CAPTCHA on a form on my website. The plan is to create the captcha code and save it as a session variable. There's then a separate PHP file which uses this to generate the CAPTCHA image, and then of course the test on the page that receives the form submission to ensure the submitted code matches the one that was originally set. How do I create a new session variable and make it stick with Joomla?
I've used $_SESSION['myvariable'] = "captchacode"; but in the PHP file that generates the image, $_SESSION['myvariable'] is just an empty string. I've been struggling with this for days - please help!
|
|
|
|
|
|
|
The administrator has disabled public write access.
|
kksou (Admin)
Admin
Posts: 1680
|
|
Re:Global / session variables 4 Years, 4 Months ago
|
Karma: 27
|
|
Hi,
Sorry, I must have missed your posting.
Have you got this fixed yet?
Did you issue a session_start() before starting to use the session variable?
Depending on the configuration of your PHP, you might or might not be able to use DirectPHP, as the header information will already been written by the time it gets to your DirectPHP statement.
If your PHP setup does not allow you to do a session_start() from within an article content, then you might need to write your own component or module to handle this.
Regards,
/kksou
|
|
|
|
|
|
|
The administrator has disabled public write access.
|
|
|
|
Re:Global / session variables 2 Years ago
|
Karma: 0
|
|
It is not a problem. This is intended behavior of newer versions of Joomla.
Joomla 1.5.x implements its own session handler which takes control over $_SESSION. Hence, anything you put in there will be overwritten by Joomla. You have to work with Joomla's session handler from your DirectPHP code instead of PHP's session handler.
Here is my Joomla session storage library that is compatible with both 1.0 and 1.5.x. This code is ready to use. If you want to know more about it I recommend looking up the classes and methods it mentions. I have basically torn it out of my website and tried to remark on it; if something is not clear you will want to refer to PHP manual, websites that teach object-oriented programming, Joomla docs, and maybe just google some pieces of code below to find other people talking about the same thing.
/////cut here/////
var $JoomlaSession;
function JoomlaSessionRead10($key)
{
session_start();
return $_SESSION[$key];
}
function JoomlaSessionWrite10($key, $val)
{
session_start();
$_SESSION[$key] = $val;
}
function JoomlaSessionRead15($sessionSpace)
{
// For Joomla 1.5.x
global $JoomlaSession;
/* Connect to Joomla */
// Initialize framework
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// Get Joomla session data, for storing & recalling our data
$JoomlaSession =& JFactory::getSession();
return $JoomlaSession->get($sessionSpace);
}
function JoomlaSessionWrite15($sessionSpace, $sessionData)
{
// For Joomla 1.5.x
global $JoomlaSession;
/* Done, save info back to Joomla sessions for next time */
return $JoomlaSession->set($sessionSpace, $sessionData);
}
Some notes:
- Use the functions ending in 10 for version 1.0, ending in 15 for Joomla 1.5.x.
- Joomla's session handler stores an entire array of data under a name. i.e.
$myData = array();
$myData['cool']='This string is going to be saved in session in the namespace CoolApp';
JoomlaSessionWrite15('CoolApp',$myData);
So you will want to maintain $myData outside, and then simply call JoomlaSessionWrite15 anytime you want to save it or any of its keys.
- You will want to re-use $JoomlaSession. It is your access to the session handler. First call JoomlaSessionRead15('your app name') to initialize your session storage namespace. Then you may call JoomlaSessionWrite15 with that namespace. If you do not call Read first, the session handler and namespace will not be initialized for the Write function to use and Write will not work. So call Read in the beginning of your PHP code, then work with your data in some array, then in the end before your code terminates Write your array to store it all.
HTH
Alex Trust
|
|
|
|
|
|
|
The administrator has disabled public write access.
|
|
|
|
|