Jump to content

Cookie Question


ntwiles

Recommended Posts

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?

 

Link to comment
Share on other sites

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() {
}

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.