willium
willium

Reputation: 2128

Cookies Not Working Properly

this is my code in logout.php

    <?php
if(isset($_COOKIE['cookie-username']) || isset($_COOKIE['cookie-password'])) {

    setcookie("cookie-username", NULL, time()-60*60*24*100);
    setcookie("cookie-password", NULL, time()-60*60*24*100);
}
    header( 'Location: ../login' ) ;
    ?>

I can confirm the cookies exist, If I do a while loop I get
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 79 bytes) in /home/ios/public_html/logout.php on line 5

which means It cannot delete the cookies... I've done a lot of googling, but I can't figure out why it won't delete said cookies...

Help? Thanks In Advance

edit: proof of cookies: http://cl.ly/1c31120de4b722518b05

creation of cookies:

if (isset($_POST['submit'])) {

        try {           
            if (!isset($_POST['username']) || !isset($_POST['password']) || trim($_POST['username']) =='' || trim($_POST['password']) =='') {               throw new Exception('All Fields Are Required!');
            }
            $username = strtolower($_POST['username']);
            $password = md5(strtolower($_POST['password']));

            include('classes/config.php');

                        [SQL REMOVED]
            foreach ($pdo->query($sql) as $row) {
                $dbPassword = $row['password'];
            }  

            if (!isset($dbPassword)) {
                throw new Exception('Invalid Username or Password!');
            }

            if ($dbPassword == $password) {
                 if (!isset($_COOKIE['cookie-username']) || !isset($_COOKIE['cookie-password'])){ 

                    $days = 1;

                    if (strtolower($_POST['remember']) == 'on') {
                        $days = 30;
                    }


                    setcookie("cookie-username", $username, time()+60*60*24*$days, "/");
                    setcookie("cookie-password", $password, time()+60*60*24*$days, "/");

                }  

                   echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../index">';    
            }   
            else if ($dbPassword != $password) {
                throw new Exception('Invalid Username or Password!');
            }   
        }

            catch (Exception $e) {
                $exception = $e->getMessage();
        }
    }

Upvotes: 0

Views: 864

Answers (2)

meze
meze

Reputation: 15087

Remove the while loop. setcookie doesn't affect $_COOKIE.

If it doesn't help, try this:

<?php
    setcookie("cookie-username", NULL, time()-60*60*24*100, "/", ".yourdomain.com");
    setcookie("cookie-password", NULL, time()-60*60*24*100, "/", ".yourdomain.com");
    header( 'Location: ../login' ) ;
?>

PS: Also it's a bad idea to store login/password in cookies, for an authentication purpose it's better to store a flag in session

Upvotes: 1

Svisstack
Svisstack

Reputation: 16616

<?
setcookie('cookie-username', 0, -1);
setcookie('cookie-password', 0, -1);
header('Location: example.com/path/login');
?>
  1. This functions don't throw anything then try is not usable for you.
  2. $_COOKIES is refreshing only when script start then if you erase cookie, you will see on next refresh results.

Upvotes: 0

Related Questions