By Dennis Feldbusch


2019-09-11 09:24:26 8 Comments

I'm writing a login app in Xamarin for Android and iOS.

I'm trying to save the php session ID on the server so that it is not necessary to send it to the server with every request.

Now everytime I do a POST request my session ID on Android is changing. When I test it on iOS or with Postman it is always the same. Why is it like that? And is there a solution or a workaround?

Here is my function which calls my login.php:

private async void Login(object sender, EventArgs e)
    {
        string username = UserLoginText.Text;
        string passwort = sha256(PassLoginText.Text);
        using (var client = new HttpClient())
        {
            var formContent = new FormUrlEncodedContent(new[]
            {
                    new KeyValuePair<string, string>("username", username),
                    new KeyValuePair<string, string>("passwort", passwort)
            });
            Uri uri = new Uri(urlAbfrage);
            client.Timeout = TimeSpan.FromSeconds(5);
            try
            {
                var response = await client.PostAsync(uri, formContent);
                var stringContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine(stringContent);
            }
            catch (Exception er)
            {
                await DisplayAlert("Fehler", er.Message, "Okay");
            }
        }
    }

My login.php:

<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];

include('conn.php');
if(isset($username) && isset($password)){
    $sql_query = "SELECT `User_ID`,`Passwd` FROM `user` WHERE `User_ID` = '$username' AND `Passwd` = '$password'";
    $result = $conn ->query($sql_query);
    if(mysqli_num_rows($result)>=1){

        echo "true";
        $_SESSION["loggedin"] = true;
        $_SESSION["login_user"] = $username;
    }else{
        echo "false";
        $_SESSION["loggedin"] = false;
    }
}else{
    echo "Missing input";
}
$conn->close(); 
?>

My function which calls the member.php to check if the session is the same:

private async void check_Button(object sender, EventArgs e)
    {
        using (var client = new HttpClient())
        {

            Uri uri = new Uri(urlcheck);
            client.Timeout = TimeSpan.FromSeconds(5);
            try
            {
                var response = await client.PostAsync(uri, null);
                var stringContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine(stringContent);
            }
            catch (Exception er)
            {
                await DisplayAlert("Fehler", er.Message, "Okay");
            }
        }
    }

And my member.php:

<?php
session_start();
if ($_SESSION['loggedin'] == true){
    echo "Welcome to the members area, " . $_SESSION['login_user'] . "!";
} else {
    echo "Please log in first";
}
?>

0 comments

Related Questions

Sponsored Content

46 Answered Questions

[SOLVED] Is there a unique Android device ID?

37 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2371659 View
  • 2314 Score
  • 37 Answer
  • Tags:   php arrays unset

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 208044 View
  • 2405 Score
  • 15 Answer
  • Tags:   php mysql database

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

94 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

27 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

13 Answered Questions

[SOLVED] How do I expire a PHP session after 30 minutes?

  • 2009-02-06 13:14:14
  • Tom
  • 910235 View
  • 1006 Score
  • 13 Answer
  • Tags:   php session cookies

Sponsored Content