http, php, session handling, each request gets a new sessionid

efc_dev

Member
Licensed User
Longtime User
I've got a little script, which should be used to handle two things
1. user login, save this in a php session
2. send back some information

But the demo doesn't work?
Every time a call the script i will get a new phpsession id.

Is it possible to change the location of the request object and not to use a New1(...) call?

the basic sourcecode
' wwwres webresponse
' wwwreq webreuest
'
B4X:
Sub Globals
   'Declare the global variables here.
   URL="http://domain/session.php"   
End Sub

Sub App_Start

wwwres.New1
wwwreq.New1(URL)
wwwres.Value = wwwreq.GetResponse

regex.New1("PHPSESSID=.[a-zA-Z0-9]+")
match.New1
match.Value=regex.Match(wwwres.Headers)
GC_Session_ID=match.String

Msgbox(GC_Session_ID)

wwwreq.New1(URL&"?do=2")' init with a value
     wwwreq.Method = "GET"
     wwwres.Value = wwwreq.GetResponse 'This line calls the server and gets the response.
      string = wwwres.GetString 'Get the Response string.
      Msgbox("2:"& string) ' return some value

wwwreq.New1(URL&"?do=3") ' read it from session
     wwwreq.Method = "GET"
     wwwres.Value = wwwreq.GetResponse 'This line calls the server and gets the response.
      string = wwwres.GetString 'Get the Response string.
      Msgbox("3:"& string) ' return nothing
     '--> i expect "some value" @ result

wwwres.Close

End Sub
PHP Script
PHP:
<?php
  if (session_id() == "") session_start();
  
  switch ($_GET['do']) {
      case 2:
        $_SESSION["some_variable"]="some value";
        echo $_SESSION["some_variable"];
      break;
      case 3:
        echo "Session".$_SESSION["some_variable"];
      break;
  }
?>

based on this thread:
http://www.b4x.com/forum/questions-help-needed/2994-reciving-cookie-how.html
 
Last edited:

efc_dev

Member
Licensed User
Longtime User
two steps are neccesary

1. Step
use http://domain/session.php?do=1 to get the PHPSESSID

PHP:
<?php
  if (session_id() == "") session_start();
  
  switch ($_GET['do']) {
      case 1:
        $_SESSION["some_variable"]="some value 123";
        echo session_id();
      break;
      case 2:
        echo "Session->".$_SESSION["some_variable"]."\n".session_id()."\n".$_GET['PHPSESSID'];
      break;
  }
?>

2. Step
always send the PHPSESSID to the script
B4X:
Sub Globals
   'Declare the global variables here.
   URL="http://domain/session.php"   
End Sub

Sub App_Start

wwwres.New1
wwwreq.New1(URL)
wwwres.Value = wwwreq.GetResponse

regex.New1("PHPSESSID=.[a-zA-Z0-9]+")
match.New1
match.Value=regex.Match(wwwres.Headers)
GC_Session_ID=match.String

Msgbox(GC_Session_ID)

wwwreq.New1(URL&"?do=1")' init with a value
     wwwreq.Method = "GET"
     wwwres.Value = wwwreq.GetResponse 'This line calls the server and gets the response.
      string = wwwres.GetString 'Get the Response string.
      'Msgbox("2:"& string) ' return some value

wwwreq.New1(URL&"?[B][COLOR="Red"]PHPSESSID[/COLOR][/B]="&string&"&do=2") ' read it from session
     wwwreq.Method = "GET"
     wwwres.Value = wwwreq.GetResponse 'This line calls the server and gets the response.
      string = wwwres.GetString 'Get the Response string.
      Msgbox( string) ' return nothing
     

wwwres.Close

End Sub

And that's it
 
Top