如何使用PHP创建会话?

时间:2022-11-22 13:44:38

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

我正在android上开发一个应用程序,它有一个Web服务器和一个MySQL数据库。

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.

我想实现登录过程并使用PHP在Android设备和Web服务器之间建立会话。

Thanks in advance...

提前致谢...

2 个解决方案

#1


6  

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.

从你的问题看起来你似乎只想知道如何在php,android或不这样做。您需要在数据库中存储用户和密码(散列),例如当用户向您注册帐户时。

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";

#2


0  

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...

whereLoginCorrect是一个验证用户名和密码是否正确的函数。然后你可以在你的模板文件中使用这样的东西...

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

然后注销你可以使用,session_destroy()或unset($ _ SESSION ['logged_in']);

#1


6  

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.

从你的问题看起来你似乎只想知道如何在php,android或不这样做。您需要在数据库中存储用户和密码(散列),例如当用户向您注册帐户时。

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";

#2


0  

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...

whereLoginCorrect是一个验证用户名和密码是否正确的函数。然后你可以在你的模板文件中使用这样的东西...

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

然后注销你可以使用,session_destroy()或unset($ _ SESSION ['logged_in']);