Javascript 如何创建可以从多个应用程序使用的PHP登录web服务?
我使用php编写了以下身份验证web服务: authenticate.phpJavascript 如何创建可以从多个应用程序使用的PHP登录web服务?,javascript,php,html,http,authentication,Javascript,Php,Html,Http,Authentication,我使用php编写了以下身份验证web服务: authenticate.php <?php require('_includes.php'); $username = fetchPostParam('username'); $password = fetchPostParam('password'); // A higher "cost" is more secure but consumes more processing power $cost = 10
<?php
require('_includes.php');
$username = fetchPostParam('username');
$password = fetchPostParam('password');
// A higher "cost" is more secure but consumes more processing power
$cost = 10;
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
// this is so php can recognize / verify this later. "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
$salt = sprintf("$2a$%02d$", $cost) . $salt;
$hash = crypt($password, $salt);
//grab the username/password from the database
$mysqli = getMysqlConnection();
if (mysqli_connect_errno()) {printf("Connect failed: %s\n", mysqli_connect_error());exit();}
$query = "SELECT * FROM usercreds where username = '{$username}'";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$dbusername = $row["username"];
$dbhash = $row["hash"];
}
$result->close();
}
$mysqli->close();
if($username == $dbusername && password_verify($password, $dbhash))
{
// ????????????????????
}
else
{
// ????????????????????
}
?>
为什么不在PHP中实现OAuth,请查看以了解更多详细信息。正如您所说,您希望将其用于多个web应用程序,因此我认为OAuth是最好的。为什么不在PHP中实现OAuth,请查看以了解更多详细信息。正如您所说,您希望将其用于多个web应用程序,因此我认为OAuth是最好的。在您的操作中,您为您提供了web服务,这就是它重定向到该页面的原因。相反,您可以在您的操作中提供其他页面,从那里您可以调用您的web服务。在您的操作中,您为您提供了web服务,这就是它重定向到该页面的原因。相反,您可以在操作中提供其他页面,从那里您可以调用您的Web服务。
<form id="loginform" method="POST" action="https://localhost:8888/webservices/authenticate.php">
<input type="text" name="username" id="username_field" placeholder="Username"/><br />
<input type="password" name="password" id="password_field" placeholder="Password"/><br />
<button type="submit" id="login_button">Let's do this!</button><br />
</form>