Jump to content

Changing Values of Cookies


bsamson
Go to solution Solved by Jacques1,

Recommended Posts

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
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by 0x00
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.