I am storing some data in variables like this,
$dbs_office = $request->session()->get('dbs_office');
$reference = $request->session()->get('reference');
$request->session()->flush();
return view('confirmation', ['ref' => $reference, 'dbs_team' => $dbs_office]);
I was hoping that by storing some of the session data in their vars I would be able to persist the data past the flush, however the vars are empty when I check. Is there a way to store these bits of data for use in a view, but remove the session data totally?
Related
So I finally managed to save some session data in to the database.
Now a new problem arises. When I load the page once all data is being saved.
When I reload the page and I write something to the session all data that's already in the session is being replaced with this new data. This means that with each load all the data is being replaced for new data. This is giving me problems because I'm saving a order_id into the session when there is no order_id present in the session.
This is the code that writes the data into the database. And yes it says replace into. But because this is a custom handler how can i actually append data to this current session with out renewing everything. Also how can I replace old data for new data witch is already present in the session.
The code that handles the writing to the database.
function _write($id, $data) {
$access = time();
$id = $this->session_db->real_escape_string($id);
$access = $this->session_db->real_escape_string($access);
$data = $this->session_db->real_escape_string($data);
$sql = "REPLACE INTO sessions VALUES ('$id', '$access', '$data')";
return $this->session_db->query($sql);
}
Is there something that i can do to put all the data directly after session_start() in the session variable so it always has all the data and doesn't overwrite the data with a single value when I do something like:
$_SESSION['LAST_ACTIVITY'] = time(); ?
EDIT: Also when I try to echo the session directly after the session_start();
It gives me there error
Warning: session_start(): Failed to decode session object. Session has been destroyed in /var/www/vhosts/url/httpdocs/index.php on line 13
Is there something wrong with the serialized string I don't understand why it is giving me this error.
Thanks in advance, some help will be appreciated.
Ok so now everything is working. I kinda feel embarrassed for saying what the problem was. So ok the problem and the solution.
The problem was that the session serialized data was way to long for a regular text field in the database. My string was over 500.000 characters long regular text field in database supports until 65.000+ characters.
So I changed the field from "TEXT" to "LONGTEXT" now it has enough space to save the string. And now everything works properly.
Everybody thanks for your time so far.
I'am trying to create a cookie in ZF2 controller. When I am on a "hotelPage" the controller is supposed to create a cookie with the value of the hotel. I need to add dynamically data to the cookie depending on the hotelPage I am currently on. My code sofar:
$cookieData = $this->getRequest()->getCookie();
if(empty($cookieData->recently_viewed)){ // cookie doesn't exists
// get data from hotel object
$recentlyViewedHotel = array('hotelId' => $hotel->id, 'hotelName' => $hotel->name);
array_push($hotels, $recentlyViewedHotel);
System::setCookie("recently_viewed", json_encode($hotels) );
} else{ // cookie already created, add more values to the cookie
$recentlyViewedHotel = array('hotelId' => $hotel->id, 'hotelName' => $hotel->name);
System::setCookie("recently_viewed", json_encode($hotels) );
}
The output that I get is an array of one object that is always overwritten. Can anyone tell me what am I doing wrong?
By looking at the code and on the basis of what you're getting as output, I think the problem is with the System::setCookie function which you're using to set the cookie WHEN your cookie already exists. Try getting the contents of the cookie if the cookie already exists, json_decode it. Append the new values into your object and then call the System::setCookie function with the updated object.
Note: Obviously, you would need to do the above process, if there isn't any existing API which you can call to append the values to your existing cookie :)
Cheers!
I wan to use CI session in a external script and I got following data from database.
__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;
I have tried unserialize and unserialize(base64_decode($data)) but I am fail yet.
Please help to extract this data.
I got the solution here
So I have used session decode
session_decode('__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;');
So session decode stored all the encrypted data in normal php session.
Which I can access using: echo $_SESSION['ci_UserID'];
Well guys thanks for the help
If this is a session variable, you can use CodeIgniter's own session library. Consider the following code (in a controller):
$this->load->library('session'); // load the session library
$session_data = $this->session->all_userdata(); // get all session data
print_r($session_data); // print and get the corrresponding variable name, e.g. "item"
$var = $this->session->userdata('item'); // pick one that suits your needs, e.g. item
Sorry, I have read "the external script" only after having posted the code. This obviously only works in the CI framework.
For an external script you may need to have a closer look. The variables are separated by ";" and "|" and then serialized, so this might work (not tested):
$row = explode(';', '__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example#xyz.com";logged_in|b:1;'); // load the database row
$userid = explode('|', $row[1]);
$userid = unserialize($userid[1]); // now $userid holds the value "2"
I want each time a file is called, to make a new sub-array. So say if I have a URL like http://example.com?name=[INPUT] , every time it calls it will add a new element to the array. With $users being the main array. EA:
$users[0][name] = "John"
$users[1][name] = "Sally"
Each call to the input will create a new $users[INCRIMENTAL_KEY][name] value.
A simple solution would be
$user[]['name'] = $name;
Although if all you are storing is a single item it would be even simpler to do
$user[] = $name;
Although if you want to keep this information across page executions and not be effected by WHO is running the page, you are going to have to keep the information in a database and just add a new row to a table each time the script is run.
You need a persistent way to track the array's data, whether that be sessions, database or files (depends on how long you want persistence for and whether it should be public or per-user).
Session example:
session_start();
// Initialise an empty array
if (empty($_SESSION['users'])) {
$_SESSION['users'] = [];
}
// Add each name to the session array
if (isset($_GET['name'])) {
$_SESSION['users'][] = ['name' => $_GET['name']];
}
Session would of course be per-user and per-session. You could use cookies and/or sessions for a longer persistence per-user, or use flatfile/database storage for more control and non-ending persistence.
I have used sessionHandlerInterface to save the session in database. Everything works fine. but I want to get all the serialized data from the database like
SELECT data FROM session;
and want them to decode the data when i output those.
i have tried using session_decode() which is manipulating $_SESSION array itself which is causing trouble. I just want to get the serialized data and return the decoded data.
This is the sample session data saved in database in data column
fb_422782977793963_code|s:216:"AQAVKa4Q8sOuioW75V9Ls-cDUEizgJDX5ZkRVLArDlroFvvhasdwKvbyzKEwiMVrN7nc5ghMaw0W67jQu5kt_sc_Edm9sABzB5PakdkUpXr52AViTOltPfPBQHM9T-JoGOQ4gasdbssC8Xt93NKFvdV7XRZ7ZYGZqIsu5SFpfFBHK_hNGDdRVbnbe_xUZVP9WI4h0jDy";fb_422782977793963_access_token|s:111:"AAAGAhasdaAKL7hAvXRv6FHUj1Tk24r7j4NqDwWWUzmXDZA2Igsb1pHjuP4jbBRNKfeYzutm0MFmgxuKPg1n0NEbvZAXM3bwoNZBiZCgZDZD";fb_422782977793963_user_id|s:15:"100004835469598";picture|s:61:"http://m-static.ak.fbcdn.net/rsrc.php/v2/yo/r/sdIqmHJn-SK.gif";
It works fine with normal session handling, it reads and writes session to database as it should.
I want to get all the data of active sessions. if i use SELECT data FROM sessions. it returns the above session data(encoded) i want to get the decoded data of it.
The PHP serialize and unserialize functions can not be used to serialize and unserialize session data. Even if (by default - and only by default) the serialization might look similar, there is an important difference to those two functions that care about a single variable contents only:
Those [sessions] are a list of serialized values with their variable name.
(from: Serialized README)
So you would need to create your own a session_unserialize function that is able to decode the string (e.g. via session_decode) which is returned from your database. Take care that this needs everything in there, e.g. if the session contains serialized objects, the class definitions needs to be loaded.
An exemplary session_unserialize function could look like (adopted from: a session related answer):
function unserialize_session($data) {
$hasBuffer = isset($_SESSION);
$hasBuffer && $buffer = $_SESSION;
session_decode($data);
$session = $_SESSION;
$hasBuffer ? $_SESSION = $buffer : unset($_SESSION);
return $session;
}