Merhaba arkadaşlar. Bu kodaman’daki ilk yazım. Lafı uzatmadan kodlara geçmek istiyorum.

Yazılan mesajları mesajlar.txt dosyasından okuyan, basit ama güvenli bir ziyaretçi defteri uygulaması yazacağız. Bu tip uygulamalar hazırlamak isteyen arkadaşlar da buradaki mantıktan yararlanabilirler.

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
<?php
session_start();
// post metodu ile mesaj gelmiş mi?
if (isset($_POST['mesaj'])) {
// oturum oluşturulmuş mu ve post ile gelen oturum bizim sessiondakine eşit mi?
if (isset($_SESSION['oturum']) && $_POST['oturum'] == $_SESSION['oturum']) {
// html temizliği
$mesaj = htmlentities($_POST['mesaj']);
// mesajlar.txt yi yazmak için aç
$fp = fopen('./mesajlar.txt', 'a');
// mesajı yaz
fwrite($fp, "$mesaj<br /><hr />");
//dosyayı kapat
fclose($fp);
}
}
// rastgele sayı üret ve md5le
$oturum = md5(uniqid(rand(), true));
//ve ürettiğin sayıyı sessiona koy
$_SESSION['oturum'] = $oturum;
?>
<form method="post">
<input type="hidden" name="oturum" value="<?php echo $oturum ?>" />
<textarea name="mesaj" /></textarea><br />
<input type="submit">
</form>
<?php
// mesajlar.txt yi oku
readfile('./mesajlar.txt');
?>