'Using PHP sessions to open new android activity
I have created the following PHP in order to check if some username already exists inside a database.
If it exists and the username+password is correct, I had like to open a new activity in android, for example profile page.
The PHP code looks as follows:
<?php
session_start();
$DATABASE_HOST = "XX";
$DATABASE_USER = "XX";
$DATABASE_PASS = "XX";
$DATABASE_NAME = "XX";
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ( !isset($_POST['username'], $_POST['password']) ) {
die ('Please fill both the username and password field!');
}
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
}
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if ($_POST['password'] === $password) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
echo 'Welcome ' . $_SESSION['name'] . '!';
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
$stmt->close();
?>
So basically, if the login is successful, a session named "loggedin"
is created. However how can I use it in android to open new activity?
Something like:
if (session == "loggedin"){
context.startActivity(new Intent(context, profile.class));
}
I also tried to use the echo message for example:
if ($_POST['password'] === $password) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
echo 'Welcome ' . $_SESSION['name'] . '!';
} else {
echo 'good';
}
} else {
echo 'bad';
}
And then in my android studio to check if the result that I read from the echo was good
or bad
and based on it to open activity however Im sure there are better ways.
Thank you
Solution 1:[1]
I believe what you are looking for is Deep Linking.
Here's a link that will give you a short summary
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ivan Wooll |