Is it possible to keep flash data after session destroy in codeigniter.i saw several answers on stackoverflow but none of them works.
Below is my logout method.
public function logout() {
$this->session->sess_destroy();
$this->session->sess_create();
$this->session->set_flashdata('success', 'You have been logged out successfully.');
redirect('welcome');
}
Getting error message
Fatal error: Call to undefined method CI_Session::sess_create()
I m using codeigniter 3.0.
Do nothing just remove or comment the bottom line and try:-
$this->session->sess_destroy();
// $this->session->sess_create();
Also set in config.php file
$config['sess_match_useragent'] = FALSE;
and read this link
You can set the cookie instead of a session
$this->load->helper('cookie'); // Call the helper
$this->input->cookie('logoutstatus', TRUE); //set the cookie
In the page where you want to display the message, check for that cookie
$this->load->helper('cookie'); // Call the helper again in that view page
$checkStatus = get_cookie('logoutstatus');
if($checkStatus){ //if its available
//display thge message and then delete the cookie
delete_cookie("logoutstatus");
}
NOTE: It would be a lot better if you just include the cookie helper in your autoload.php configuration.
Related
I am sending a mail and after mail is send I want to redirect user to a specific url
$mail = Mail::send('test.mail', ['a' => 'a'], function (Message $message) {
$message->to(['hod#vu.edu.pk', 'student#vu.edu.pk',]);
$message->from('stms#vu.edu.pk');
$message->subject('Test Mail');
});
// dd($mail);
return response()->redirectToRoute('allocate_supervisor')
->with('message', 'Supervisor Assigned and sent to HoD for approval.');
I have tried to return redirect()->route(), url(), and redirect('allocate_supervisor') but each time same issue.
dd($mail); works fine and shows output but on redirect it shows blank page.
Also request status code is 200.
Also tried
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
before return still no output
There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:
return redirect('/allocate_supervisor');
make sure 'allocate_supervisor' route must be present in your web.php file.
if you have named routes, you may use route() method.
return redirect()->route('/allocate_supervisor');
I was calling storePhase function from store function and returning response from storePhase function which was being overridden in store function and I was receiving blank page...
I moved this response code to store function and it worked fine.
In October CMS on the next request the session does not contain data previously set.
What I did is:
I am trying to use an action method of a plugin controller in October CMS to put data in the session.
\Session::put('name', 'Test Name');
\Session::keep(['name']);
var_dump(\Session::get('name'));
After that I redirect to a specific page of my application
return \Redirect::to('/created');
In this page a component is loaded which is supposed to display data.
However when I try to access the session data in the back-end of this component
var_dump(\Session::get('name')); echo '<br>';
the session does not contain the data I'd put previously at all.
I already
Checked whether the '/storage/framework/sessions' folder is writable.
Whether the session has started.
Checked whether there are no other requests between the controller action and the next page (component).
Set a longer session lifetime.
Checked whether it is 'the correct' session and it is since it contains data set by middleware.
Also tried to add flash messages with both:
session()->flash("message", "Registered successfully");
or
\Flash::success('Settings successfully saved!');
or
return \Redirect::to('/created')->with('message', 'Registered successfully');
I have my controller class in the following folder structure:
Plugin controller
And the action method looks as following:
public function create(\HttpRequest $request)
{
// When robot
if($request->input("recaptcha") != "success") {
// Sets a successful message
session()->flash("message", "Registered successfully");
session()->flash("status", "Success");
session()->flash("alert-class", "alert-success");
\Session::put('name', 'Test Name');
}
return \Redirect::to('/created');
}
Can anybody help?
I know this response is late, but I had the exact same issue and after many attempts, I found out the issue. You have to group your routes inside web middleware as follows:
Route::group(['middleware' => ['web']], function () {
// your routes here
});
I have the following session data which gets set just prior to logging in via facebook
session(['redirecturl'=>'someurl']);
Then in my facebook callback code, i have:
public function callback($provider)
{
$getInfo = Socialite::driver($provider)->user();
$user = $this->createUser($getInfo,$provider);
auth()->login($user);
return redirect()->to(session('redirect_url'));
}
The issue is, the value of session('redirect_url') is null at the callback area.
What can I do to resolve this issue? It seems like facebook is trashing the session data. If i can even get a reload of the calling page thatd be useful.
I'm implementing redirect to previous page after login and logout.
So in each methods of controller I've saved session like as follow.
$this->session->set_userdata('previous_page', current_url());
And after successful login and logout, I'm calling a library method as follows.
function redirect_to_previous_url() {
$url = base_url();
if($this->_CI->session->userdata('previous_page')) {
// Get previous_url
$url = $this->_CI->session->userdata('previous_page');
$this->_CI->session->unset_userdata('previous_page');
}
return $url;
}
But Its redirecting to base_url of the site. After checking the session value Its showing not found image path but not what I've saved it before.
I'm not able to find out what is the problem behind this.
Please help me to rectify and the work would be appreciated
Try this..
function redirect_to_previous_url() {
$url = base_url();
if($this->_CI->session->userdata('previous_page')) {
// Get previous_url
$url = $this->_CI->session->userdata('previous_page');
$this->_CI->session->unset_userdata('previous_page');
return $url;
}
return $url;
}
I would ensure the session was set. Like this;
if($this->_CI->session->userdata('previous_page')) {
show_error('The session is set');
}
If you don't see the error, the session isn't set. Then you know this isn't where the problem lies.
No need to store Previous URL in session.
In core php you can get previously visited URL in following server variable
$_SERVER['HTTP_REFERER'];
Same can be achieved in CodeIgniter as
$this->load->library('user_agent');
echo $this->agent->referrer();
I have a login function. When I login, the session gets saved. But when I refresh the page or redirect to another function, then the session (userdata) is shown blank. I have loaded the session library in autoload, but the userdata is deleted after every page refresh.
Here is my code.
public function index () {
$user = $this->input->post('user');
// after successful user checking
$this->session->set_userdata('user', $user);
// when I print session here,
print_r($this->session->all_userdata());
// session user gets print
}
But when I redirect to a function (suppose 'test'), then no any session is shown.
public function test() {
print_r($this->session->all_userdata());
die;
}
When you read the post value with $this->input->post('user'); and there isn't any post the function returns null and save this in the user value.
You have to check before setting.
if ($this->input->post('user')) {
$this->session->set_userdata('username', $this->input->post('user'));
}
I solved the problem. Actually, accidentally I had destroyed the session at the beginning of the code. So, my session was all destroyed. I removed the code and it's working fine.