Skip to content

Commit 84f26b1

Browse files
first commit
0 parents  commit 84f26b1

File tree

10 files changed

+139
-0
lines changed

10 files changed

+139
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php-csrf.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Orkhan Shukurlu <orkhandev@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# PHP CSRF
2+
3+
## Kurulum
4+
```shell
5+
composer require orkhanshukurlu/php-csrf
6+
```
7+
8+
### Basit Kullanım
9+
`vendor/autoload.php` dosyasını dahil ettikten sonra `Csrf` sınıfını üretin. Sınıfı `new` anahtar kelimesiyle çağırdığınızda token üretecektir. Tokeni kullanmak için:
10+
```php
11+
use ahmetbarut\Csrf\Csrf;
12+
13+
require __DIR__ . "/vendor/autoload.php";
14+
15+
// Sınıfı ürettiğinizde token oluşturur. Herhangi bir istek yoksa üretilir, istek varsa token üretmez.
16+
$csrf = new Csrf;
17+
18+
// Üretilen token değerini getirir.
19+
$csrf->getToken();
20+
```
21+
### HTML İçinde Kullanım Ve Kontrol
22+
Burda kolay kullanım açısından form içinde helper fonksiyonları kullanılıyor. İki yöntemi de kullanabilirsiniz.
23+
24+
## ! Not :
25+
`hasToken` yöntemine gelen verileri olduğu gibi veriniz arkaplanda, `input[name=_token]` olarak bakacaktır.
26+
```php
27+
<?php
28+
use ahmetbarut\Csrf\Csrf;
29+
30+
require __DIR__ . "/vendor/autoload.php";
31+
32+
$csrf = new Csrf;
33+
if($_POST){
34+
$csrf->tokenHas($_POST); // bool
35+
}
36+
?>
37+
38+
<form method="POST">
39+
<?=csrf_field()?>
40+
<input type="text" name="test">
41+
<button>Gönder</button>
42+
</form>
43+
```
44+
45+
| Method | Hakkında |
46+
| :--- | :----:
47+
| __construct | Nesne üretildiğinde beraberinde `session`'u başlatır ve token oluşturur. |
48+
| tokenHas | Verilen tokeni, oluşturulan token ile karşılaştırır. |
49+
| getToken | Oluşturulan son tokeni döndürür.
50+
| csrf_field | İnput oluşturur ve token verir. Helper fonksiyonudur.
51+
| create_token | Yeni token oluşturur.

composer.json

Whitespace-only changes.

config/csrf.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

src/Csrf.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace OrkhanShukurlu\Csrf;
4+
5+
class Csrf
6+
{
7+
public static function check(array $request): bool
8+
{
9+
return isset($request['_token'], $_SESSION['_token']) && hash_equals($request['_token'], $_SESSION['_token']);
10+
}
11+
12+
public static function field(): string
13+
{
14+
return '<input type="hidden" name="_token" value="' . csrf_token() . '">';
15+
}
16+
17+
public static function token(): string
18+
{
19+
if (session_status() === PHP_SESSION_NONE) {
20+
session_start();
21+
}
22+
23+
if (!isset($_SESSION['_token'])) {
24+
$_SESSION['_token'] = bin2hex(random_bytes(40));
25+
}
26+
27+
return $_SESSION['_token'];
28+
}
29+
}

0 commit comments

Comments
 (0)