core php session is lost in codeigniter when redirecting controller to controller - codeigniter

core php session is lost in codeigniter when redirecting from one controller to another.
In the first controller, it works perfectly when we assign value to it. Also, in the first controller, the session is correctly handled but after a redirect to another controller, the session is lost.
Here is first controller:
$_session['user'] = $data[0]['u_name']; // assign value to session
echo $_session['user'] // works fine here and print user name
redirect("useraccount",'refresh'); // redirection to user account
its not working in useraccount.php controller
here second controller
public function index() {
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}
it is printing no session. How do I pass the session to 2nd controller after redirecting
to second controller?
note: i want to use core php session and not codeigniter session.

Is there something wrong with:
$this->session->set_userdata('user', $data[0]['u_name']);

PHP variable names are case sensitive. it should be
if (isset($_SESSION['user'])) {
^^^^^^^--- note: all CAPS

Use session_start() property to access core php session
Try this
public function index()
{
session_start();
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}

Related

use laravel session value from function to another in the same controller in api or web route

When i'm trying to set session value in function then using this value it's fine but when i'm trying to reuse the same value which saved in the session with it's key i got undefined key in the array of the session why that's happen and how to handle that i tried to set middleware in the route but it gave me the same error
note: i redirect from my domain to another domain i know that's the problem occur after redirecting how to handle that i need standard solution i used $_cookie and it's worked but i want to use session ???
I'm tring to use the session values from function to another in the same controller after redirecting to another domain and back to my domain can someone tell me how to handle that and you must know that i don't controll the response from the external domain and this domain don't return the data that i want ???
the question here i need session to not expired if i go to this domain then back to my domain ?
try to capture the id after the creation
public function setCaptureOrderId($id) {
Session::put('catpureId', $id);
Session::save();
}
can use this getCaptureOrderId function
public function getCaptureOrderId() {
return $_SESSION['orderID'];
}
then redirect
public function http_create(){
return Redirect::to($url);
}
finally after the redirecting
getCaptureOrderId don't return anything and undefined key

problem YII2 session destroy is not working

i have a problem.
In Yii2 i have a view, controller, and layout.
in the layout and view, i check for the session if active do something.
the code is like this:
if (!isset($session)) $session = Yii::$app->session;
if ($session->isActive && $session->has('username')):
//some code here if there is session echo something
else:
//some code here if there is no session echo something else
endif;
then i have a controller action, which destroy session but also redirect to the view file above, the code is like this:
public function actionDestroysess($status = 1) {
Yii::$app->session->destroy();
return $this->render('masuk', ['status' => $status]);
}
the problem is, the session condition in view return true, it means that the Yii::$app->session->destroy(); is not working at all, i tried to var_dump(Yii::$app->session->get('username)); at the controller below the destroy command, but still get the value. Help me, why this session destroy is not working?
try this:
Change
Yii::$app->session->destroy();
to
Yii::$app->getSession()->destroy();
if you want to remove exact session you must use:
Yii::$app->session->remove('name')

Sessions in laravel 5.3 don't start

I am having problems with storing values in a session. I am using laravel 5.3 on xampp local host. I have tried the following but when I try to access the session is empty:
//using global session helper method
$variable = session(['exam'=>$exam]);
$exam = session('exam');
echo $exam; //exam is always empty
//I have also tried this
public function myControllermethod(Request $request)
{
$exam = $request->session()->put('exam',$exam);
//get the session
$exam = $request->session()->get('exam');
echo $exam // this approach returns the following exception
}
RuntimeException in Request.php line 905:
Session store not set on request.
I have tried both the file and database drivers without lack. Sessions are actually not being registered at all. Session::get('key') also do not work,
How can I start sessions in laravel 5.3. Any Help?

CodeIgniter session not working on Safari

I'm facing a problem in my CodeIgniter application. I'm using set_userdata() to put some information in the session from a specific controller method, and when I try to retrieve this information from another controller method using userdata() it returns empty when using Safari. It works ok when I use Chrome or Firefox.
Here is the controller code:
<?php
class Helloworld extends CI_Controller {
public function display()
{
$this->load->helper('url');
$this->load->library('session');
$this->session->set_userdata('some_name', 'some_value');
//echo $this->session->userdata('some_name');exit; //HERE IT WORKS
redirect("dashboard");
}
public function user_dashboard()
{
$this->load->library('session');
echo "redirect and session test - ";
echo $this->session->userdata('some_name');exit; //HERE IT FAILS
}
}
?>
Here is a snippet code from routes.php:
$route['dashboard'] = 'helloworld/user_dashboard';
So, when I access this URL
http://mydomainhere.com/CI/index.php/helloworld/display
It redirects to this one:
http://mydomainhere.com/CI/index.php/dashboard
The output in Chrome and Firefox is:
"redirect and session test - some_value"
The output in Safari is:
"redirect and session test - "
The strange thing is that it works on Safari if I try to retrieve the information in the session inside the same controller method where I put the information in the session. It doesn't work if I try to retrieve the information from another controller method, in this case I assign the information to the session inside the "display" method and I try to get it from "user_dashboard" method.
I appreciate any help.
Thank you guys.

Session value get deleted on page refresh in ci

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.

Resources