Facebook ajax losing session - ajax

I'm trying to develop a Facebook app but am hitting a problem with session variables.
I call a script via ajax which sets a session variable.
Later, I call a script via ajax which retrieves the previously set session variable, but the variable is empty.
The first script called when the app loads in index.php.
I have the following at the top
<?php
session_start();
//Get PHP SDK
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook( array('appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'cookie' => true, ));
$signed_request = $facebook -> getSignedRequest();
$like_status = $signed_request["page"]["liked"];
?>
The first script called via ajax sets the session variable like this:
$numbers = range(1, 52);
shuffle($numbers);
$_SESSION['cards'] = serialize($numbers);
The second script (which is called about 10 seconds later,so I don't think it will be a race problem) looks like this:
$numbers = unserialize($_SESSION['cards']);
The variable $numbers is empty even though no other script has emptied $_SESSION['cards'].
Should this work or am I doing it wrong? I'm very new to ajax.
I've read there can be problems with ajax calls in an iframe which is the way the facebook app is set up but I haven't managed to find a resolution.
Anyone know how to fix this?

you must call session_start() on both index.php and ajax script to share same session

Related

Joomla JFactory Class in module

I made a module which after receive &_POST the details from external server will select the tables from database and update the table cell. but: Between the 'if' statement, the jFactory Class is not working i mean i can not call username using $user->username but if i call the $user->username before the 'if' statement everything works fine. Could you help me with this and tell why its not working correctly
if($_SERVER['REMOTE_ADDR']=='IP NUMBER' && !empty($_POST)){
$xxx = $_POST['xxx'];
$zzz = $_POST['zzz'];
$yyy = $_POST['yyy'];
}
The post variables are received from external server
Have you any idea ?
I need to add that $_post from the exterran server is receive with a delay but module is active all time..

Firefox 26 on Windows 7 SP1 doesn't reset codeigniter session cookies

When a user logs into our site, a cookie gets set like so:
$this->input->set_cookie(self::AUTH_COOKIE, 'v1='.$var1.'&v2='.$var2, '1814400', ".oursite.com", "/", "", FALSE);
When they log out, the cookie is 'unset' like so:
----- UPDATED -----
$carray = array(
'name' => self::AUTH_COOKIE,
'value' => null,
'expire' => time() - 86500,
'domain' => '.oursite.com',
'path' => '/',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($carray);
delete_cookie(self::AUTH_COOKIE, ".oursite.com", "/", "");
----- END UPDATE -----
This works fine on most browser/platform combinations, but in Firefox 26 on Windows 7 SP1, the cookie never gets updated. It retains the data that was set when it was created. Any idea why this approach would not work in Firefox on Windows?
*----- Additional update - this no longer works on Firefox on my Mac and also fails on Internet Explorer. Haven't tested Safari or Chrome.
cfr. http://ellislab.com/codeigniter/user-guide/libraries/input.html:
To delete a cookie set it with the expiration blank.
For site-wide cookies regardless of how your site is requested, add your URL to the domain starting with a period, like this: .your-domain.com
the dot "." is missing in front of oursite.com:
.oursite.com
I think this last thing might be causing it.
If all else fails try with native PHP cookies to see whether that works.
I finally solved this with a potentially 3-part solution. Honestly I do not know whether steps 1 & 2 are necessary as they did not work on their own, but I do not wish to mess with my code now that it seems to be working.
I changed the expire value of the cookie array to null.
I unset a second cookie called sap_r that was set if the user had clicked 'Remember Me' when logging in.
My original code contained an ajax call immediately after setting the cookie values to null. I switched the order so that the ajax is called first and then the cookies are 'unset'.
I tested after each of these and it was after this final step that the cookies were successfully removed. So - I do not know if some combination of the three solved it or only the last one. If I get a chance to test further, I will comment here.

Meteor: unreliable session variable?

I'm making a service where there are no user accounts, and I want to restrict by what page I'm visiting.
So each page is a "box", and on each "box" I have a bunch of "files".
I've published the relevant info in server/publications.coffee
Meteor.publish 'files', (boxId)->
console.log boxId
return Files.find({boxId:boxId})
My file for 'box' has a subscription handle:
#filesHandle = Meteor.subscribe 'files', Session.get('currentBoxId')
And the currentBoxId is stored in the session variable.
Here's the crazy part: I expect this to work, and it does on the first time I start the server. The console.log in the first bit of code prints the proper ID. Then, all of a sudden the console log suddenly starts returning "null", even when I console.log the session var in the browser console, it returns correctly.
I feel like there's some kind of loading asynchrony issue here, but I have no idea what's going on.
Any clues?
Ah, figured it out. The template can be rendered before the session variable is set, apparently. Usually you put your collection handles in the main.js file in the application scope, but this doesn't work if the subscription depends on session variables.
I did the following:
Template.boxPage.created = ()->
#filesHandle = Meteor.subscribe 'files', Session.get('currentBoxId')

zend framework 2 Set TextDomain in onBootstrap

I followed the instructions of this link successfully, now my web is multilanguage without requiring put "locale" in the "traslate()" calls.
But I have to put the TextDomain each time that I call it.
$this->traslate("Hello", __NAMESPACE__) //where __NAMESPACE__ is the text domain.
I would like set TextDomain in the onBootstrap method instead of put it in each call of the the "traslate()" helper.
I have tried with setTextDomain method, but it doesn't exist.
Somebody know how do it?
The onBootStrap Code is following:
.....//Code for define $locale.
$sm = $e->getApplication()->getServiceManager();
$translator = $sm->get('translator');
$translator->setLocale($locale);
$traslator->SetTextDomain($textdomain); //This line not work!!!!!
Didn't see this right the first time. Going by DASPRIDS Presentation about ZF2 I18N the correct function to call is:
$this->plugin('translate')->setTranslatorTextDomain('module-b');
Though if i see this correctly, that's from within the view Scripts. Getting the Translator from ServiceManager however - i haven't tested this - but try the following:
$translator->getPluginManager()->get('translate')->setTranslatorTextDomain('foo');
Okey. We have advanced one step.
The first solution works ok (the view solution), now my web page traduce texts only using this helper parameters, being Locale and TextDomain defined by the config:
$this->translate('HELLO');
But the second solution not works. I don't understand because the same plugin is accepted in the view and not in the onBootstrap when the name is the same.
I rewrite my onBootstrap code bellow:
$translator = $e->getApplication()->getServiceManager()->get('translator');
$pm = $translator->getPluginManager(); //until here works ok.
$pm->get('translate'); //this throws an error message how if 'translate' not found.

Get environment inside controller

I have a situation in one of my controllers that should only be accessed via AJAX, I have the following code.
if (!$request->isXmlHttpRequest()) {
$response = new Response();
$response->setContent('AJAX requests only!');
return $response;
}
When I am testing this gives me an issue because the request hasn't actually been made via AJAX. This then breaks my tests every time. How should I go about working around this?
My Ideas:
I have tried to set a server header but have had absolutely no success.
Check if I am in the test environment in the controller and don't do the check if it is. I know this is dirty, but it would work. :-/ The problem was that I couldn't figure out how to discover what environment I am in.
Anyone else have any other ideas or tips that I am missing to get one of the above to work?
Of course in Icode4food's case, it's better to use Matt's solution, but here is how to find the current environment:
$this->container->getParameter(‘kernel.environment’)
Looking at the code for isXmlHttpRequest in class Request and method getHeaders in class ServerBag, the piece of code below should do the trick:
$client->request(
'GET',
'/path/to/test',
array(),
array(),
array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
)
);
I did not test it personally but I think it should works. The line of code below in Request is used to check if the http request is a XmlHttpRequest.
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
In the code, $this->headers is set using:
$this->headers = new HeaderBag($this->server->getHeaders());
The method getHeaders creates an array of headers. Each server variable starting with HTTP_, plus some special server variables like CONTENT_TYPE, are put in this array.
Hope this helps.
Regards,
Matt

Resources