2017-06-01 123 views
0

我有登錄和註銷頁面,但無法註銷,總是說用戶已經登錄。這是我的登錄頁面:session_destroy無法破壞會話php

<?php 


session_start(); 
    include_once("connection.php"); 
    if(isset($_POST) & !empty($_POST)) { 
    $userName = mysqli_real_escape_string($connection, $_POST['userName']); 
    $userPassword = md5($_POST['userPassword']); 
    $login = "SELECT * FROM `users` WHERE userName = '$userName' and password = '$userPassword'"; 
    $result = $connection->query($login); 
    while ($val = mysqli_fetch_array($result)) 
    { 
     $isAdmin = $val['isAdmin']; 
     $companyID = $val['companyID']; 
     $branchID = $val['branchID']; 
     $ID   = $val['ID']; 
    } 
    $count = mysqli_num_rows($result); 
    if($count == 1){ 
     $_SESSION['userName'] = $userName; 
     setcookie("userID", $ID); 
     setcookie("companyID", $companyID); 
     setcookie("branchID", $branchID); 
     if(!$isAdmin){ 
     header('location: home.php'); 
     } 
     else { 
     header('location: admin/home.php'); 
     } 
    } 
    else { 
     $fmsg = "Wrong user name"; 
    } 
    } 
    if(isset($_SESSION['userName'])){ 
    $smsg = "Already loggedin"; 
    } 
?> 

這裏是loggout頁:

<?php 
    session_start(); 
    session_destroy(); 
    unset($_COOKIE['companyID']); 
    unset($_COOKIE['userID']); 
    unset($_COOKIE['branchID']); 
    setcookie('companyID', null, -1, '/'); 
    setcookie('userID', null, -1, '/'); 
    setcookie('branchID', null, -1, '/'); 
    header('location: index.php'); 
?> 

我怎麼能解決這個問題?任何人都可以幫我嗎?還有一個附加的圖像顯示餅乾。 enter image description here

回答

2

只要使用此:

$_SESSION = array(); 
session_destroy(); 
1

在這裏你可以做的是

$_SESSION['userName'] = ''; //clear userName only 

並添加以下代碼中的

if(isset($_SESSION['userName']) && $_SESSION['userName'] != ''){ 
    $smsg = "Already loggedin"; 
    } 

unset($_SESSION); //all session will be gone 
$_SESSION = array(); 


// unset cookies 
if (isset($_SERVER['HTTP_COOKIE'])) { 
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']); 
    foreach($cookies as $cookie) { 
     $parts = explode('=', $cookie); 
     $name = trim($parts[0]); 
     setcookie($name, '', time()-1000); 
     setcookie($name, '', time()-1000, '/'); 
    } 
} 

希望這有助於

+0

很抱歉,但couldnot進行任何更改。我有同樣的問題:( –

+0

這是我的註銷頁面現在 '' –

+0

如果您打算將cookie的時間設置爲減號,我的意思是這並不能解決你的問題,但它沒有意義 –

0

這裏我的代碼工作對我來說

session_start(); 
unset($_SESSION['userid']); 
unset($_SESSION['username']); 
unset($_SESSION['photo']); 
session_destroy(); 
unset($_COOKIE['userid']); 
unset($_COOKIE['emailid']); 
unset($_COOKIE['photo']); 
setcookie('userid', null, -1, '/'); 
setcookie('emailid', null, -1, '/'); 
setcookie('photo', null, -1, '/'); 
header("Location:login"); 
+0

不幸的是我不能爲我工作 –

+0

嘗試我的模式,並告訴PHP版本 –

0

我覺得問題是一個與MS邊工作的Firefox。感謝您對您的所有幫助我的代碼是在這裏: 註銷:

<?php 
    session_start(); 
    setcookie("branchID", "", 1); 
    setcookie("companyID", "", 1); 
    setcookie("userID", "", 1); 
    setcookie("userName", "", 1); 
    session_destroy(); 
    header('location: index.php'); 
?> 

登錄:

<?php 


session_start(); 
    include_once("connection.php"); 
    if(isset($_POST) & !empty($_POST)) { 
    $userName = mysqli_real_escape_string($connection, $_POST['userName']); 
    $userPassword = md5($_POST['userPassword']); 
    $login = "SELECT * FROM `users` WHERE userName = '$userName' and password = '$userPassword'"; 
    $result = $connection->query($login); 
    while ($val = mysqli_fetch_array($result)) 
    { 
     $isAdmin = $val['isAdmin']; 
     $companyID = $val['companyID']; 
     $branchID = $val['branchID']; 
     $ID   = $val['ID']; 
    } 
    $count = mysqli_num_rows($result); 
    if($count == 1){ 
     $_SESSION['userName'] = $userName; 
     setcookie("userID", $ID); 
     setcookie("companyID", $companyID); 
     setcookie("branchID", $branchID); 
     if(!$isAdmin){ 
     header('location: home.php'); 
     } 
     else { 
     header('location: admin/home.php'); 
     } 
    } 
    else { 
     $fmsg = "Hatalı Kullanıcı Adı/Şifre"; 
    } 
    } 
    if(isset($_SESSION['userName']) && $_SESSION['userName'] != ''){ 
    $smsg = "Kullanıcı Halihazırda Giriş Yapmış"; 
    } 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="css/style.css"> 
    <link rel="stylesheet" href="css/lib/lobipanel/lobipanel.min.css"> 
    <link rel="stylesheet" href="css/lib/jqueryui/jquery-ui.min.css"> 
    <link rel="stylesheet" href="css/lib/font-awesome/font-awesome.min.css"> 
    <link rel="stylesheet" href="css/main.css"> 
    </head> 
    <body> 
    <header class="site-header"> 
     <div class="container-fluid"> 
      <a href="#" class="site-logo"> 
       <img class="hidden-md-down" src="img/logo-2.png" alt=""> 
       <img class="hidden-lg-up" src="img/logo-2-mob.png" alt=""> 
      </a> 
      <div class="site-header-content"> 
       <div class="site-header-content-in"> 
        <div class="site-header-shown"> 
         <a href="#" class="site-logo"> 
          <img class="hidden-md-down" src="img/logo-ds.png" alt=""> 
          <img class="hidden-lg-up" src="img/logo-ds-mob.png" alt=""> 
         </a> 
        </div><!--.site-header-shown--> 
        HOŞ GELDİNİZ 
       </div><!--site-header-content-in--> 
      </div><!--.site-header-content--> 
     </div><!--.container-fluid--> 
    </header><!--.site-header--> 
    <div class="form-style-6"> 
     <h1>Giriş Yap</h1> 
     <form action="" method="post"> 
     <input type="text" name="userName" placeholder="Kullanıcı Adı" /> 
     <input type="password" name="userPassword" placeholder="Şifre" /> 
     <input type="submit" value="Giriş Yap" /> 
     </form> 
    </div> 
    <div class="container"> 
     <?php if(isset($smsg)){ ?> 
     <div class="alert alert-success" role="alert"> <?php echo $smsg; ?></div> 
     <?php } ?> 
     <?php if(isset($fmsg)){ ?> 
     <div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?></div> 
     <?php } ?> 
     <?php if(isset($asmsg)){ ?> 
     <div class="alert alert-success" role="alert"> <?php echo $asmsg; ?></div> 
     <?php } ?> 
     <?php if(isset($afmsg)){ ?> 
     <div class="alert alert-danger" role="alert"> <?php echo $afmsg; ?></div> 
     <?php } ?> 
    </div> 
    </body> 
</html>