{"id":281,"date":"2025-05-08T20:55:14","date_gmt":"2025-05-08T20:55:14","guid":{"rendered":"https:\/\/www.webmobkey.com\/blog\/?p=204"},"modified":"2025-11-16T10:21:54","modified_gmt":"2025-11-16T10:21:54","slug":"php-ile-guvenlik-kodu-captcha-uygulamasi-yapmak","status":"publish","type":"post","link":"https:\/\/www.webmobdesign.com\/blog\/tr\/php-ile-guvenlik-kodu-captcha-uygulamasi-yapmak\/","title":{"rendered":"PHP ile G\u00fcvenlik Kodu (Captcha) Uygulamas\u0131 Yapmak"},"content":{"rendered":"\n<p id=\"4b4a\">Web sitelerinde kullan\u0131c\u0131 etkile\u015fimli formlar yayg\u0131n olarak kullan\u0131l\u0131r. Ancak bu formlar, botlar ve k\u00f6t\u00fc niyetli yaz\u0131l\u0131mlar taraf\u0131ndan hedef al\u0131nabilir. Bu t\u00fcr tehditlere kar\u015f\u0131 al\u0131nan en etkili \u00f6nlemlerden biri CAPTCHA sistemidir. Bu yaz\u0131da, PHP ve JavaScript kullanarak basit bir g\u00fcvenlik kodu (CAPTCHA) do\u011frulama sistemi nas\u0131l yap\u0131l\u0131r, ad\u0131m ad\u0131m ele alaca\u011f\u0131z.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"bafe\">Sistemin Bile\u015fenleri<\/h4>\n\n\n\n<p id=\"5e95\">Bu proje \u00fc\u00e7 temel dosyadan olu\u015fuyor:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>index.php<\/code>&nbsp;\u2014 Kullan\u0131c\u0131 aray\u00fcz\u00fcn\u00fc i\u00e7erir.<\/li>\n\n\n\n<li><code>captcha.php<\/code>&nbsp;\u2014 Rastgele g\u00f6rsel g\u00fcvenlik kodu \u00fcretir.<\/li>\n\n\n\n<li><code>verify.php<\/code>&nbsp;\u2014 Kullan\u0131c\u0131n\u0131n girdi\u011fi kodu do\u011frular.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9015\">Tam \u00d6rnek Kod<\/h4>\n\n\n\n<p id=\"7b04\">A\u015fa\u011f\u0131da CAPTCHA do\u011frulama sisteminin t\u00fcm dosyalar\u0131n\u0131 tek seferde bulabilirsiniz:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"7be5\">\u00d6nemli Not:\u00a0<code>LiberationSans-Italic.ttf<\/code>\u00a0Yaz\u0131 Tipi Dosyas\u0131n\u0131 Eklemeyi Unutmay\u0131n<\/h4>\n\n\n\n<p id=\"a67b\">CAPTCHA sistemimizin \u00e7al\u0131\u015fabilmesi i\u00e7in PHP\u2019nin\u00a0<code>imagettftext()<\/code>\u00a0fonksiyonu arac\u0131l\u0131\u011f\u0131yla yaz\u0131y\u0131 g\u00f6rsele basabilmesi gerekir. Bu i\u015flem i\u00e7in bir\u00a0<strong>TrueType Font (.ttf)<\/strong>\u00a0dosyas\u0131na ihtiya\u00e7 duyulur. Bu y\u00fczden\u00a0<code>LiberationSans-Italic.ttf\u00a0<\/code>dosyas\u0131n\u0131 internet \u00fczerinden ayn\u0131 dizine indirmeniz gerekiyor.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"c393\"><code>index.php<\/code><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?php session_start(); ?&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;tr&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;G\u00fcvenlik Kodu&lt;\/title&gt;\n    &lt;script&gt;\n        function refreshCaptcha() {\n        fetch('captcha.php')\n                .then(response =&gt; response.json())\n                .then(data =&gt; {\n                    document.getElementById('captchaContainer').innerHTML = `\n                        &lt;img src=&quot;${data.image}&quot; \n                             alt=&quot;G\u00fcvenlik Kodu&quot;\n                             style=&quot;height:100px;object-fit:contain;&quot;\n                             class=&quot;img-fluid border rounded p-2&quot;&gt;`;\n                });\n        }\n        function submitForm(event) {\n            event.preventDefault();\n            var formArea = document.getElementById('form-area');\n            var userInput = document.getElementById('userInput');\n            var userValue = userInput.value;\n            fetch('verify.php', {\n                method: 'POST',\n                headers: {\n                    'Content-Type': 'application\/x-www-form-urlencoded',\n                },\n                body: 'userInput=' + encodeURIComponent(userValue)\n            })\n            .then(response =&gt; response.json())\n            .then(data =&gt; {\n                userInput.value = &quot;&quot;;\n                const messageDiv = document.getElementById('message');\n                if (data.success) {\n                    formArea.innerHTML = '&lt;p style=&quot;color:green;&quot;&gt;' + data.message + '&lt;\/p&gt;';\n                } else {\n                    refreshCaptcha();\n                    messageDiv.innerHTML = '&lt;p style=&quot;color:red;&quot;&gt;' + data.message + '&lt;\/p&gt;';\n                }\n            });\n        }\n        document.addEventListener('DOMContentLoaded', function () {\n            refreshCaptcha();\n        });\n    &lt;\/script&gt;\n    &lt;link href=&quot;https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.6\/dist\/css\/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;\n&lt;\/head&gt;\n&lt;body class=&quot;bg-dark w-75 mx-auto my-4&quot;&gt;\n    &lt;div class=&quot;card&quot;&gt;\n        &lt;div class=&quot;card-body&quot;&gt;\n            &lt;div class=&quot;container mt-5&quot;&gt;\n                &lt;div class=&quot;card shadow p-4&quot;&gt;\n                    &lt;h5 class=&quot;card-title mb-4 text-center&quot;&gt;G\u00fcvenlik Do\u011frulama&lt;\/h5&gt;\n\n                    &lt;form onsubmit=&quot;submitForm(event)&quot; class=&quot;text-center&quot; id=&quot;form-area&quot;&gt;\n                        &lt;div class=&quot;mb-3&quot; id=&quot;captchaContainer&quot;&gt;\n                            &lt;!-- Captcha resmi buraya gelecek (innerHTML ile) --&gt;\n                        &lt;\/div&gt;\n                        &lt;div class=&quot;mb-3&quot;&gt;\n                            &lt;button type=&quot;button&quot; onclick=&quot;refreshCaptcha()&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;\n                                Yenile\n                            &lt;\/button&gt;\n                        &lt;\/div&gt;\n                        &lt;div class=&quot;mb-3&quot;&gt;\n                            &lt;input type=&quot;text&quot; id=&quot;userInput&quot; class=&quot;form-control text-center&quot; placeholder=&quot;G\u00fcvenlik kodunu girin&quot; required&gt;\n                        &lt;\/div&gt;\n                        &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary w-100&quot;&gt;G\u00f6nder&lt;\/button&gt;\n                    &lt;\/form&gt;\n\n                    &lt;div id=&quot;message&quot; class=&quot;mt-3 text-center&quot;&gt;&lt;\/div&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"3955\"><strong>captcha.php<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?php\nsession_start();\n\nfunction generateSecurityCode($length = 6) {\n    $chars = 'asdfgjkljeruo1346789';\n    $code = '';\n    for ($i = 0; $i &lt; $length; $i++) {\n        $code .= $chars&#x5B;rand(0, strlen($chars) - 1)];\n    }\n    return $code;\n}\n\nfunction createSecurityImage($code) {\n    $fontSize = 40;\n    $angle = 0;\n    \/\/ \u0130\u015fletim sistemine g\u00f6re font yolu ayarla\n    $fontPath = __DIR__ . '\/LiberationSans-Italic.ttf'; \/\/ Linux i\u00e7in\n    \/\/ Yaz\u0131 tipi dosyas\u0131 kontrol\u00fc\n    if (!file_exists($fontPath)) {\n        die('Yaz\u0131 tipi dosyas\u0131 bulunamad\u0131: ' . $fontPath);\n    }\n\n    $bbox = imagettfbbox($fontSize, $angle, $fontPath, $code);\n    $textWidth = abs($bbox&#x5B;2] - $bbox&#x5B;0]);\n    $textHeight = abs($bbox&#x5B;7] - $bbox&#x5B;1]);\n\n    $padding = 20;\n    $width = $textWidth + $padding * 2;\n    $height = $textHeight + $padding * 2;\n\n    $image = imagecreatetruecolor($width, $height);\n    $white = imagecolorallocate($image, 255, 255, 255);\n    $black = imagecolorallocate($image, 0, 0, 0);\n    $gray = imagecolorallocate($image, 200, 200, 200);\n\n    imagefilledrectangle($image, 0, 0, $width, $height, $white);\n\n    for ($i = 0; $i &lt; ($width * $height) \/ 300; $i++) {\n        imagesetpixel($image, rand(0, $width - 1), rand(0, $height - 1), $gray);\n    }\n\n    for ($i = 0; $i &lt; 3; $i++) {\n        imageline($image, rand(0, $width), rand(0, $height),\n            rand(0, $width), rand(0, $height), $gray);\n    }\n\n    $x = intval(($width - $textWidth) \/ 2);\n    $y = intval(($height + $textHeight) \/ 2);\n\n    imagettftext($image, $fontSize, $angle, $x, $y, $black, $fontPath, $code);\n\n    ob_start();\n    imagepng($image);\n    $imageData = ob_get_clean();\n    imagedestroy($image);\n\n    return 'data:image\/png;base64,' . base64_encode($imageData);\n}\n\n\/\/ Kod \u00fcret ve session'a kaydet\n$securityCode = generateSecurityCode();\n$_SESSION&#x5B;'security_code'] = $securityCode;\n$imageSrc = createSecurityImage($securityCode);\n\nheader('Content-Type: application\/json');\necho json_encode(&#x5B;'image' =&gt; $imageSrc]);\n<\/pre><\/div>\n\n\n<p><strong>verify.php<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?php\nsession_start();\nheader('Content-Type: application\/json');\n\n$userInput = $_POST&#x5B;'userInput'] ?? '';\n$savedCode = $_SESSION&#x5B;'security_code'] ?? '';\n\n$response = &#x5B;];\n$_SESSION&#x5B;'security_code'] = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ23456789'), 0, 6);\nif (strtolower($userInput) === strtolower($savedCode)) {\n    \/\/ Yeni kod olu\u015ftur ve oturuma yaz\n    $_SESSION&#x5B;'security_code'] = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ23456789'), 0, 6);\n    $response = &#x5B;\n        'success' =&gt; true,\n        'message' =&gt; 'Do\u011frulama ba\u015far\u0131l\u0131!'\n    ];\n} else {\n    $response = &#x5B;\n        'success' =&gt; false,\n        'message' =&gt; 'G\u00fcvenlik kodu yanl\u0131\u015f!'\n    ];\n}\n\necho json_encode($response);\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Web sitelerinde kullan\u0131c\u0131 etkile\u015fimli formlar yayg\u0131n olarak kullan\u0131l\u0131r. Ancak bu formlar, botlar ve k\u00f6t\u00fc niyetli yaz\u0131l\u0131mlar &hellip; <a title=\"PHP ile G\u00fcvenlik Kodu (Captcha) Uygulamas\u0131 Yapmak\" class=\"hm-read-more\" href=\"https:\/\/www.webmobdesign.com\/blog\/tr\/php-ile-guvenlik-kodu-captcha-uygulamasi-yapmak\/\"><span class=\"screen-reader-text\">PHP ile G\u00fcvenlik Kodu (Captcha) Uygulamas\u0131 Yapmak<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-web-tr"],"_links":{"self":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":1,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/281\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}