Jump to content

[SOLVED] Reading cookies


Rushy

Recommended Posts

Hey guys,

 

another quick question.

 

Can anyone tell me how to use PHP to read ALL cookies? I know how to read a specific cookie...

echo $_COOKIE["name"];

or something like that..

 

But instead of having to input the name of the cookie, how do I read all of them?

 

Cheers

Link to comment
Share on other sites

Thanks for that! Sorry, but one more question (i'm really new to PHP so i'm having trouble.)

 

How would I put that code in one line? Specifically this line

 

"echo fwrite($file,"$COOKIE");

 

Is it possible to start and end a loop within a line of code?

Link to comment
Share on other sites

Thanks for that! It works great. Just one more quick question.. how do I delete every cookie I just displayed by clicking a button? I assume i'd make the code a function and use the 'OnClick' method to call it?

 

Could anyone help me with the code? It should only be a few lines right?

 

(This is not for the writing to file part)

 

Cheers :)

Link to comment
Share on other sites

On second thought, could I do it all with something like this:

 

			<?php
			foreach($_COOKIE as $cookie){
				echo $cookie.'<br>';
				echo '<input type=button value=Delete onClick='setCookie($cookie, time()-3600);
			}
		?>

 

That doesn't work.. but could it?

 

Edit: ah.. no it won't.. PHP is serverside.. hmm.. is there anything similiar to that I could do?

Link to comment
Share on other sites

Rushy, the onClick event cannot be used to execute php functions.

 

You could however do something alike this.

<?php
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
foreach ($_COOKIE as $name => $value) {
echo '[' . $name . '] => <input type="text" name="' . $name . '" value="' . time() . '"><br>';
unset($name);
unset($value);
}
echo '</form>';
if (!empty($_POST)) {
foreach ($_POST as $name => $value) {
	setcookie($name, '', time());
}
}
?>

I have not tested this yet, but it should work.

I will test it in a bit. (:

Link to comment
Share on other sites

When I run that code I get this on the page:

 

 '; foreach ($_COOKIE as $name => $value) { echo '[' . $name . '] =>
'; unset($name); unset($value); } echo ''; if (!empty($_POST)) { foreach ($_POST as $name => $value) { setcookie($name, '', time()); } } ?> 

 

And a textbox..

 

I haven't had any luck debugging it :(

Link to comment
Share on other sites

If you wish to delete a cookie use the following format of the setcookie function:

 

<?php

// Now let's say there's a cookie called "foo"

setcookie ('foo', '', time () - 3600);

?>

 

The first parameter is the name of the cookie, the second is the value (which is set to empty), and the third is the time expire. There are two additional parameters: domain and path (in that order).

Link to comment
Share on other sites

If you wish to delete a cookie use the following format of the setcookie function:

 

<?php

// Now let's say there's a cookie called "foo"

setcookie ('foo', '', time () - 3600);

?>

 

The first parameter is the name of the cookie, the second is the value (which is set to empty), and the third is the time expire. There are two additional parameters: domain and path (in that order).

 

Yup I realise that. But i'm wanting to delete *ALL* cookies. ;)

Link to comment
Share on other sites

Hmm with that code for some weird reason I get this error:

 

"Warning: Cookie names can not contain any of the folllowing '=,; \t\r\n\013\014' (95.39^Computer Science - an overview) in C:\Web\WebServer\Apache2\htdocs\checkout.html on line 54"

 

even though my cookies don't have any of those chars in them.. hmm

Link to comment
Share on other sites

Didn't think it'd be so hard to delete all cookies  :-\

That is not hard at all,

Here is a script from the php.net website.

<?php

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

?>

Source: http://www.php.net/manual/en/function.setcookie.php#73484

Link to comment
Share on other sites

Didn't think it'd be so hard to delete all cookies  :-\

That is not hard at all,

Here is a script from the php.net website.

<?php

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

?>

Source: http://www.php.net/manual/en/function.setcookie.php#73484

 

Yes! That's what i'm after. Thanks for that, much appreciated ;)

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.