Search code examples
javascriptphpsessionappcelerator

PHP Session Issue, not working 100% of the time


I have a really weird issue, the PHP session sometimes returns Undefined index but not all the time. I don't know if anyone has had a similar issue, I haven't changed much regarding session start. I also checked the the available disk space and that seems to be fine for the session data. Has anyone had similar experiences?

<?php

session_start();
include_once "security_mobile.php";
include "session.php";
switch($var)
{
 case "get_name":
 echo $securitymob->get_name();
 exit;
}
?>

Security_mobile.php

function get_name(){
 return $_SESSION['customer']['name'];
}

This is where the first undefined occurs on customer, then after that it won't work on any others. but sometimes it does work which is what is bugging me!

Update on issue

I can fix the issue as soon as I remove this from my app, however I need to get this working.

connection.js

 var getdb = Ti.Network.createHTTPClient({
    onload : function(e) {
          var response = this.responseText;
          Ti.App.Properties.setString('tempDB', response);
    },
    onerror : function(e) {
        failed(e);
    },
    timeout : 5000,
    validatesSecureCertificate : false
   });
   getdb.open('POST', this.url, true);
   getdb.send({
        'action' : 'get_name',
        'device' : 'mobile'     
   });

could it be to do with return $_SESSION['customer']['name'], the $_SESSION['customer']['name'] is used within Login stage.


Solution

  • The answer was the HTTP client request was posting to fast, this was not giving time for a connection and session data to be established, hence why results where intermittent. To fix the issue i had to perform the next HTTP client once the first had finished. See this other answer for more details.

    https://stackoverflow.com/a/39877622/6371663