开发者

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开发者_JAVA百科...


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']);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜