formToken::getField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Class for quickly securing forms against CSRF attacks
4
 *
5
 * @author Sam Collins
6
 * @copyright 2015 Sam Collins
7
 * @link https://gist.github.com/MightySCollins/0096d193fdc4160565b3
8
 */
9
10
class formToken
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
11
{
12
    /**
13
     * Makes hidden form input using session form token.
14
     *
15
     * @return string
16
     */
17
    public static function getField()
18
    {
19
        return "<input name='token' value='" . $_SESSION['formtoken'][0] . "' type='hidden' />";
20
    }
21
22
        /**
23
         * Makes sure token in session is valid
24
         *
25
         * @return bool
26
         * @param string $curToken
27
         */
28
    public static function validateToken($curToken)
29
    {
30
        if (!isset($_SESSION['formtoken']) || !isset($curToken['token'])) {
31
            return false;
32
        } else {
33
            if ($_SESSION['formtoken'][0] == $curToken['token']) {
34
                return true;
35
            } else {
36
                return false;
37
            }
38
        }
39
    }
40
41
    /**
42
     * Sets token in session.
43
     */
44
    public static function generateToken()
45
    {
46
        $_SESSION['formtoken'] = array(sha1(mt_rand(0, 1000000)), time());
47
    }
48
}