Android Question [ OkHttpUtils2 ] problem with php $_Session

Waldemar Lima

Well-Known Member
Licensed User
hello everyone !!
I'm trying to make a game of answers and questions, using php and OkHttpUtils2, but I have a problem with the requests ( Job.Download and Job.PostString )

B4X code :
B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Private GameTimer As Timer
End Sub

Sub EnterGame

    GameTimer.Initialize("GameTimer", 500)
    GameTimer.Enabled = True
   
End Sub

Sub GameTimer_Tick
   
    Dim j As HttpJob
    j.Initialize("", Me)
    'j.Download("http://localhost/getviews.php")
    j.PostString("http://localhost/getviews.php","")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
   
End Sub

output B4X:
B4X:
Waiting for debugger to connect...
Program started.
views = 1
views = 1
views = 1
views = 2
views = 2
views = 3
views = 4
views = 5
views = 6
views = 7
views = 8
views = 9
views = 10
views = 11
views = 12
views = 13
views = 14
views = 15
views = 16
meucu1
views = 17
views = 18
views = 19
helloworld
views = 20
views = 21
views = 22
views = 23
views = 24
views = 25
views = 26

repeat 3x number 1 , and 2x number 2 initially ...

PHP source :

PHP:
<?php

session_start();
 
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+1;
else
    $_SESSION['views']=1;
     
echo"views = ".$_SESSION['views'];

?>

Have a way to fix it ?

1593501245405.png
 

Sandman

Expert
Licensed User
Longtime User
Best guess, on top of my mind, is that the server is somewhat slow and doesn't finish the request fast enough (until the server "warms up" and manages to keep pace). So your B4X code sends the request several times before the server part gets to increase the variable.

This theory should be very simple to test by increasing the gametimer to a higher value. I would test with 1000 or perhaps even 5000, to see if the theory holds water.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
try lengthening the delay on the timer. also, read very carefully the documentation relating to $_SESSION for your particular version of php. for example, the $_SESSION variable can be reset (unbeknownst to you). there are other considerations, which may or may not be relevant here. start with your timer and see if there's a difference. you can always tweak it to your liking.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You could also do something like this:
B4X:
'You'll need to set up a global variable for this
'Public getviewsBusy As Boolen = False
Sub GameTimer_Tick
    If getviewsBusy then return
    getviewsBusy = True
    Dim j As HttpJob
    j.Initialize("", Me)
    Try
        'j.Download("http://localhost/getviews.php")
       j.PostString("http://localhost/getviews.php","")
       Wait For (j) JobDone(j As HttpJob)
       If j.Success Then
           Log(j.GetString)
       End If
    Catch
        Log(LastException)
    End Try
    j.Release
   getviewsBusy = False
End Sub
 
Upvote 0
Top