ntwiles Posted August 29, 2010 Share Posted August 29, 2010 Hey guys I'm pretty new to both PHP and Javascript. I think my problem is a PHP one, not a Javascript one. But it involves both. I'm trying to delete a cookie by clicking a link. I call the javascript function like so: if (isset($_COOKIE["active"])) { echo "<a href='addpost.php'>Add Post</a> <a href onClick='eraseCookie()'>Log Out</a>"; } And the function looks like this: function eraseCookie() { <?php setcookie(active, 0, time()-3600); echo "It works"; ?> } The function doesn't delete the cookie and doesn't echo "It works". Like I said this could be a Javascript error, not a PHP one, but I have to start somewhere. Can someone tell me what I'm doing wrong? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 29, 2010 Share Posted August 29, 2010 You cannot mix JavaScript and PHP together. PHP will be parsed long before JavaScript. So with the following code function eraseCookie() { <?php setcookie(active, 0, time()-3600); echo "It works"; ?> } When you go to use the javascript function eraseCookie() it'll be an empty function. function eraseCookie() { } Quote Link to comment Share on other sites More sharing options...
ntwiles Posted August 29, 2010 Author Share Posted August 29, 2010 Thanks for the reply. I was afraid of that. Originally I tried to use javascript but that didn't work any better. If I create a cookie in php can I delete it using javascript without any problems? Is there a way to go about deleting a cookie by clicking a link using php? Or would I just be better off going to the javascript section and asking about why my old script didn't work? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 29, 2010 Share Posted August 29, 2010 You can delete a cookie with PHP from clicking a link. However it'll require the page to be reloaded. You'll code your logout link as <a href="file.php?action=logout">Logout</a> Now in file.php you'd do this if(isset($_GET['action']) && $_GET['action'] == 'logout') { setcookie('active', 0, time()-3600); echo "It works"; } You shouldn't use JavaScript for managing your cookies as not all users will have JavaScript enabled. Managing cookies on the server side would be better. You shouldn't store anything sensitive (such as passwords, personal information etc) within cookies. Cookies can be very easily be viewed/modified by most modern browsers. 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.