SessionCookie.php 1.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 4/30/17
 * Time: 1:45 PM
 */

namespace qfq;

require_once(__DIR__ . '/../Constants.php');

class SessionCookie {

    private $pathFileNameCookie = '';

    /**
     * Copy all current cookies to a temporary file.
     *
     * @param string $domain
     * @param string $path
     * @throws CodeException
     */
    public function __construct($domain, $path) {
        $lines = '';

27
28
        // $_COOKIES[]
        if (false === ($this->pathFileNameCookie = tempnam(sys_get_temp_dir(), SESSION_COOKIE_PREFEIX))) {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
            throw new CodeException('Error creating output file.', ERROR_IO_CREATE_FILE);
        }

        foreach ($_COOKIE as $name => $value) {
            // e.g.: SANDBOXSESSION=a83f1o69jbv12932q54hmgphk6; domain=qfq.math.uzh.ch; path=/;
            $lines .= $name . "=" . $value . "; domain=$domain; path=$path;\n";
        }

        file_put_contents($this->pathFileNameCookie, $lines, FILE_APPEND);
    }

    /**
     * Unlink current cookie file
     */
    public function __destruct() {
        if (file_exists($this->pathFileNameCookie)) {
            unlink($this->pathFileNameCookie);
            $this->pathFileNameCookie = '';
        }
    }

    /**
     * @return string PathFilename of cookie file
     */
    public function getFile() {

        return $this->pathFileNameCookie;
    }
57

58
}