简体   繁体   English

在&#39;中执行PHP <a href=“” action=“php here”></a> &#39;

[英]Executing PHP in an '<a href=“” action=“php here”></a>'

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,该PHP会从您的浏览器中删除Cookie(用于删除您的登录数据cookie),当您单击名为“注销”的按钮时,我尝试使用操作来执行此操作,但并没有似乎以这种方式工作?

<?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. 我已经尝试过这种方式,但是它没有做任何事情。

As far as I'm aware anchor tags ("a" tag) have no concept of an "action" attribute. 据我所知,锚标记(“ a”标记)没有“ action”属性的概念。 I think what you want here is "onclick" instead of "action". 我认为您想要的是“ onclick”而不是“ 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. 对我来说,您也在尝试在“ action”属性中执行php函数,但这显然行不通-您需要创建一个简单的JavaScript函数来清除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. 然后您可以在链接上的“ onclick”中调用它。

I reccomend just having a logout.php button which redirects to the login page after logout button has been clicked like so: 我建议仅使用一个logout.php按钮,该按钮在单击注销按钮后将重定向到登录页面,如下所示:

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

Logout.php 注销.php

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

    //Redirect to page
    header( 'Location: https://www.foo.com/login.php' ) ;

Better still link your a href tag to a php file that runs the function you need and use 最好还是将您的href标记链接到运行所需功能的php文件

header("Location: Your URL")

To redirect back to the login page or anywhere you want 重定向回登录页面或所需的任何位置

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM