Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php在注销按钮上添加函数_Php_Html - Fatal编程技术网

php在注销按钮上添加函数

php在注销按钮上添加函数,php,html,Php,Html,我正在创建一个网站,我想在那里登录和唱出来。因此,当我登录时,我重定向到home.html,因此在home.html中,我添加了一个按钮,将其命名为“logout”,我想在其上添加功能,以便每当我单击该按钮时,它都会将我注销。正如我已经知道的,我必须使用的代码,但不知道如何将该代码放入注销按钮? 我想知道如何将我的this button name=“logout”按钮引用到该特定代码 会话_destroy();因此,当我在home.html中时,单击“注销”按钮,它将破坏我当前的季节,并将我定位

我正在创建一个网站,我想在那里登录和唱出来。因此,当我登录时,我重定向到home.html,因此在home.html中,我添加了一个按钮,将其命名为“logout”,我想在其上添加功能,以便每当我单击该按钮时,它都会将我注销。正如我已经知道的,我必须使用的代码,但不知道如何将该代码放入注销按钮? 我想知道如何将我的this button name=“logout”按钮引用到该特定代码 会话_destroy();因此,当我在home.html中时,单击“注销”按钮,它将破坏我当前的季节,并将我定位回index.php。
index.php

用户名:
输入密码
让我登录!
创建另一个名为“logout.php”的文件。放置下面的代码

<?php

session_start();
unset($_SESSION);
session_destroy();
header("Location: index.php");

?>


单击“注销”按钮,它应该重定向到“logout.php”。

只需创建另一个名为logout.php的页面。取消设置那里的会话取消设置($_session['someusername']),并编写代码重定向到您的主页


标题('位置:')

我假设您的登录页面是
login.php
,我猜您在那里有一个用户名和密码的html表单,还可以通过重定向到home.html进行用户验证。将注销逻辑也放在其中是有意义的

将以下代码放在
login.php
的顶部:

$action = $_GET['action'];

if($action == 'logout') {
  /* I'm sure you don't want to destroy the entire session, as there
     could be other valuable data stored, so use unset to destroy only 
     the specific login-variable. */
  unset($_SESSION['loginVar']);
}
$\u SESSION['loginVar']
是存储用户登录数据的会话变量

现在,您可以将以下href用于注销链接/按钮:

login.php?action=logout

您所要做的就是为用户提供一个注销链接,并将页面重定向到index.php

home.html:

<a href="logout.php">Logout</a>

logout.php

<?php
    session_start();

    session_destroy();
    header('Location: index.php');
?>

正常 您有logout按钮,您必须使用logout.php并将其链接到logout按钮 比如说

logout.PHP

<?PHP 
session_start();
session_destory();
unset($_SESSION['user_id']); 
header("location: login.html");
?> 

和index.html,如下所示

<a name="logout" href="logout.php"> Logout </a>

HTML


你的代码是什么样子的?我已经添加了我用来制作该页面的所有代码,你能检查一下吗。我听了你的话,但还是不起作用。感谢u@HasanSyed提供您的文件或完整地解释您拥有的内容
<a href='index.php?logout=1'>Logout</a>
if(isset($_GET['logout'))
{
   session_destroy();
   header('Location:home.php');
}