PHP Sessions problems - php

I'm working with a third party COM object from PHP and I want to save the object on a session variable to access it in future server calls.
If I save the object in session:
$_SESSION['collItem'] = $collItem;
I can access it's methods and properties through $_SESSION['collItem'] inmediately after definition.
But, in future calls to server, if I try to use $_SESSION['collItem'], I can't access it again.
I write here some more code to clarify.
Method to initailize COM object in my script "functions.php":
public function setAppData() {
try {
$appD = new COM('ASData.CASDataApp');
$appD->InitMasterData(true, 1, 91);
$appD->DateMask = 'ymd';
$_readDB = $appD->InitApp($this->readDB());
} catch (Exception $e) {
$err = 'Connection error: ' . htmlentities(addslashes(strip_tags($e->getMessage())));
$this->setError($err);
return false;
}
$appD->appPath = str_replace('\app\include', '', __DIR__);
$this->iniciarCollections($appD);
$this->appData = $appD;
}
Call to method from my script "edit_json.php":
require_once('functions.php');
if (!session_id()) {
session_start();
}
// We recover $mbw object saved in session and initialize COM object
if (isset($_SESSION['mbw'])) {
$mbw = unserialize($_SESSION['mbw']);
}
$mbw->setAppData();
$appData = $mbw->getAppData();
$_SESSION['appData'] = $appData;
If I try access $_SESSION['appData'] inmediately after COM initialization I can work with it without problems, but if I try next code in future server calls (whith $appData object saved in $_SESSION['appData']:
if (!session_id()) {
session_start();
}
$appData = $_SESSION['appData'];
// I can't work with $appData object if I don't initialize it again
Reinitializing COM object isn't a good solution for me because I lose all changes I made.

Use session_start() at the beginning of your script. You need it to retrieve session data from the server into the $_SESSION variable.
From http://php.net/manual/en/function.session-start.php :
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

You should edit your question and include your getAppData() method, as well, so we can do a better diagnostic. But what I can guess is that your getAppData() returns an object, and, in that case, when you have a code like
$appData = $mbw->getAppData();
$_SESSION['appData'] = $appData;
what PHP is doing is saving in $_SESSION['appData'] only a reference to the $mbw->getAppData() object, and not the real object.
If you want to store the real object in session, you would have to do the following:
$_SESSION['appData'] = serialize($appData);
and then, whenever you want to use the stored object, you would do:
if (isset($_SESSION['appData'])) $appData = unserialize($_SESSION['appData']);
on the beggining of every file that uses the $appData that was saved in the session.

Related

How to add and access array from session in PHP

I am new with php. I want store multidimensional associative array in session and also fetch the value of array from session. But I am not able to do this properly.
Below is my code. Thanks.
$myArray = array();
if ((!isset($_SESSION['game']))) {
echo 'the session is either empty or doesn\'t exist';
} else {
$myArray = unserialize($_SESSION['game']);
}
array_push($myArray, new MapResult($keyId, $colorCode, 'NP', $highlow, 'NP', $evenOdd, 'NP'));
session_start();
$_SESSION['game'] = serialize($myArray);
?>
You can't access $_SESSION variables before calling session_start. Also, your serializing of the data is probably pointless. Session data is stored server-side. Serializing would be used if you wanted to dump the session state to a file or to a database or something.
A word of advice...
Instead of littering your code with $_SESSION references, it helps to wrap access in reusable functions. Your code will look nicer and you'll be free to change how session data is stored/accessed at any time without having to refactor your entire app.
// must be called before reading/writing $_SESSION
session_start();
function session_is_initialized() {
return isset($_SESSION['game']);
}
function session_setup() {
$_SESSION['game'] = [/* some initial data */];
}
function session_add_item($item) {
array_push($_SESSION['game'], /* more data */);
}
Now you can write nice clean code
if (!session_is_initialized()) {
session_setup();
}
session_add_item(['some', 'data']);

Close session without writing or destroying it

Is there a way to close a PHP session without either writing or destroying it? Am I missing something or are there just two functions (session_write_close() and session_destroy()) which reset session_status() to PHP_SESSION_NONE? In other words, if I have an open session, can I simply close it without affecting the external session data, so it can be reloaded again.
You can do this by $_SESSION = null. The session is unavailable but the data remains (does not get deleted).
Consider you have data in the session:
<?php
session_start();
session_regenerate_id(); // get a fresh id
echo session_id();
$_SESSION['test'] = '12345';
print_r($_SESSION); // echoes the data in the session
Data: test|s:5:"12345";
Then in the next request:
<?php
session_start();
echo session_id() . "<br>"; // echoes the same id as of the last request
$_SESSION = null;
print_r($_SESSION); // echoes nothing
echo "<br>";
echo session_status(); // echoes 2 (PHP_SESSION_ACTIVE)
Data still the same: test|s:5:"12345";
(php-fpm 5.4.29, most recent nginx, memcached as session handler).
Well the data still can be written through $_SESSION['whatever'] = ... but not read. I'm not sure whether this is a good solution but I have to admit I still don't understand why or whatfor you need this.
As an alternative, you could implement a wrapper class for the session with a property $this->active = true; // or false:
class MySessionWrapper {
protected $active;
protected $data;
// getter setter for $this->active and $this->data ...
public function getData($var) {
if ($this->active !== true) {
throw new Exception('session disabled');
}
}
}
Some months later after raising an issue the two new methods are now available in PHP 5.6, but the documentation arrived only with 5.6.2.
session_abort() – Discard session array changes and finish session, see the docs.
session_reset() – Re-initialize session array with original values, see the docs.

File session: is it always generated?

Does Php always create a session file as soon as session_start() is called, even though there's nothing to keep track of (= no variable written in $_SESSION[])? If so, why?
Default PHP file-based sessions encode the session ID into the session file's filename. Since that's the only place the ID is kept normally, SOMETHING has to be kept to store the id. That means you'll get a file created, even if nothing is EVER written to $_SESSION.
In PHP-like pseudo code, basically this is occuring:
function session_start() {
if (isset($_COOKIE[ini_get('session.name')])) {
// session ID was sent from client, get it
$id = $_COOKIE[ini_get('session.name')];
} else {
// no session ID received, generate one
$id = generate_new_id();
setcookie(ini_get('session.name'), $id, ......);
}
$session_file = ini_get('session.save_path') . '/sess_' . $id;
if (file_exists($session_file)) {
// found a session file, load it
$raw_data = file_get_contents($session_file);
$_SESSION = unserialize($raw_data);
} else {
// brand new session, create file and initialize empty session
file_put_contents($session_file, serialize(array());
$_SESSION = array();
}
// lock the session file to prevent parallel overwrites.
flock($session_file);
}

How to save an object in PHP and access it everywhere?

I am new to PHP and I want to save an object from this class which I can access in my webservice this object hold a sessions id which can be used for calling an API:
MyObject.php:
class MyObject {
private $sessionId = '';
private function __construct(){
$this->sessionId = '';
}
public static function getInstance() {
if (!$GLOBALS['MyObject']) {
echo 'creating new instance';
$GLOBALS['MyObject'] = new MyObject();
}
return $GLOBALS['MyObject'];
}
public function getSessionsId() {
if ($GLOBALS['MyObject']->sessionId == '') {
// Do curl to get key (works)
if (!curl_errno($curlCall)) {
$jsonObj = json_decode($result);
$sessionID = $jsonObj->session_id;
$GLOBALS['MyObject']->sessionId = $sessionID;
}
}
return $GLOBALS['MyObject']->sessionId;
}
}
Webservice GetKey.php
include 'MyObject.php';
$instance = MyObject::getInstance();
echo $instance->getSessionsId();
The I visit the GetKey.php file it always echoes 'creating new instance'
what you store in $GLOBALS will be destroyed when the page is loaded... that variable is available only when the response is being created...
You don't save an object and use it everywhere (for all users) .. you are in php :) and you can store it anywhere... but if you're in a function then it will be destroyed when you are outside the function...
And in webservices you don't use session because it won't exist at next operation call
In the case that you want to store that variable for multiple requests of the same user you can store it in $_SESSION and not in $GLOBALS...
In PHP everything is destroyed when the user gets the response... (only session is an exception)... a session is like in java or any other language... you have it / user and you can modify it at request time... you don't have something like applicationScope and store there everything for all users... everything is being recreated at every request

The Right Strategy for PHP Session User in Ajax Application

I'm writing a Ajax/PHP web application. Most ajax calls are using accessing the user object which is stored in the session.
<?php
session_start();
function session_user()
{
static $session_user = null;
if (!isset($session_user))
{
if (isset($_SESSION['user']))
$session_user = unserialize($_SESSION['user']);
else
$session_user = new User();
}
return $session_user;
}
class User {
public $books_borrowed = array();
public function __construct()
{
}
function __destruct()
{
// store the user object in the session upon destruction
session_start();
$_SESSION[ 'user' ] = serialize( $this );
}
function authorise($user_id, $password)
{
// if the user_id and password match, load books_borrowed from the DB
...
}
function deauthorise()
{
session_destroy();
}
}
?>
Ajax calls access the user object like this:
return session_user()->books_borrowed;
Note that the user object stores itself upon destruction, which, as far as I can tell, happens just before the ajax call return.
The reason I'm storing the user object to the session every time the object is destroyed is that it contains other objects (books) that might change during ajax calls, and neither do I want the book object to 'know' about the user object (for reusability) nor do I want to bother with having to remember storing the user object whenever any information within it changes.
Can someone see anything wrong with this strategy?
Thanks
The main strategy when you make a design for brand new application with ajax is not to think of ajax like about something special. It is regular request performed by browser. Absolutely the same like when you open new page by typing url manually and pressing enter.

Categories