heron
heron

Reputation: 3661

Having issue with cookies

Checking for cookies with code below

        if (!isset($_COOKIE['auth'])) {
            header('Location:' . wsurl);
        }

My php code redirects to website url even if browser shows that there is cookie from this website

enter image description here

What can cause this problem?

Whole code looks like that.

public function protect() {
    if (!isset($_SESSION)) {
        session_start();
    }
    $data = array();

    if (isset($_SESSION['auth'])) {
        $stmt = $this->db->prepare("SELECT l.browser, l.ip, u.ban from log AS l, users AS u WHERE l.token =? AND u.id=l.user_id LIMIT 1") or die($this->db->error);
        $stmt->bind_param("s", $_SESSION['auth']) or die($stmt->error);
        $stmt->execute() or die($stmt->error);
        $stmt->store_result();
        if ($stmt->num_rows == 0) {
            $this->signout();
        }
        $stmt->bind_result($data['browser'], $data['ip'], $data['ban']);
        $stmt->fetch() or die($stmt->error);
        $stmt->close() or die($stmt->error);
        $result = $this->validation->check("protection", $data);
        if ($result != true) {
            $result = (is_numeric($result)) ? $result : true;
            $this->signout($result);
        }
    } else {
        if (!isset($_COOKIE['auth'])) {
            header('Location:' . wsurl);
        }
        $stmt = $this->db->prepare("SELECT l.browser, l.timeout, l.ip, u.ban from log AS l, users AS u where l.token =? AND u.id=l.user_id LIMIT 1") or die($this->db->error);
        $stmt->bind_param("s", $_COOKIE['auth']) or die($stmt->error);
        $stmt->execute() or die($stmt->error);
        $stmt->store_result();
        if ($stmt->num_rows == 0) {
            $this->signout();
        }
        $stmt->bind_result($data['browser'], $data['timeout'], $data['ip'], $data['ban']) or die($stmt->error);
        $stmt->fetch() or die($stmt->error);
        $result = $this->validation->check("protection", $data);
        if ($result != true) {
            $result = (is_numeric($result)) ? $result : true;
            $this->signout($result);
        }
        session_regenerate_id();
        $_SESSION['auth'] = $_COOKIE['auth'];
        $stmt->close() or die($stmt->error);
    }
}

Upvotes: 1

Views: 44

Answers (1)

J. Bruni
J. Bruni

Reputation: 20492

It seems the domain and / or path is not properly set:

http://en.wikipedia.org/wiki/HTTP_cookie#Domain_and_Path

And be aware that exam.az is different from www.ezam.az...

I guess the Path: /core is causing the issue.

Upvotes: 1

Related Questions