通过 php 在注销按钮上添加功能


Adding Function on Sign Out button by php

我正在创建一个网站,我想在其中登录并唱出。因此,当我登录时,我重定向到home.html,因此在主页中.html我通过将其命名为"注销"来添加了一个按钮,我想在其上添加功能,因此每当我单击该按钮时,它都会将我注销。正如我已经知道的,我必须使用的代码,但不知道如何将该代码放到注销按钮?我想知道如何将我的这个按钮名称="注销"按钮引用到该特定代码session_destroy();因此,当我在家时.html单击"退出"按钮,它将破坏我当前的赛季并将我定位回index.php。
索引.php

<form class="form-horizontal" role="form" action="process.php" method="post">
  <div class="form-group">
    <label id="email" for="inputtext" class="col-sm-4 control-label">User name:</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="inputEmail" placeholder="User name" name="username">
    </div>
  </div>
  <div class="form-group">
    <label id="pass" for="inputPassword" class="col-sm-4 control-label">Enter Password</label>
    <div class="col-sm-4">
      <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="pass">
    </div>
  </div>
  
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-success">Sign me in!</button>
    </div>
  </div>
</form>

过程.php

<?php
     $action = $_GET['action']; 
     if ($action == 'logout') {
     unset($_SESSION['username']); 
     }
     
     
     
     $username = $_POST ['username'];
     $password = $_POST ['pass']; 
     
     //fixed values
     
     if($username=='syedhasan' AND $password=='Syed712207') {
     echo "You have successfully logged in"; 
      header('Location: home.html');   
     }
     
     else {
     echo "Credential is wrong";  
      
     }
      
     ?>
在主页上.html我添加了此按钮

<input href="logout.php" type="submit" class="signout btn btn-warning"" value="Sign Out" name="logout">

我创建了注销.php

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

创建另一个名为"logout.php"的文件。放置以下代码。

<?php
session_start();
unset($_SESSION);
session_destroy();
header("Location: index.php");
?>
单击"注销

"按钮时,它应重定向到"注销.php"。

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

标头("位置:http://www.example.com/");

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

首页.html:

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

注销.php

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

ok你有注销对接,你必须注销.php并将其链接到注销屁股例如

注销。.PHP

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

和索引.html像这样

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

我假设您的登录页面是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']);
}

$_SESSION['loginVar']是存储用户登录数据的会话变量。

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

login.php?action=logout

HTML

<a href='index.php?logout=1'>Logout</a>

.PHP

if(isset($_GET['logout'))
{
   session_destroy();
   header('Location:home.php');
}