0

I'm trying to execute some PHP that removes a cookie from your browser (it's used for removing your login data cookie) and when you click on the button called 'Log Out'I tried using an action to do this, but it does not seem to work this way?

<?php
                    if(isset($_COOKIE['LoggedIn']) && !empty($_COOKIE['LoggedIn'])) {
                    echo "<li><a href=\"#\" action=\"setcookie(\"LoggedIn\", \"\", time(), \"/\");\">Log Out</a></li>";
                    } else {
                        echo "<li><a href=\"login.php\">Log in</a></li>";
                    }
                ?>

I am using the '\' to change make the quotes into regular text quotes that can be placed inside the main quotes.

So my question is mainly, how will i achieve executing it correctly? I've tried it this way but it does not do a thing.

  • 1
    Unless I've missed something, there's no `action` attribute on an `a` element. Are you trying to make a link or a form? It looks like you're trying to simply make a button with an `onclick` instead, so why not do that? As for the `setcookie()` function, where do you define that? – David Oct 05 '16 at 15:22
  • 1
    Nope. PHP is processed only before the page loads. When the page is fully loaded, you want to use javascript, either to do a similar process, or call an ajax function to send a request to a PHP page. – aynber Oct 05 '16 at 15:23
  • 1
    You're trying to execute a PHP function via the 'action' in HTML. I think you'd want to call a JavaScript function instead, that will do an AJAX call to your page which will execute the PHP function. – Just Rudy Oct 05 '16 at 15:25

4 Answers4

0

As far as I'm aware anchor tags ("a" tag) have no concept of an "action" attribute. I think what you want here is "onclick" instead of "action".

Occurs to me you are also trying to execute a php function in the "action" attribute, this clearly will not work - you need to create a simple javascript function that clears out the cookie. For example:

var deleteCookie = function(name,path) {
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;' + (path ? ' path=' + path : '');
};

Which you can then invoke in an "onclick" on your link.

Max Mammel
  • 51
  • 2
0

I reccomend just having a logout.php button which redirects to the login page after logout button has been clicked like so:

echo "<li><a href=\"logout.php\">Log Out</a></li>";

Logout.php

    //Expire Cookie
    setcookie('LoggedIn', '', time() - 60*100000, '/');

    //Redirect to page
    header( 'Location: https://www.foo.com/login.php' ) ;
Kitson88
  • 2,889
  • 5
  • 22
  • 37
  • I have tried this, even used a var_dump to check if the cookie still exists, the cookie holds a string of the users name, however it still shows that the cookie exists –  Oct 05 '16 at 17:28
  • You need to make sure that you have set the exact same parameters (ignoring value and expiry) you had before or else it will not work. I had the same issue and it was this SO question which helped me: http://stackoverflow.com/questions/2856366/problems-deleting-cookies-wont-unset – Kitson88 Oct 05 '16 at 17:30
0

Better still link your a href tag to a php file that runs the function you need and use

header("Location: Your URL")

To redirect back to the login page or anywhere you want

0

You could use something like this;

<a href='/?logout'>Logout</a>

if(isset($_GET['logout'])){ Logout(); }

function Logout() {

    unset( $_SESSION[''] ); // unset and session data
    session_unset();  // remove all session variables
    session_destroy(); // destroy the session
    setcookie("LoggedIn", "", time() - 36000, "/");   //unset the remember me cookie
    header( "Location: /?loggedOut=1" );
    exit;
}
atoms
  • 2,993
  • 2
  • 22
  • 43