Joomla - Unset multiple session variables not working - session

I thought this would be simple but I guess there's a catch somewhere...
I'm developing a custom part of code for a Joomla installation and I need to unset some session variables before executing my code. So, naturally, I have
$session->clear('var1');
$session->clear('var2');
$session->clear('var3');
$session->clear('var4');
but the page appears totally blank and nothing happens. Any suggestions?

Assuming that you got the $session variable like this:
$session = JFactory::getSession();
If you are getting a blank page, you probably some error in your code. Do you have access to some kind of error log? If not, you can try to force displaying errors from your code if it's not a production environment (although it's not the best way to do it) or enable debug mode from the joomla administrator.
You can also try to run the php file in your browser, and if everything is ok and there are no parse errors in the file, you should see a message like 'Restricted access' or similar.
Besides, if the script is not crashing, you can check what value is returning each call to $session->clear( 'xxx' ) (It should return the value you just cleared.
The last thing that comes to my mind is that the vars you have stored in session are in a different "context". When you get/set data to session, you can pass a "namespace" as an additional parameter, so these vars are stored in that "namespace" (in fact, it's stored inside another index inside the session. So if possible, you should check if these variables are stored in session using a different "namespace":
$session->set( 'var1', $value, 'another_namespace' );
If so, you should clear it like this:
$session->clear( 'var1', 'another_namespace' );
P.S.: I said "namespace" because it's the parameter name that Joomla uses in these session methods, but don't get confused with PHP namespaces.
I hope it helped!

Related

How do I pass admin module data to Smarty in WHMCS?

I would like to pass some data from a WHMCS admin module to the client summary admin template. In order to get the data from our admin module to the Smarty template, we have been attempting to use an action hook called "AdminAreaPage". This action hook is supposed to take an array of variables (returned in the action hook) and make them accessible as Smarty variables.
The issue I am having is that none of the variables we return in that action hook seem to be accessible to the template (clientssummary.tpl). I have tried listing all available Smarty variables with {debug} as well as several other methods, but none of our custom variables are displayed (the other template variables are successfully listed). The basis for our action hook is taken directly from the WHMCS documentation example:
http://docs.whmcs.com/Hooks:AdminAreaPage
The action hook is running on each page load, as we are able to echo text directly to the page. We are not able to access any data passed to Smarty, however, or we are not passing it correctly. Here is some example code similar to what we are using in our action hook, (nearly unmodified from the WHMCS example code):
function module_hook_test($vars) {
$return = array();
$return = array("field1" => "value1", "field2" => "value2");
return $return;
}
add_hook("AdminAreaPage",1,"module_hook_test");
(with "module" being replaced with our actual module name)
Any assistance with this action hook, or an alternate recommended method of passing the data, would be greatly appreciated. Thank you.
I submitted a ticket to WHMCS support. They have believe the issue may be due to a bug in WHMCS and said that the developers will address the issue in a future release. In the mean time, they suggested querying the data directly from the template using {php}{/php} tags. This will work fine for us as a temporary workaround.

Include a jade file with the use of a session variable

I am developing a web application that have 3 different acctypes.
Student teacher and admin.
Depending on what acctype is logged in I wan't to load different menus.
I have acctype assigned to a session variable;
#{session.acctype}
So I used to handle it like this;
script(src='js/sidebar#{session.acctype}.js')
But I wanted to get rid of the use of document.write that I used in the js file that was loaded above.
So now I have the menu in a jade file inside.
Here comes the problem, I don't know how to use the session variable to load a jade file!
I tried this without any success;
include sidebar#{session.acctype}.jade
So, is there any way to handle this?
In order to give your templates access to your session variables, you need to add them to res.session.locals. An easy way to accomplish this is by adding
app.use(function(req,res,next){
res.locals.session = req.session;
next();
});
to your application before you call app.use(app.router). This technique is called using middleware.

CakePhp2 defined CAKE_CORE_INCLUDE_PATH, now DS and CORE_PATH undefined in ShellDispatcher

In order to share CakePhp2 core between many sites I edited CAKE_CORE_INCLUDE_PATH in webroot/index.php to point to the cake directory. That works and I can reach my welcome page.
However, when I attempted to run the Cake Console, I ran into issues with ShellDispatcher.php not defining DS and CORE_PATH because they were only defined if CAKE_CORE_INCLUDE_PATH was NOT defined. Once I defined them out of the if statement which checks if CAKE_CORE_INCLUDE_PATH was not defined, I was good to go. However, I would prefer not to "hack" this file because I want to keep the cake core files clean. Are there any better and cleaner options?
I also had to defined CAKE_CORE_INCLUDE_PATH and use it to set $dispatcher in Console/cake.php which is part of the app of course.
Yes there are. As a start never define application-wide variables in Webroot/index.php. As a matter of fact you shouldn't be touching this file at all. You can define variables in
Config/bootstrap.php.
You were getting the error because when in a Shell you're not firing Webroot/index.php at all - you're actually running PHP's CLI and CakePHP fires off in a different manner.
It will go through the bootstrap.php file of course.
You should also use this file to define any global vars, but there is a little bottleneck
here when using PHP's $_SERVER variable's web server related members - e.g. HTTP_HOST and the definition: $myHost = $_SERVER['HTTP_HOST'];. If you start a shell Cake will try to set this variable and it will fail throwing an error. This is because as mentioned before PHP will be running in CLI mode when the CakeShell is envoked. There is a way to detect this of course - you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['SCRIPT_NAME'] in bootstrap.php to identify if you're in CLI or not. :)

How to trace CakePHP's inner workflow

Short description
I'm getting used to CakePHP right now and am wondering about how to get more debug-information about what is happening inside the framework.
Let me please explain my situation a little more detailed
As you know CakePHP does a lot for you without putting you into the need to write additional code. One example is the handling of models.
I just created a model User and added validation-rules (no other methods). As described in the API methods like save will just work.
After that I created the needed controller and view to add a new user. When I try to add a user from the view I just receive the flash-message The user could not be created. Please, try again. No validation-violations are flashed.
I also did set the debug-level to 2: Configure::write('debug', 2); but do not receive any errors. The error.log inside \tmp\logs is also empty.
I really do want to learn how to solve those issues in the future.
So what else can I do to debug / display inner processes of cake?
Thank you very much for your help!
DebugKit is an official plugin that gives you lots of information on the request, queries and variables produced by Cake:
https://github.com/cakephp/debug_kit
You can also use trace() and other methods in the Debugger to show what is being executed in the background:
http://book.cakephp.org/2.0/en/development/debugging.html
Use a PHP IDE with an integrated debugger. That will allow you to follow execution line by line as it is executed and even inspect variable values as you go. Netbeans is a free one.

How do I access a asp.net session variable from APP_CODE?

I have seen lots of posts here and elsewhere stating that one can access session variables from app_code. I want to access an already created session.
this code errors out because of a null exception.
string myFile = HttpContext.Current.Session["UploadedFile"];
this creates a null session variable.
System.Web.SessionState.HttpSessionState Session = HttpContext.Current.Session;
It looks like I can create a new session variable but not access an already created one. Anyone have any idea what might be giving me problems?
I was having ths same problem, but I was able to fix it because I controlled where in the event lifecycle I was attempting to leverage HttpContect.Current.Session... it makes a difference because Session isn't available, for example, on construction.
Maybe that will help you.

Resources