Skip to content

Commit 9146c88

Browse files
authored
Update README.md
1 parent fb5bbb3 commit 9146c88

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

Diff for: README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,89 @@
11
# php_basic_csrf
2-
Simple CSRF control class with PHP
2+
Simple CSRF control class with PHP. With this php class you can generate and validate tokens that are disposable or refreshed on every page refresh. The generated tokens are encrypted with openssl for extra security, so you need the openssl extension on your php server.
3+
4+
## **Configuration**
5+
The class must be configured in order to run.
6+
7+
###### **Configuration Example**
8+
```php
9+
$csrf = new Csrf([
10+
'key' => 'SuperKey', // Key
11+
'secret' => 'SuperSecret' // Secret Key
12+
]);
13+
```
14+
**The Key and Secret values are used to encrypt the tokens when generating, so enter these values once and do not change them.**
15+
16+
## **Get()**
17+
It allows you to call the generated token so you can add it to your forms.
18+
19+
###### **Get() Example**
20+
```php
21+
$csrf->Get();
22+
```
23+
24+
###### **Get() Example Result**
25+
> C8/mA9vfc4ST1D8+hSVrjKOaA2Y+UcVYvIBaEbYXKTN45DQVe1+qO29ntVDqSx2p4Xp3MrjiTh8lihWSK0Uo6b2jUbWzO+8DbCIieY0wYwE=
26+
27+
## **Check()**
28+
It compares the token you have printed on your forms with the token registered in the session and checks its accuracy. Create a _csrf entry in your forms and print the value generated by the class using the Get() method.
29+
30+
###### **Check() Example**
31+
```php
32+
$csrf->Check($token);
33+
```
34+
35+
###### **Check() Result**
36+
> true/false
37+
38+
## **Reset()**
39+
Use this method to reset and regenerate the token after verifying the token. If you want, you can increase the security a little more by creating a new token every time the page is refreshed.
40+
41+
###### **Reset() Example**
42+
```php
43+
$csrf->Reset();
44+
```
45+
46+
###### **Reset() Result**
47+
> true/false
48+
49+
## **Example Form Usage and Controls**
50+
```php
51+
<?php
52+
53+
session_start(); // Start sessions.
54+
// Include the CSRF Class in your file.
55+
56+
// Configure the class.
57+
$csrf = new Csrf([
58+
'key' => 'SuperKey',
59+
'secret' => 'SuperSecret'
60+
]);
61+
62+
if($_POST){
63+
64+
$firstname = $_POST['firstname'];
65+
$_csrf = $_POST['_csrf']; // We get the _csrf value from the form.
66+
67+
// We verify the token from the form with the Check() method.
68+
if($csrf->Check($_csrf)){
69+
70+
$result = "Token is correct";
71+
$csrf->Reset(); // We reset the token.
72+
73+
}else{
74+
75+
$result = "Token is not correct";
76+
$csrf->Reset(); // We reset the token.
77+
78+
}
79+
80+
}
81+
82+
?>
83+
84+
<form method="POST" action="post.php">
85+
<input type="text" name="firstname"><br>
86+
<input type="text" name="_csrf" value="<?= $csrf->Get(); ?>"><br>
87+
<button type="submit">Submit</button>
88+
</form>
89+
```

0 commit comments

Comments
 (0)