CakePHP - database sessions, ajax not saving - cakephp

I am using sessions saved in the database. Works well. Lot of data relating to pagination, browsing history etc is stored perfectly within the database.
However, I notice that data sent to a controller using Ajax is not being stored successfully.
If I debug the session within the controller called by ajax, right after I have set the session vars, I see the values appear to be stored correctly in the session, but on subsequent requests, it transpires that the session vars have NOT been saved.
I have done some testing and have found that the problem disappears if I change back to using "php" for the session instead of "database".
I have eliminated pretty much everything from the mix - and it boils down to Cake not saving session data that is sent by ajax. Again, simply switching back to using "php" for sessions, and everything works perfectly.
I wonder if anyone has experienced anything similar?
CakePHP 2.4
Many thanks.

Well, in case anyone is interested, it turns out that the issue I was having was not strictly related to storing sessions in the database.
My application was making 2 ajax calls at the same time, both attempting to update the session. This was a bug / error on my part, and was causing other session-related issues too, such as returning 403 error status.
I removed the offending bug and all is now well.

Related

Mongodb data corruption from heroku app cause & prevention

I have a free heroku plan and a nodejs app on the heroku server. The nodejs app is built with meanjs, so the code for mongodb connections is exactly what you would find in the configuration files. I use a mongolab free mongo database to store the data. Occasionally (depending on how much I interact/change code I believe), the mongodb data is corrupted. I believe this to be true because I use a script to register names, and I can always log into them for awhile until I receive a no user/pass error. If I get this error and immediately create a new user, the user can successfully be logged in and out. All of the user data is still in the database. I also have a few other crud modules that use different collections in the same database, and I (so far) have not seen anything happen to that data, or anything to any of the data besides the password. I don't know where my error is possibly coming from, or what code is relevant, as I haven't touched the config files at all and to my knowledge haven't written any code that looks at user passwords at all. Also, my user object is occasionally empty (user = "") in the markup, but that bug was introduced after the original, I believe while I was trying to find out what was going on. Again, I don't have any clue though, so I included it just in case. Thanks!
After a lot of trial and error, I found the cause to my problem.
After I created these users, I go into my Mongolab account and manually edit the roles based on what module I'm working on (doing role based authentication). It is when editing the data that my passwords become corrupted. I don't know why, but I've pinpointed the problem to there. I've messed with some other data, with similar results.

Laravel: Gracefully Fallback if Session Database is Not Avaiable

Is there a middware, package, or general approach for having Laravel gracefully fallback to a session-less state if the session storage engine isn't available?
That is, let's say you have you a system using the database session engine. If that database goes down, Laravel's going to throw an exception whenever it can't connect to the database. I'd like a way to, instead, have Laravel not throw an exception, and just continue on without a working session engine.
(I realize this will mean careful coding on the application level to never assume sessions are available, but a pre thank you for all the warnings)
Use Case to Correct For:
Session storage system goes down temporarily (maintenance window, unexpected outage, etc).
Logged in user hits a page, sees Laravel error page because session engine can't connect
User is sad
I'd rather the user see some sort of normal web-page instead of a generic error message, even if that means we can't include stateful session data on the page.
That depends, Laravel does not persistently require a session engine to work, only on pages that actually use it. So that means that a fallback would basically not help - in fact an exception is the best thing Laravel can actually do to help you here.
Why? Because an exception can be cought and, if that is what you want to do (even though it makes little to no sense), be ignored.
Maybe I'm understanding you wrong, what exactly do you want to fall back to?
For me it's really hard to imagine how could it work and what you need it for. For example when you need user to be logged to access some page what should happen if session db or whole db is down? For me the only solution is show the user info that something gone wrong because it will be hard to pretend that website is working if it's not. So application throw exception, you catch it and display error page for user (and send site admin e-mail or sms)
If you would try to pretend you probably make your users angry because they would try to log in and they wouldn't be logged in without any info, so they would try 2nd time, 3rd time and finally they would think that your site is broken and would never come back again. In my opinion it's better to tell them something is wrong and "hey, come back here in about 2 hours"

appengine.google.com & objectify - working ok locally but not on gae?

I am developing a backend using Android Studio and Java.
I use objectify to manipulate entities in Google's Datastore.
The question is the following: everything works ok locally. I can call any of the endpoint and add, update and delete entities. However, anything to do with the same entities do not seem to work on GAE... Is there anything specific required for this to work?
Logs are fairly limited also but it appears all endpoints register properly.
The only thing meaningful out of the GAE and logs section is for return calls being set to 200 but nothing more....
Thoughts? Thanks.
I was looking at the google public issue tracker and saw your code posted there (I am also editing your answer to add said code). I can't help but notice that in your "test.html", you load the function from "localhost:8080". This will obviously break your code whenever you're using it on a client's machine, because it will try to load the function from HIS localhost, which won't have your function in it.
When you're running your code on your production server, you would need to use something like "your-end-point.your-application.appspot.com" to get your function.
Also, I think that you can ONLY pass around either JSON or "JavaBeans" object. And it seems the "Test" you are passing isn't a JavaBeans (it has a constructor with at least a parameter).
The fact it never loads the function properly could also explain why your Logs don't show.

Heavy database load when using CodeIgniter Session class?

After reading about how CodeIgniter handles sessions, it has me concerned about the performance impact when sessions are configured to be stored and retrieved from the database.
This is from the CI documentation: "When session data is available in a database, every time a valid session is found in the user's cookie, a database query is performed to match it."
So every AJAX call, every HTML fragment I request is going to have this overhead? That is potentially a huge issue for systems that are trying to scale!
I would have guessed that CI would have implemented it better: include the MD5 hash to cover both the sessionID+timestamp when encoding them in the session record. Then only check the database for the session record every X minutes whenever the sessionID gets regenerated. Am I missing something?
You can make your AJAX requests use a different controller, for example Ajax_Controller instead of MY_Controller. MY_Controller would load the Session class but the Ajax_Controller doesn't. That way when you call to your AJAX, it doesn't touch session data and therefore doesn't make any erroneous calls to the database that aren't necessary.
If you are autoloading the Session class, maybe you can try unloading it for the AJAX requests? I've never tried it but it's talked about here http://codeigniter.com/forums/viewthread/65191/#320552 and then do something like this
if($this->input->is_ajax_request()){
// unload session class code goes here
}

CakePHP based website becomes unresponsive in browser

I am working on a large website built over Croogo in CakePHP. Today, without any warning, the following started to happen: if making a few consecutive HTTP request, the site remains in loading mode, inside the browser. Any attempt to access any other URL from the same domain results in the same result.
The interesting part, now: if I delete the "CAKEPHP" cookie and then try reloading it, everything works fine, until it freezes again.
Notes:
this is happening client side. The site is responding in other clients
PHP goes to use 30% of the processor power for a very short time just before the site becomes unresponsive
This is application related - I've tested it on three different configurations and all acted the same
I've commented the code I was writing before this happened and still no change
An Apache restart also makes the website responsive in the web browser
There are absolutely no slow queries. The largest time recorded for a series of queries is 134 ms. Also PHP mostly only parses data, with no demanding operations
This is happening equally in scripts with only one query and one variable showing and scripts that are parsing large data sets
This was solved by telling CakePHP to store sessions into database instead of "php".

Resources