-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchat.php
92 lines (84 loc) · 2.48 KB
/
chat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<html>
<head>
<?php
header("X-XSS-Protection: 0");
$dsn = 'mysql:dbname=weakchat;host=localhost';
$user = 'weakchat';
$password = 'weakpa33w0rd';
try{
$dbh = new PDO($dsn, $user, $password);
}catch (PDOException $e){
print('Error:'.$e->getMessage());
die();
}
if(isset($_POST["content"],$_POST["name"],$_COOKIE["key"])){
$sth = $dbh->query(
"INSERT INTO data(name,hash,content) VALUES('".
$_POST["name"].
"','".
substr( hash("sha256",$_COOKIE["key"]) , 0, 10).
"','".
$_POST["content"].
"')"
);
}
?>
<meta charset="utf-8">
<title>チャットルーム</title>
<script src="sha256.js"></script>
<script>
window.onload = function(){
var flag = false;
if (document.cookie){ //Cookieの確認
var cookies = document.cookie.split("; ");
for (var i = 0; i < cookies.length; i++){
var str = cookies[i].split("=");
if (str[0] == "key"){
flag=true;
break;
}
}
}
if(!flag){ //Cookieがなかった
document.cookie = "key="+String(new Date().getTime()) +decodeURIComponent(location.hash);
}
//Hiddenの設定
document.getElementById("name").value = decodeURIComponent(location.hash).substr(1);
document.getElementById("hname").innerHTML = decodeURIComponent(location.hash).substr(1);
document.getElementById("post").action = "chat.php"+location.hash;
}
</script>
</head>
<body>
<center>
<h1>チャットルーム</h1>
<h2>ようこそ<span id="hname"></span></h2>
<hr>
<table border="1">
<?php
$sth = $dbh->query('SELECT content,name,hash FROM data');
while($row = $sth -> fetch(PDO::FETCH_ASSOC)) {
$name=$row{"name"};
$hash=$row{"hash"};
$content=$row{"content"};
?>
<tr>
<td><?php echo $name ?></td>
<td><?php echo $hash ?></td>
<td><?php echo $content ?></td>
</tr>
<?php
}
$sth = null;
$dbh = null;
?>
</table>
<hr>
<form id="post" method="POST">
<input type="text" name="content"><br>
<input type="hidden" name="name" id="name">
<input type="submit" value="送信">
</form>
</center>
</body>
</html>