bsamson Posted November 1, 2015 Share Posted November 1, 2015 I'm running into some issues with cookies and after reading the documentation, I am still very confused on why the following code produces this output. Why is the cookie not deleted by using the second setcookie function? Any help is GREATLY appreciated. Code: <?php setcookie(custID, "Brian", time()+7200, "/"); echo "Cookie Value: ".$_COOKIE["custID"]."<br>"; setcookie(custID, "", time()-7200, "/"); echo "Cookie Value: ".$_COOKIE["custID"]; ?> Result: Cookie Value: Brian Cookie Value: Brian Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted November 1, 2015 Solution Share Posted November 1, 2015 PHP code runs on the server, cookies are managed by the client. That means deleting a cookie can only take effect after the code has been executed and the client has received the response. I recommend that you use the developer tools of your browser to see when cookies are set and deleted. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 1, 2015 Share Posted November 1, 2015 you cannot use a setcooke() (or anything else that sends a http header to the browser) after you have sent any characters to the browser. your first echo statement is preventing the second setcooke() statement from working. if you had php's error_reporting set to E_ALL and display_errors set to ON, php would be telling you this. next, the $_COOKIE data comes from the browser. it is only available after the browser requests the page and sends the cookie data to the server. $_COOKIE data is not available on the same page request where it was set using a setcookie() statement. Quote Link to comment Share on other sites More sharing options...
0x00 Posted November 1, 2015 Share Posted November 1, 2015 (edited) I believe it works on the next page load, you need to unset variables on this calling. I use set_cookie_params() for some reason I can't remember, but I also generate a new session id, unset the old vars and then refresh the page, just to make sure. // DESTROY SESSION COOKIE by setting expiry date to past session_set_cookie_params(0, $this->cParams["path"], $this->cParams["domain"], false, true); session_start(); // LOGGING ... // DESTROY SESSION, VARIABLES AND ID session_unset(); session_regenerate_id(true); //session_destroy(); header("Location: ".oops_path_self()); exit(); Edited November 1, 2015 by 0x00 Quote Link to comment Share on other sites More sharing options...
bsamson Posted November 1, 2015 Author Share Posted November 1, 2015 THANK YOU SO MUCH @Guru!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.