Android Question OkHttpUtils2 & PHP login problem

queensoft

Member
I have this HTML / PHP form / login.
Yes, I know it is incomplete, but as far as I can tell, the rest is unimportant.
PHP:
..........................................................................
elseif($_SERVER["REQUEST_METHOD"] == "POST") {
            $error = false;
            $authenticated = 'failed';
            $username = test_input(strtolower($_POST["username"]));
            $password = test_input($_POST["password"]);
            
            foreach ($validUsernames as $key=>$value) {
                if ($value == $username && $validPasswords[$key] == $password) {
                  $authenticated = 'valid';
                  break;
                }
..........................................................................
      <?php if ($error === true) { ?>
        <div class="alert alert-danger" role="alert">
          Failure
        </div>
      <?php } ?>
      <?php if ($error === false) { ?>
        <div class="alert alert-success" role="alert">
          Success
        </div>
      <?php } ?>
..........................................................................
<form class="form-signin" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
      <img class="mb-4" src="mqtticon.png" alt="" width="72" height="72">
      <h1 class="h3 mb-3 font-weight-normal">Sign in</h1>

      <?php if ($error === true) { ?>
        <div class="alert alert-danger" role="alert">
          Failure
        </div>
      <?php } ?>
      <?php if ($error === false) { ?>
        <div class="alert alert-success" role="alert">
          Success
        </div>
      <?php } ?>
      
      <?php if ($error !== true && $error !== false) { ?>
        <label for="inputUsername" class="sr-only">Username</label>
        <input type="username" name="username" value="<?php echo $username;?>" id="inputUsername" class="form-control" placeholder="Username" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" name="password" value="<?php echo $password;?>" id="inputPassword" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
        <p class="mt-5 mb-3 text-muted">&copy; 2019</p>
      <?php } ?>
    </form>
..........................................................................

Then I have this B4A code.
Yes, the URL address is removed, sorry, I cannot share the working website link.
B4X:
#Region  Project Attributes
    #ApplicationLabel: AAA Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Dim j As HttpJob
    j.Initialize("j", Me)
    Dim values As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    


    ''''''''''''''''''''''' try 1
    values.Initialize
    values.Put("username","user")
    values.Put("password","pass")
    
    'j.PostMultipart("https://xxxxxxxxxxxxxxxx/index.php",values,Null)
    
    
    
    
    
    ''''''''''''''''''''''' try 2
    'j.Username = "xxx"
    'j.Password = "xxx"
    'j.PostString("https://xxxxxxxxxxxxxxxx/index.php", "")
    
    
    
    
    ''''''''''''''''''''''' try 3
    Dim userPwd As String = "user:password"
 
    Dim su As StringUtils
    Dim byt() As Byte = userPwd.GetBytes("UTF8")
    Dim suUserPwd As String = su.EncodeBase64(byt)
 
    j.Download("https://xxxxxxxxxxxxxxxx/index.php")
    j.GetRequest.SetHeader("Authorization:","Basic "&suUserPwd)
    
    
    
    ''''''''''''''''''''''' try 4
    'j.Download2("https://xxxxxxxxxxxxxxxx/index.php", Array As String("username","xxx","password","xxxx"))
    
    
    
    wait for (j) JobDone(job As HttpJob)
    If job.Success Then
        Dim response As String = job.GetString
        Log(response)
        xui.MsgboxAsync(response, "PHP response")
    Else
        Log(job.ErrorMessage)
        xui.MsgboxAsync(job.ErrorMessage, "PHP error")
    End If

End Sub

As you can see, I tried 4 different versions, none of the working.
PHP should return Failure or Success.
But, tries 1, 2 and 4 return the entire HTML page.
Try 3 returns stream was reset, protocol error.

Any ideea would be great. Thank you.
 

Attachments

  • Screenshot_20210124_174317_b4a.example.jpg
    Screenshot_20210124_174317_b4a.example.jpg
    128.4 KB · Views: 210
  • Screenshot_20210124_174558_b4a.example.jpg
    Screenshot_20210124_174558_b4a.example.jpg
    458.7 KB · Views: 213

queensoft

Member
1. Site / webpage is already built.
There's even more than this, after login there are lots of other operations.

2. Can't, see 1.

3. I'll check it.
How do I do that, just in case I don't figure it out ?? :)

Thank you.
 
Upvote 0

queensoft

Member
B4X:
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Signin</title>
~l10327724:
    <!-- Bootstrap core CSS -->
    <!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> -->
    <link rel="stylesheet" href="bootstrap.min.css">
~l10327724:
    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
      }
~l10327724:
      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
    <!-- Custom styles for this template -->
    <link href="style.css" rel="stylesheet">
  </head>
  <body class="text-center">
    
    <form class="form-signin" action="/tuer/index.php" method="post">
      <img class="mb-4" src="mqtticon.png" alt="" width="72" height="72">
      <h1 class="h3 mb-3 font-weight-normal">Sign in</h1>
~l10327724:
                  
              <label for="inputUsername" class="sr-only">Username</label>
        <input type="username" name="username" value="" id="inputUsername" class="form-control" placeholder="Username" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" name="password" value="" id="inputPassword" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
        <p class="mt-5 mb-3 text-muted">&copy; 2019</p>
          </form>
</body>
</html>
 
Upvote 0

Num3

Active Member
Licensed User
Longtime User
Some of the html part is always returned, because it is not inside the <?php ?> tags (all html code should be inside the <?php /?> and printed or echoed by the php script ).

(untested):
      <?php
        if ($error === true) {
        echo '<div class="alert alert-danger" role="alert">Failure</div>';
        }
        elseif ($error === false) {
        echo '<div class="alert alert-success" role="alert">Success</div>';
        }
      ?>
      <b>This will always show, it is outside the php script</b>
 
Last edited:
Upvote 0

Num3

Active Member
Licensed User
Longtime User
Yes, it returns html string, but it shouldn't.
The page is working OK in browser: login form, then succcess / faile and the other operations.

Yes. I have the entire URL in the B4A program: https://www.xxxxxxxx.com/tuer/index.php

What you receive on the android app is what is expected, it is the data and hardcoded html code that the php page generates and sends.
It will work on the browser, because it will render it as html.
So unless you change the php code, you will always have the same result.
 
Upvote 0

queensoft

Member
Note that your code is wrong. Move the HttpJob outside of Globals.

OkHttpUtils2 simply sends a request and returns the server response. It doesn't do anything else.

You need to call it with PostString instead of Download.
PostString is try 2, same problem.
I will try what you suggested.
 
Upvote 0

queensoft

Member
What you receive on the android app is what is expected, it is the data and hardcoded html code that the php page generates and sends.
It will work on the browser, because it will render it as html.
So unless you change the php code, you will always have the same result.
I'll have to talk to the owner of the website. Thank you.
 
Upvote 0

queensoft

Member
Problem (partially) solved.
Using same PHP, but with this code:
B4X:
    ''''''''''''''''''''''' try 1
    values.Initialize
    values.Put("action","login") ' THIS IS NEW
    values.Put("username","user")
    values.Put("password","pass")
   
    j.PostMultipart("https://xxxxxxxxxxxxxxxx/index.php",values,Null)
It still returns some HTML code, but the FAIL / SUCCESS string is also present, near the end.
 
Upvote 0
Top