Hi all!Original Post
I'm trying to set a PHP $_SESSION["test"] variable by using Poststring from B4J, and then view the session on the webpage.
However the session remains empty. It is not set, even though the log displays the string in B4J.
To sum this up, the string Hello B4X! is sent to the php webpage set.php to set a session variable. At success I open the webpage from B4J to view the changes online (in this case the session variable remains to be empty as it is never set).
Somehow jOkhttpUtils2 v2.96 Poststring don't set the session variable. Any ideas for a solution to this?
Here's my example code
B4X:
Sub SendPostRequest
Dim sendjob1 As HttpJob
Dim var1 As String = "Hello B4X!"
Dim response As String
' Init okHttpUtils2
sendjob1.Initialize("", Me)
'Send a POST request
sendjob1.PostString("https://www.domain.com/set.php", "var1=" & var1)
Wait For (sendjob1) JobDone(sendjob1 As HttpJob)
If sendjob1.Success Then
'xui.MsgboxAsync("Data was successfully sent to server.", "")
response = sendjob1.GetString
Log("Log Start -----------------------------------------------------------")
Log(response & " " & CRLF)
Log("Log End -------------------------------------------------------------")
Else
response = sendjob1.ErrorMessage
End If
'WebView1.LoadHtml(response)
' Open external browser to view session variable
fx.ShowExternalDocument("https://www.domain.com/get.php")
sendjob1.Release
End Sub
set.php
B4X:
<?php
// Start the session
SESSION_START();
// Get incoming POST variable
$_SESSION["test"] = $_POST["var1"];
//$_SESSION["test"] = "Hey!";
echo "Incoming POST variable is : " . $_SESSION["test"] . "<br><br>";
echo "<br><br>Log session variables : <br>";
echo json_encode($_SESSION);
?>
get.php
B4X:
<?php
// Start the session
SESSION_START();
echo $_SESSION["test"] . "<br><br><br>";
echo "<br><br><br>";
print_r($_SESSION);
?>
Log
Log Start -----------------------------------------------------------
Incoming POST variable is : Hello B4X!<br><br><br><br>Log session variables : <br>{"test":"Hello B4X!"}
Log End -------------------------------------------------------------
get.php webpage (opened from B4J at success)
EDIT
Update
I wrote a B4XSessions Library to at least have a starting point from B4X to work with cookies and make own sessions. I've updated it to v0.5.
You can play around with it, modify it to your on liking. Hopefully improve it and post your contributions here. Let me know so I can put it in this first post.
Here's what you need. A domain server space to store PHP files on. For simplicity I will post them in code format. I hope you find this useful for whatever usage you might find. These PHP script templates is also baken into the library source code.
Do not store sensitive data like privacy information this way. If that is the case encrypt the data and store it in a database with security.
Anyway, this opens up for alot of uses. You can write network applications, pass gaming data for positioning, values, temporary information, verifications, you name it.
It's a lightweight way of storing information without using a database. It's an alternative to standard cookies. This gives you direct control from B4X, that can be useful.
B4XSessions Alternative methods:
Methods:
B4XSessions.GenerateUID
Generates a random UID (user identification number) This can be stored as a file in a cookie folder on your domain.
B4XSessions.Domain(DestinationString)
Returns the default path to your domain. Make sure you change the domain variable in the B4XSessions module for code to work.
B4XSessions.CookieFolder
Returns the domain cookie folder. Subfolder to store the cookie files on your server.
Make sure you change the cookie folder variable in the B4XSessions module for code to work.
B4XSessions.SETSession
Stores the generated UID cookie file on the server.
B4XSessions.UNSETSession
Removes the generated UID cookie file on the server.
B4XSessions.CHECKSession
Opens webpage (check.php) to search if a specific UID cookie file exists on the server.
Note this had to be hardcoded since POST data are lost whenever ShowExternalDocument is called.
(Needs to be solved).
Dependencies:
create.php
B4X:
<?php
// Recieve POST string to create Cookies subfolder
$CookieFolder = $_POST['CookieFolder'];
$CreateUID = $_POST['CreateUID'];
// Create Cookies folder
mkdir($CookieFolder);
// Change directory to Cookies folder
chdir($CookieFolder);
// Create Cookie File
$CookieCreate = fopen($CreateUID, "w")
?>
remove.php
B4X:
<?php
// Recieve POST string to create Cookies subfolder
$CookieFolder = $_POST['CookieFolder'];
$RemoveUID = $_POST['RemoveUID'];
// Remove files in Cookie folder
$dir = $CookieFolder . "/";
array_map('unlink', glob("{$dir}" . $RemoveUID));
?>
check.php
B4X:
<?php
$CookieFolder = "Cookies";
$CheckUID = "1627580073964893881104629331612202613825855710527480866101217920394995325";
if (is_file($CookieFolder . "/" . $CheckUID))
{
echo "File exists";
} else
{
echo "File not found";
}
?>
Default B4XSessions methods:
Credits to OliverA for updating this method.
B4XSessions.SendPostRequest(String)
Sends a POST request to the server. You need to pass a string with this call.
Dependencies:
set.php
B4X:
<?php
// Start the session
SESSION_START();
// Get incoming POST variable
$_SESSION["test"] = $_POST["var1"];
//Let's return our session ID
echo session_id();
?>
get.php
B4X:
<?php
session_id($_GET["session"]);
// Start the session
SESSION_START();
echo $_SESSION["test"] . "<br><br><br>";
echo "<br><br><br>";
print_r($_SESSION);
?>
unsetvar.php (called manually)
B4X:
<?php
// Start the session
SESSION_START();
unset($_SESSION["test"]);
echo "Session was successfully destroyed!";
?>
First thing to do is to go into B4XSessions module and change the variables for domain and cookie folder for your domain.
You can test the methods from the buttons in the GUI. I wish you a great time to explore this library and the many uses you might find.
The PHP scrips are part of the code so you can get it from there. v0.6 is the latest version which is well documented and should be very easy to dive into/use.
Special notes about v0.6:
You can now choose between two types of ID generators. My own and also Erel's GUID generator.
SendPostRequest (Default method) should now work to set session on your domain and open the ShowExternalDocument.
Corrections made from the previous versions. Cleaner GUI, easier to generate User ID's and change type of generator.
Check B4XSession module, run the code and read the log. Follow the instructions and you'll pick it up easily.
/Yours,
{ ThRuST }
Attachments
Last edited: