简体   繁体   中英

How to create sessions using PHP?

I am developing an application on android, which has a web server and a MySQL database.

The database contains the user names and the passwords.

I would like to implement the login procedure and establish a session between the android device and the web server using PHP.

Thanks in advance...

From your question it seems like you just want to know how to do this in php, android or not. You need to have users and passwords (hashed) stored in your DB, for instance when the user registers an account with you.

session_start(); //Start the session.  Call on every page that will have the login.
$login = $_POST['login'];
$pass = $_POST['pass'];  //Get login and password the user has sent.

if (is_valid_user($login, $pass)) {
   //If user is valid, pass them along to the next page.
   $_SESSION['logged_in_user'] = $login; //Keep track of the username in the session
}
else echo "Not a valid login";

Okay... so for the login action you could have something like...

session_start();

if(isLoginCorrect())
{
    $_SESSION['logged_in'] = true;
}

Where isLoginCorrect is a function which verifies the username and password are correct. Then you can have some logic in your template files with stuff like this...

<?php if($_SESSION['logged_in']): ?>
    You are logged in
<?php else: ?>
    You aren't logged in
<?php endif; ?>

Then to logout you can use, session_destroy() or unset($_SESSION['logged_in']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM