November Black Friday C# week
Black friday is here! Get up to 80 % extra points for free! More info
Only this week up to 80 % off on C# courses. More info

Discussion: PHP MVC - Can't delete cookie

Activities (2)
Avatar
gold604
Member
Avatar
gold604: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
David Capka
ICT.social team
Avatar
Replies to gold604
David Capka: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.