Codeigniter 4 how to unset session - session

I am trying to unset user session and set user session back again after use update their profile. I keep facing issue trying to unset the user session, I keep getting errors.
I have no issue setting my session like this :
session()->set($data);
But if I unset my session like this :
session()->unset($data);
I get an error message : Call to undefined method CodeIgniter\Session\Session::unset()
Or should I just destroy session and re-set it again?
Can anyone help me out here? Thanks in advance guys!

In CI4, need to do use
session()->remove($data);
further, please explore https://codeigniter.com/user_guide/libraries/sessions.html?highlight=#removing-session-data

Destroying a Session: https://codeigniter.com/user_guide/libraries/sessions.html?highlight=sessions#destroying-a-session
To clear the current session (for example, during a logout), you may simply use either PHP’s session_destroy() function, or the library’s destroy() method.
Both will work in exactly the same way:
<?php
session_destroy();
// or
$session->destroy();

Related

Parse.User.current(), globals etc

I am new to Express and Parse. I am trying to make a simple modification to the Anyimg cloud tutorial. I would like to display the default home page only if the user is authenticated. If I use Parse.User.authenticated(), I get:
The error was TypeError: Object function (){e.apply(this,arguments)} has no method 'authenticated'
at app.js:33:18
at main.js:1:1
Parse.User.current() compiles with no error, but the code does not work. What am I missing? I have tried literally over 60 different options over the last 3 days with no success.
I also tried to declare a GLOBAL from within user.js and that did not work. I did a require cloud/user from within app.js and that did not help at all.
I hope someone can help. It appears that Parse tutorials (outside parse.com) from about a year ago break.
On the other hand, I wanted to display the login page (not default) upon logout. I got it done in less than 10 minutes.
The correct way to do this would be:
var currentUser = Parse.User.current();
if (currentUser) {
// logged in
} else {
// not logged in
}
Parse.User.current() returns the current user, or undefined if not logged in.
as seen here: https://parse.com/docs/js_guide#users-current
As mentioned above, I already had code that was similar to Fosco's. However, the var statement was not in the scope of the function as I was trying out o. I just moved it in and it all works. Thanks to everyone for looking into this.

Meteor: unreliable session variable?

I'm making a service where there are no user accounts, and I want to restrict by what page I'm visiting.
So each page is a "box", and on each "box" I have a bunch of "files".
I've published the relevant info in server/publications.coffee
Meteor.publish 'files', (boxId)->
console.log boxId
return Files.find({boxId:boxId})
My file for 'box' has a subscription handle:
#filesHandle = Meteor.subscribe 'files', Session.get('currentBoxId')
And the currentBoxId is stored in the session variable.
Here's the crazy part: I expect this to work, and it does on the first time I start the server. The console.log in the first bit of code prints the proper ID. Then, all of a sudden the console log suddenly starts returning "null", even when I console.log the session var in the browser console, it returns correctly.
I feel like there's some kind of loading asynchrony issue here, but I have no idea what's going on.
Any clues?
Ah, figured it out. The template can be rendered before the session variable is set, apparently. Usually you put your collection handles in the main.js file in the application scope, but this doesn't work if the subscription depends on session variables.
I did the following:
Template.boxPage.created = ()->
#filesHandle = Meteor.subscribe 'files', Session.get('currentBoxId')

Meteor 0.5.9: replacement for using Session in a server method?

So, I was attempting to do something like the following:
if(Meteor.isServer){
Meteor.methods({connect_to_api: function(vars){
// get data from remote API
return data;
}});
}
if(Meteor.isClient){
Template.myTpl.content = function(){
Meteor.call('connect_to_api', vars, function(err,data){
Session.set('placeholder', data);
});
return Session.get('placeholder');
};
}
This seemed to be working fine, but, of course, now breaks in 0.5.9 as the Session object has been removed from the server. How in the world do you now create a reactive Template that uses a server-only (stuff we don't want loading on the client) method call and get data back from that Method call. You can't put any Session references in the callback function because it doesn't exist on the server, and I don't know of any other reactive data sources available for this scenario.
I'm pretty new to Meteor, so I'm really trying to pin down best-practices stuff that has the best chance of being future-proof. Apparently the above implementation was not it.
EDIT: To clarify, this is not a problem of when I'm returning from the Template function. This is a problem of Session existing on the server. The above code will generate the following error message on the server:
Exception while invoking method 'connect_to_api' ReferenceError: Session is not defined
at Meteor.methods.connect_to_api (path/to/file.js:#:#)
at _.extend.protocol_handlers.method.exception ... etc etc
Setting the session in the callback seems to work fine, see this project I created on github: https://github.com/jtblin/meteor_session_test. In this example, I return data in a server method, and set it in the session in the callback.
There are 2 issues with your code:
1) Missing closing brace placement in Meteor.methods. The code should be:
Meteor.methods({
connect_to_api: function(vars) {
// get data from remote API
return data;
}
});
2) As explained above, you return the value in the session, before the callback is completed, i.e. before the callback method had the time to set the session variable. I guess this is why you don't see any data in the session variable yet.
I feel like an idiot (not the first time, not the last). Thanks to jtblin for showing me that Session.set does indeed work in the callback, I went back and scoured my Meteor.method function. Turns out there was one spot buried in the code where I was using Session.get which was what was throwing the error. Once I passed that value in from the client rather than trying to get it in the method itself, all was right with the world.
Oh, and you can indeed order things as above without issue.

CakePHP 1.3 - Calling Shells from a Controller?

I cannot for the life of me figure out how to call a shell from a controller.
We have a background process that packages up data in a .pdf, and we don't want to bog down the page loads waiting for this to occur, so we want to put all this processing in a shell.
I've figured out how to pass values to a shell with $this->args
I know you can use App::import('Shell','TestShell')... but after that I am lost.
How do I call the main() function of the shell within a controller?
In Cake 1.3, I was able to get it working by doing the following:
App::import('Shell', 'Shell');
App::Import('Vendor', array('shells/shell_title'));
$myShell = new ShellTitleShell(new Object());
$myShell->initialize();
$myShell->someAction();
I should be more focused reading the question :/
Could do it in Cake2, not sure how different would it be for 1.3. :?
<?php
App::import('Console/Command', 'AppShell');
App::import('Console/Command', 'HelloWorldShell');
$h = new HelloWorldShell();
$h->dispatchMethod('main');
?>
Windows:
If you do not have your environment variables set you will need to provide location of PHP executable.
C:\wamp\bin\php\php_v\php.exe C:\wamp\www\cakephp\cake\console\cake.php test this_arg_0 this_arg_1
Linux:
You may already have your php location defined. If not, you may need to export it to your $PATH or provide full path to php
php /var/www/html/cakephp/cake/console/cake.php test this_arg_0 this_arg_1
main() function will be called by default.
Hope it helps!

rails session_store odd behaviour

I am using active_record_store in a rails application which is storing this in session session[:email] = "email#address.com"
now this works fine in the action. but when this action gets over and is redirected to another page, which also accesses the same session[:email] I get an error
undefined method `eq' for nil:NilClass
this should probably mean that i am trying to compare values at some place i am not allowed to. but i cannot see anything like that in the code.
This looks like an old question, but I was just having the same problem and had to figure it out on my own, and thought I would post the solution up here for anyone else that runs into this. It's not very well documented, but to get this to work you have to add:
config.action_dispatch.session_store = :active_record_store
to application.rb, and
Application.config.session_store :active_record_store
to config/initializers/session_store.rb. Then, you have to do:
rake db:sessions:create
and:
rake db:migrate
Then, you have to restart your rails server. I think it was the db:sessions:create step that tripped up the original poster. Not only does that database table have to be laid out the way rails is expecting (that is, with an 'id' column, which is the actual cause of this error, I think), but also the current session has to have a valid ID. Hence the need to create the table and re-start the server, or potentially empty the table if it exists.

Resources