php - 将登录会话用户名/用户 ID 存储在本地存储中,然后在页面之间传递它是否安全?
问题描述
我正在使用以下脚本来验证我的移动应用程序。我知道如何使用 php hash 和 verify() 函数来散列用户密码。
当用户登录时,我将用户用户名/用户 ID 存储在本地存储中,然后将其从页面传递到页面以用于其他数据库查询。此实施是否安全。攻击之类的呢
1.) 直接页面访问,因为本地存储使用 javascript。攻击者将无法修改本地会话用户名。
2.) 会话固定攻击/会话劫持怎么样。本地存储会保护我免受这种影响。
3.) 如何对welcome.html 页面中的表单实施跨站请求伪造攻击。
4.)通过身份验证保护我的移动应用程序的最佳方法是什么。我听说过令牌,但不知道它如何与移动应用程序一起使用。
<script>
$(document).ready(function(){
$('#login').click(function(){
var username = localStorage.getItem("username");
var password = $('#password').val();
if(username==""){
alert('please Enter Username');
}
else if(password==""){
alert('please Enter Username Password');
}
else{
var datasend = "username="+ username + "&password=" + password;
$.ajax({
type:'POST',
url:'login.php',
data:datasend,
crossDomain: true,
cache:false,
success:function(msg){
$('#alertbox_img').fadeIn('slow').prepend(msg);
$('#alerts_img').delay(5000).fadeOut('slow');
}
});
}
})
});
</script>
登录.php
<?php
error_reporting(0);
?>
<?php ob_start(); ?>
<?php
/*
please I know how to use php Password hash and verify function(). so forget about that
*/
require('db.php');
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$result = $db->prepare('SELECT * FROM users where username = :username and password = :password');
$result->execute(array(
':username' => $username,
':password' => $password
));
$count = $result->rowCount();
$row = $result->fetch();
if( $count == 1 ) {
echo "<div class='alert alert-success'>login sucessful</div>";
echo "<script>window.location='welcome.html'</script>";
}
else {
echo "<div class='alert alert-danger' id='alerts_img'><font color=red>Either Username or Password is Wrong</font></div>";
}
?>
<?php ob_end_flush(); ?>
欢迎.html
<script>
if (localStorage.getItem("username") === null) {
alert('Requesting page directly is not allowed.');
window.location='index.html'
}
</script>
<script>
$(document).ready(function(){
$('#payment').click(function(){
var username = localStorage.getItem("username");
var amount = $('#amount').val();
if(amount==""){
alert('please Enter amount');
}
else{
var datasend = "username="+ username + "&amount=" + amount;
$.ajax({
type:'POST',
url:'payment.php',
data:datasend,
crossDomain: true,
cache:false,
success:function(msg){
alert('payment successful');
}
});
}
})
});
</script>
<input type="text" id="amount">
<button id="payment">Pay Now</button>
有人可以帮我解释并在必要时修复安全漏洞。谢谢
解决方案
推荐阅读
- html - 放置静电旁边的带引导网格宽度
- ios - 如何从 iOS(swiftui)中的应用程序共享我的应用程序的链接?
- android - 列表未从 Activity 传递给 Recycler Adapter
- java - 从多个线程向 SoftAssertions 添加失败
- python - 使用 Xarray 从 netCDF 文件中提取数据到高 DataFrame 的有效方法
- java - 每次创建新实例时,ArrayList 都会被覆盖
- javascript - 如何将 2D 实时动态文本添加到 Three.js 场景?
- javascript - React - 并排显示图像
- python - 如何打印与两个不同列表中的不同值相对应的值?(Python)
- reactjs - 如何在 setState 之后 localStorage 设置项目?