Discussion: PHP MVC - Can't delete cookie

In the previous quiz, Online PHP Quiz, we tested our experience gained from the course.

Activities
Avatar

Member
Avatar
:5/3/2017 10:09

Hello i'm building my own simple MVC framework. When I tried to create login page all worked - session all ok. But when I try to work with cookies, it doesn't work. Exactly deleting cookie don't work. :( Please help me, i can't find any solutions on google. File code where I'm trying to delete cookie:

<?php
class HomeController extends Controller {

    function __construct(){
        $this->view = 'testView';
    }


    public function index($params){
        if(isset($_COOKIE['logged'])){
            echo "cookie exists";
        } else{
            if($_POST){
                if(isset($_POST['rr'])){
                    setcookie('logged', true, time()+3600);
                }
            }
        }
    }

    public function logout($params){
        setcookie('logged', null, time()-3600);
    }

}

My whole code

Thanks.

 
Reply
5/3/2017 10:09
Avatar
Replies to
David Capka Hartinger:5/3/2017 12:41

If you're not on local server, the problem can be your website is using pretty urls so you have to specify other parameters of the setcookie() method like this:

setcookie('logged', 'true', time() + 3600, '/', '.yourdomain.com', null, true);

Also notice specifying the last parameter to protect the cookies from being stolen using XSS attack.

However, I think you want to use sessions, we don't usually use cookies for logging in/out. Cookies can be used for permanent login, but not for the core mechanism.

Up Reply
5/3/2017 12:41
You can walk through a storm and feel the wind but you know you are not the wind.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

2 messages from 2 displayed.