1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * This file is part of Lenius Basket, a PHP package to handle |
||||
5 | * your shopping basket. |
||||
6 | * |
||||
7 | * Copyright (c) 2017 Lenius. |
||||
8 | * https://github.com/lenius/basket |
||||
9 | * |
||||
10 | * For the full copyright and license information, please view the LICENSE |
||||
11 | * file that was distributed with this source code. |
||||
12 | * |
||||
13 | * @author Carsten Jonstrup<[email protected]> |
||||
14 | * @copyright 2017 Lenius. |
||||
15 | * |
||||
16 | * @version production |
||||
17 | * |
||||
18 | * @see https://github.com/lenius/basket |
||||
19 | */ |
||||
20 | |||||
21 | namespace Lenius\Basket\Identifier; |
||||
22 | |||||
23 | use Lenius\Basket\IdentifierInterface; |
||||
24 | |||||
25 | /** |
||||
26 | * Class Cookie. |
||||
27 | */ |
||||
28 | class Cookie implements IdentifierInterface |
||||
29 | { |
||||
30 | /** |
||||
31 | * Get the current or new unique identifier. |
||||
32 | * |
||||
33 | * @return string The identifier |
||||
34 | */ |
||||
35 | public function get(): string |
||||
36 | { |
||||
37 | if (! empty($_COOKIE['cart_identifier'])) { |
||||
38 | return $_COOKIE['cart_identifier']; |
||||
39 | } |
||||
40 | |||||
41 | return $this->regenerate(); |
||||
42 | } |
||||
43 | |||||
44 | /** |
||||
45 | * Regenerate the identifier. |
||||
46 | * |
||||
47 | * @return string The identifier |
||||
48 | */ |
||||
49 | public function regenerate(): string |
||||
50 | { |
||||
51 | $identifier = md5(uniqid('', true)); |
||||
52 | |||||
53 | @setcookie('cart_identifier', $identifier, 0, '/'); |
||||
0 ignored issues
–
show
|
|||||
54 | |||||
55 | return $identifier; |
||||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * Forget the identifier. |
||||
60 | * |
||||
61 | * @return void |
||||
62 | */ |
||||
63 | public function forget(): void |
||||
64 | { |
||||
65 | @setcookie('cart_identifier', '', time() - 3600); |
||||
0 ignored issues
–
show
It seems like you do not handle an error condition for
setcookie() . This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
![]() |
|||||
66 | } |
||||
67 | } |
||||
68 |
If you suppress an error, we recommend checking for the error condition explicitly: