How can I go about unsetting flashdata using the Codeigniter Session library? I know flashdata normally expires after one page refresh, however I'm using AJAX in a portion of my app, and when setting flash data, it still exists if I make a second AJAX call, so I need a method of unsetting flashdata manually.
I thought this might work as it is similar to unsetting session userdata:
$this->session->unset_flashdata('some_val');
However it doesn't do anything, any suggestions?
if you want to destroy all session value then use this:
$this->session->sess_destroy();
and if you want to destroy a particulat session the use:
$this->session->unset_flashdata('session name');
or
$sessionvar = $this->session->userdata('sessionname');
unset($sessionvar);
if this is not working then use following to empty the session value:
$this->session->set_flashdata("sessionname","");
Related
I am having trouble with developing a Laravel application. When I set a session from a function and loaded the view the session will show:
Session::flash("test","ABC");
return view('layout.customer');
But when I set a session and redirect it to a URL in that page, the session will not work. I am currently using following code:
Session::flash("test","ABC");
return redirect()->route('customer.details');
Variables that are flashed only last one request.
When you send a redirect, the redirect itself is the first request sent to the visitor. On this request the existing flash session is cleared.
Now a second requests starts for the page that is being redirected to and the session flashes no longer exist.
Update:
You can use the with function to add flashed data to a redirect.
return redirect()->route('customer.details')->with("test","ABC");
More info: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
You can use return redirect()->route('customer.details')->with("test","ABC");to flash the data when redirecting. Here's the documentation for this: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
In customer.details controller add line use Session; at top of the page.
I am working on a webapp in php, and my session keeps getting destroyed on only certain views of my application and I can't seem to find out why. I can't upload all the code here because there is too much, so I want to know what can destroy a session and I will look for the problem.
The weird part is that it happens inbetween two views, for example it fully loads the first view with no problem (I checked with echo statements at the end of that view to make sure it was still active) and when I click on a link the session variable is destroyed before loading the next view.
if it happened only on certain view, is there any session unset or session destroy? and maybe you need to add session_start() on controller which have those view (linked). (read also https://www.sitepoint.com/php-sessions/ about session_start section )
You can destroy ALL the session array :
session_destroy();
Or only unset some parts :
unset($_SESSION('your_thing']);
An other way is to put empty some parts of the array:
$_SESSION['your_thing'] ='';
All are correct, but if you destroy all the session, on the next page you have to set session_start(); if you want use session, but you 'll lose all informations.
the best way is to use unset or empty the array ...
You can use the session_destroy();-function to destroy the whole session (and all $_SESSION-values with it) or you can the unset-function to destroy a value by key as if $_SESSION was an array.
Make always sure you're using session_start() the right way. On top on every page you are using the sessions. The session keeps destroying because the session was never made. To make it work, be sure you have set up the session start like this example:
<?php
session_start(); // Be on top
?>
<!DOCTYPE html>
<!-- The rest of your page
I hope this will help you
I have used session_destroy in MVC pattern.
If I click logout link, it will redirect correct url but page disappears. It is displaying the below error in Firefox.
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in
a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies."
This is the function I'm using for logout.
Logout function:(Not working)
public function Logout(){
session_destroy();
$this->redirect('index.php?r=admin/login');
}
I have unset($_SESSION['userName']) the session variable. It is working fine. But session_destroy is not working in that place.
What is the reason for that?
Logout function:(working)
public function Logout(){
unset($_SESSION['userName']);
$this->redirect('index.php?r=admin/login');
}
you can use another way to remove session like:-
$_SESSION = array(); // define it with empty array and clear the session values
or use start the session again and then destroy
session_start();
session_destroy();
For more :- why session_destroy() not working
and for better understanding you can read #Chen Asraf answer
From the PHP documentation of session_destroy:
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
So in order to truly get rid of the session, you also have to unset or override the $_SESSION superglobal, like you did before.
i need to avoid the creation of the session in the application unless it is completely necessary.
I have noticed that xajax calls dont work properly if the session is not started :(. My first approach was to create the session (if it doesn't exist) at the begining of the xajax function, however, it doesn't work the first time the user invokes the call (it works the second time since the session was created).
There is any way to handle/fix this situation?
Edit: an example code:
function example ($parameters) {
if (!isset($_COOKIE["PHPSESSID"])) {
session_start(); // we create the session if it didn't exist previously
}
$response = new XajaxResponse();
.....
return $response;
}
My idea is to create the session when the user makes an ajax call. With this situation, the first time i call the "example" function it doesnt work. The second it goes ok, i think because the session was created.
EDIT: Hello, i have noticed a problem under chrome and explorer :(. The first ajax call is not received (i get not answer). Than means the user needs to click twice in order to receive the properly answer (with a popup for example)
Thanks!
The issue seems to be the fact that you are not calling session_start() if $_COOKIE['PHPSESSID'] is set, and consequently the session is not initialized for the current ajax request. You must call session_start() on every script that uses the session -- it isn't just for session initialization.
function example ($parameters) {
// If this function uses the session, you MUST call session_start()
// Don't do it conditionally.
session_start();
$response = new XajaxResponse();
.....
return $response;
}
If all your ajax handler functions make use of the session, then you might as well just call session_start() at the top of the file that contains them. If you don't want the session loaded prior to ajax calls, then segregate them into their own PHP script, while you do not call session_start() on the main script they initially load.
For session you have to reload page without reloading page session can not create.....
And Ajax is useful for without reloading the whole page.
Ok, this is starting to annoy me, as it's quite simply and works elsewhere, but on this current task it doesn't, so here I go!
There is a main page which relies on either a session variable being set or not to display certain information.
Let's say this page is located here: http://dev.example.com/some_page.php
e.g.
if (isset($_SESSION["some_var"])) { /* it's set so do whatever */ }
else { /* not set so do whatever else.. */ }
There is an ajax page triggered by jQuery $.ajax() to call and set this session variable to null to change the action of the main page, let's say it's located here: http://dev.example.com/ajax/some_ajax_page.php
It's code looks like so:
<?php
if (!isset($_SESSION)) session_start();
$_SESSION["some_var"] = null;
When the main page is reloaded after the ajax is triggered, the session var "some_var" is still intact, but if it's echoed after the "null" in the ajax page then it is set to "null".
Basically it doesn't seem to write to the global session, only to the local path.
Does this make sense?
Any help please? Also if you want more clarification with anything let me know!
The session_start() function will handle the attempt to create and persist a session for you, as defined by PHP's configuration, or optionally at runtime if you set your own save handler. Make sure you read the documentation here:
http://us2.php.net/manual/en/function.session-start.php
For your code, you want to make sure to call session_start() at the beginning of any page in which you'd like to save or access session variables. So your page above may look like:
<?php
session_start();
$_SESSION['myvar'] = 'some value';
Then in a different page you can try to access that value:
<?php
session_start();
if ($_SESSION['myvar'] == 'some value') {
// do something
}
That should work fine.
Get rid of the check for session. If this is the only file your calling just do this:
<?php
session_start();
$_SESSION["some_var"] = null;
Also, are you using framework that auto-regenerates session ID on each request? If so, you'll might have problems.
If you have a dev machine to play with and permissions to do so, you can manually delete all sessions in the /var/lib/php/session/ directory. As you use your site, only one session file should be created. You can also inspect that file to see what is getting written and when.
Seems that you are using different sessions vars. One for the AJAX call and another for the normal pages calls. This may occur when you do not init both call in the same way (or using the same starting code that initializes the sessions)
Be sure to session_start() both calls using the same session_id.
// try in both calls
session_start();
echo session_id(); // must return the same id in both calls
Why don't you use unset? It is the proper way to do it.
Turns out the application I was working on had it's own session_handler and if it was not included before requesting the session data, it was always invalid, eventhough it was the same session_id.