본문 바로가기
PHP 박살내기/php ajax json

php ajax mysql 특정유저 차단하기 기능 구현

by 인생여희 2017. 10. 2.
반응형

php ajax mysql 

특정유저 차단하기 기능 구현


#화면



#login.php


<?php
include('database_connection.php');


$message = '';

if(isset($_POST["login"]))
{
if (empty($_POST["user_email"])|| empty($_POST["user_password"])) {
$message = "<div class='alert alert-danger'>빈칸을 채워주세요</div>";

} else {
//해당 이메일이 있는지 찾기
$query = "
select * from user_details2
where user_email = :user_email
";

$statment = $connect -> prepare($query);
$statment -> execute(
array(
'user_email' => $_POST["user_email"]
)
);

$count = $statment -> rowCount();
if($count > 0) //email 이 있으면 실행
{
$result =$statment -> fetchAll();
foreach($result as $row)
{
//해당 이메일 유저 상태가 active 이면
if($row["user_status"] == 'Active')
{
//입력한 비밀번호와 db에 해슁으로 저장된 비번 매칭
if(password_verify($_POST["user_password"], $row["user_password"]))
{
$_SESSION["type"] = $row["user_type"];
header("location:index.php");
}else
{
$message = '<div class="alert alert-danger">잘못된 비밀번호 입니다</div>';
}


}else
{
$message = '<div class="alert alert-danger">계정이 정지 당했습니다</div>';
}
}
}else
{
$message = "<div class='alert alert-danger'>잘못된 이메일 주소 입니다</div>";
}

}


}


?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>유저 관리</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->


</style>
</head>
<body>
<br>

<div class="container">
<h2>유저 관리하기</h2>
<br>

<div class="panel panel-default">
<div class="panel-heading">로그인</div>
<div class="panel-body">
<form method="post">
<span class="text-danger"><?php echo $message; ?></span>
<div class="form-group">
<div class="form-group">
<label>이메일</label>
<input type="text" name="user_email" id="user_email" class="form-control" />

<div class="form-group">
<label>비밀번호</label>
<input type="password" name="user_password" id="user_password" class="form-control" />
</div>

</div>
</div>

<div class="form-group">
<input type="submit" name="login" id="login" class="btn btn-info" value="login" />
</div>
</form>
</div>

</div>

</div>


</body>
</html>

#index.php


<?php
include('database_connection.php');


if(!isset($_SESSION["type"])){
header("location:login.php");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>유저 관리</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->


</style>
</head>
<body>
<br>

<div class="container">
<h2 align="center">유저 관리</h2>
<br>

<div align="right">
<a href="logout.php">로그 아웃</a>
</div>
<br>

<?php
if($_SESSION["type"]=='user'){

echo '<div align="center"><h2>hi... welcome !</h2></div>';
}else
{
?>
<div class="panel panel-default">
<div class="panel-heading">user status</div>
<div class="panel-body">
<span id = "message"> </span>
<div class="table-responsive" id="user_data">

</div>
</div>

</div>


<?php
}
?>
<script>

$(document).ready(function(){
load_user_data();
function load_user_data(){

var action = 'fetch';
$.ajax({
url:"action.php",
method:"POST",
data:{action:action},
success:function (data){
$('#user_data').html(data);
},error:function(request,status,error){
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}


})

}

//설정 버튼 눌렀을 때
$(document).on('click', '.action', function(){


//juqery 데이타 속성으로 값을 가져온다!
var user_id = $(this).data('user_id');
var user_status = $(this).data('user_status');
var action = 'change_status';
$('#message').html('');

if (confirm("정말 변경하시겠습니까?"))
{
$.ajax({
url:"action.php",
method:"POST",
data:{user_id:user_id,user_status:user_status, action:action},
success:function(data){

if(data != '')
{
load_user_data();
$('#message').html(data);
}

}
})
}
else
{
return false;
}
});


});

</script>

</div>

</body>
</html>


#logout.php

<?php


session_start();
session_destroy();
header("location:login.php");


?>

#database_connect.php

<?php

$connect = new PDO('mysql:host=localhost; dbname=open','root','비번');


session_start();

?>

#action.php

<?php

if(isset($_POST["action"]))
{
include("database_connection.php");

if($_POST["action"]== 'fetch')
{
$output = '';
$query = "select * from user_details2 where user_type = 'user' order by user_name asc";
$statment = $connect -> prepare($query);
$statment ->execute();
$result = $statment -> fetchAll();

$output .= '
<table class="table table-bordered table-striped">
<tr>
<td>이름</td>
<td>이메일</td>
<td>상태</td>
<td>설정</td>
</tr>
';

foreach($result as $row){

$status = '';
//상태가 활성화 이면
if($row["user_status"]=='Active')
{
$status = '<span class="label label-success">활성화</span>';
}else
{
$status = '<span class="label label-danger">비활성화</span>';
}
$output .= '
<tr>
<td>'.$row["user_name"].'</td>
<td>'.$row["user_email"].'</td>
<td>'.$status.'</td>
<td><button type="button" name="action" class="btn btn-info btn-xs action"
data-user_id="'.$row["user_id"].'" data-user_status="'.$row["user_status"].'">설정</td>
</tr> ';
}
$output .= '</table>';
echo $output;
}


//설정 버튼을 눌렀을 때

if($_POST["action"]=='change_status')
{
$status = '';

//상태가 활성이면 비활성으로
if ($_POST["user_status"]=='Active')
{
$status = "Inactive";
} else
{
$status = "Active";
}

//업데이트
$query ='
update user_details2 set user_status = :user_status where user_id = :user_id
';
$statment = $connect ->prepare($query);
$statment->execute(

array(
':user_status' => $status,
'user_id' => $_POST["user_id"]
)
);
$result = $statment ->fetchAll();
//결과가 존재하면 알림창 띄우기
if(isset($result))
{

echo '<div class="alert alert-success">상태가 <strong>'.$status.'</strong>로 변경 되었습니다</div>';

}
}

}

?>


#db 쿼리

CREATE TABLE IF NOT EXISTS `user_details2` (

  `user_id` int(11) NOT NULL,

  `user_email` varchar(200) NOT NULL,

  `user_password` varchar(200) NOT NULL,

  `user_name` varchar(200) NOT NULL,

  `user_type` enum('master','user') NOT NULL,

  `user_image` varchar(150) NOT NULL,

  `user_status` enum('Active','Inactive') NOT NULL

) ;



반응형

댓글