Rushy Posted November 10, 2007 Share Posted November 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 10, 2007 Share Posted November 10, 2007 <?php foreach($_COOKIE as $cookie){ echo $cookie.'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 10, 2007 Author Share Posted November 10, 2007 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? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 10, 2007 Share Posted November 10, 2007 <?php foreach($_COOKIE as $cookie){ fwrite($file, $cookie); } ?> Why are you trying to echo the fwrite() function? That is for writing to a file. Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 10, 2007 Author Share Posted November 10, 2007 Correct. I am writing information (trying to) from the cookie to an XML file. I'll give that a go Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 11, 2007 Author Share Posted November 11, 2007 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 Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 11, 2007 Author Share Posted November 11, 2007 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? Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 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. (: Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 11, 2007 Author Share Posted November 11, 2007 Hi, thank you for that code It doesn't work however. I'll do my best to debug it though. Thanks again Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
TutorMe Posted November 12, 2007 Share Posted November 12, 2007 To show all cookies, use this: print_r($_COOKIE); Quote Link to comment Share on other sites More sharing options...
jordanwb Posted November 12, 2007 Share Posted November 12, 2007 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). Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
ryeman98 Posted November 12, 2007 Share Posted November 12, 2007 Couldn't you use a foreach? Like ... foreach cookies as $cookie setcookie ... ... ... Or does that not work? Quote Link to comment Share on other sites More sharing options...
centerwork Posted November 12, 2007 Share Posted November 12, 2007 <?php if($submit = "Delete") { foreach($_COOKIE as $cookie){ setCookie($cookie, time()-3600); } } ?> Just insert the form on your page with a submit button to triger the PHP Code in your header. Quote Link to comment Share on other sites More sharing options...
centerwork Posted November 12, 2007 Share Posted November 12, 2007 I am guessing on the above. But dont forget the $submit = $_post['submit']; Make sure you insert the name="submit" in your submit button. Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
centerwork Posted November 12, 2007 Share Posted November 12, 2007 I think you may need to add a $cookie = urldecode($cookie); before you setCookie($cookie,.... Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 Hmm nope, that didn't seem to make a difference. Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 Didn't think it'd be so hard to delete all cookies :-\ Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 12, 2007 Share Posted November 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
Rushy Posted November 12, 2007 Author Share Posted November 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
jordanwb Posted November 12, 2007 Share Posted November 12, 2007 Hold on, You have to put in the value parameter or else that won't delete the cookie: <?php if($submit = "Delete") { foreach($_COOKIE as $cookie){ setCookie($cookie, '', time()-3600); } } ?> 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.