取消全局会话变量作为注销按钮 [英] Killing off Global Session Variable as a logout button

查看:39
本文介绍了取消全局会话变量作为注销按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿Stackoverflow用户,

因为我能够在遇到问题之前得到一些惊人的帮助,所以我待了很长时间,以至于我不记得我以为会遇到这个问题.

使用登录系统对用户进行身份验证并终止会话,但当前无法识别分配给该会话的变量.单击注销按钮后,一切似乎都可以正常工作,但是当通过在地址栏中键入会员页面直接进行连接时,它将加载该页面,而不是重定向到登录页面.

Members.php

 <?PHPsession_start();如果(!isset($ _ SESSION ['username'])){header('location:login.php');}?><!DOCTYPE html PUBLIC-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">< html xmlns ="http://www.w3.org/1999/xhtml">< head>< meta http-equiv ="Content-Type" content ="text/html; charset = UTF-8"/>< title> The Animator BETA</title>< style>//CSS已被删除,因为它冗长且与问题无关.</style><!-[如果IE 9]>< script src ="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]->< script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>< script>$(document).ready(function(){$('#login-trigger').click(function(){$(this).next('#login-content').slideToggle();$(this).toggleClass('active');if($(this).hasClass('active'))$(this).find('span').html('&#x25B2;')否则$(this).find('span').html('&#x25BC;')})});</script></head>< body>< header class ="cf">< nav style ="text-align:center">< a href ="logout.php">注销</a></nav></header>< h1 style ="text-align:center">动画师-BETA</h1>< hr/>< div id ="nav" style ="text-align:center">< ul style ="text-align:center">< li>< a href =#">< strong>首页</strong></a</li>< strong>< li>< a href ="industrial.html"> Industrial</a</li>< li>< a href ="educational.html">教育</a></li>< li>< a href ="independent.html"> Independent</a></li>< li>< a href ="emergent.html"> Emergent</a></li>< li>< a href ="team.html"> Team</a></li>< li>< a href ="project.html"> Project</a></li>< li>< a href ="budget.html">预算</a></li>< li>< a href ="profile.html"> Profile</a></li></strong></ul></div>< hr/>< div style ="padding-left:19%">< input type ="text" value ="search"/>< input type ="button" value =搜索!"name ="search"/></div>< div>& nbsp;</div>< div align ="center">< div>< img src ="logo.png" width ="407" height ="345" alt ="Logo" usemap ="sectors"/></div></div></div>< map name ="sectors">< area shape ="rect" coords ="72,40,194,165" alt =工业" href ="industrial.html">< area shape ="rect" coords ="210,38,328,162" alt =紧急" href ="emergent.html">< area shape ="rect" coords ="208,178,331,296" alt =独立" href ="independent.html">< area shape ="rect" coords ="71,177,194,295" alt =教育" href ="educational.html"></map>< div>& nbsp;</div>< div style ="text-align:right"></div>< div>& nbsp;</div>< div id ="footer">< hr/>< p>< strong>< u>< a href ="about.html">关于动画师</a>|< a href ="contact.html"> Contact</a>|< a href ="privacy.html">隐私权政策</a>|< a href ="faq.html">常见问题解答</a></u></strong>< u></u></p></div></body></html> 

登录页面

 <?phperror_reporting(E_ALL);ini_set('display_errors',开");包括('database_connection.php');如果(isset($ _ POST ['formsubmitted'])){//初始化会话:session_start();$ error = array();//此aaray将存储所有错误消息if(empty($ _ POST ['e-mail'])){//如果提供的电子邮件为空$ error [] ='您忘记输入电子邮件了';} 别的 {if(preg_match("/^([a-zA-Z0-9 _._-])* @([a-zA-Z0-9 _-])*([a-zA-Z0-9 \ ._-] +)+ $/,$ _POST ['e-mail'])){$ Email = $ _POST ['e-mail'];} 别的 {$ error [] ='您的电子邮件地址无效';}}if(empty($ _ POST ['Password'])){$ error [] ='请输入密码';} 别的 {$ Password = $ _POST ['Password'];}if(empty($ error))//如果数组为空,则表示未发现错误{$ query_check_credentials =选择*从帐户所在的位置(电子邮件='$电子邮件'和密码='$密码')";$ result_check_credentials = mysqli_query($ dbc,$ query_check_credentials);if(!$ result_check_credentials){//如果查询失败回显查询失败";}if(@mysqli_num_rows($ result_check_credentials)== 1)//如果查询成功{//进行了匹配.$ _SESSION = mysqli_fetch_array($ result_check_credentials,MYSQLI_ASSOC);//将此查询的结果分配给SESSION全局变量$ _SESSION ['email'] = $ Email;session_start(用户名");header("Location:Members.php");}别的{$ msg_error ='您的帐户无效或电子邮件地址/密码不正确';}}  别的 {回声'< div class ="errormsgbox">< ol>';;foreach($ error as $ key => $ values){回声'< li>'.$ values.'</li>';;}回声'</ol></div>';}if(isset($ msg_error)){回声'< div class ="warning">'.$ msg_error.</div>';}///var_dump($ error);mysqli_close($ dbc);}//主提交条件的结尾.DOCTYPE html PUBLIC-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">< html xmlns ="http://www.w3.org/1999/xhtml">< head>< meta http-equiv ="Content-Type" content ="text/html; charset = utf-8"/>< title>登录表单</title>< style type ="text/css">身体 {字体家族:"Lucida Grande","Lucida Sans Unicode",Verdana,Arial,Helvetica和sans-serif;font-size:12px;}.报名表格 {margin:0自动;宽度:500像素;内边距:14px;}标签 {宽度:10em;向左飘浮;右边距:0.5em;显示:块}.提交 {float:right;}栏位{背景:#EBF4FB无重复滚动0 0;border:2px实心#B7DDF2;宽度:500像素;}传奇 {颜色:#fff;背景:#80D3E2;边框:1px实线#781351;填充:2px 6px}.elements {内边距:10px;}p {border-bottom:1px实心#B7DDF2;颜色:#666666;font-size:11px;底边距:20px;padding-bottom:10px;}一种{颜色:#0099FF;font-weight:bold;}/*盒子样式*/.success,.warning,.errormsgbox,.validation {边框:1px实线;保证金:0自动;padding:10px 5px 10px 60px;背景重复:不重复;背景位置:10px中心;font-weight:bold;宽度:450像素;}.成功 {颜色:#4F8A10;背景颜色:#DFF2BF;background-image:url('images/success.png');}.警告 {颜色:#9F6000;背景颜色:#FEEFB3;背景图片:url('images/warning.png');}.errormsgbox {颜色:#D8000C;背景颜色:#FFBABA;背景图片:url('images/error.png');}.验证 {颜色:#D63301;背景颜色:#FFCCBA;背景图片:url('images/error.png');}</style></head>< body>< form action ="login.php" method ="post" class ="registration_form">< fieldset>< legend>登录表单</legend>< p>在</p>下方输入您的用户名和密码.< div class ="elements">< label for ="name">电子邮件:</label><输入type ="text" id =电子邮件" name =电子邮件" size ="25"/></div>< div class ="elements">< label for ="Password">密码:</label><输入type ="password" id =密码" name =密码" size ="25"/></div>< div class ="submit"><输入type ="hidden" name ="formsubmitted" value ="TRUE"/><输入type ="submit" value =登录"/></div></fieldset></form>< button onclick ="window.location ='theanimator.html';">返回!</button></body></html> 

注销

 <?php未设置($ _SESSION ['email']);header('Location:login.php');出口;?> 

解决方案

您的注销页面必须是这样的:

 <?php//开始会话session_start();//这将破坏启动的会话session_destroy();header('Location:login.php');出口;?> 

问题是,您没有销毁 session ,因此当您返回页面时,该会话存在.

您还希望查看 $ _ SESSION ['用户名'] ,但是您取消设置了 $ _ SESSION ['email'] ,这将永远无法进行.

Hey Stackoverflow users,

Since I was able to get some amazing help before with a problem I was stuck on for longer than I could remember I thought I would come at you with this.

Working with a login system that authenticates the user and kills the session off but currently it's not recognizing the variables assigned to the session. After clicking the logout button everything seems like it's working but when doing a direct connect to the Members Page by typing it into the address bar it loads the page instead of redirecting to the login page.

Members.php

<?PHP
session_start();
if (!isset($_SESSION['username'])) {
header('location:login.php');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>The Animator BETA</title>
<style>
//CSS Has been removed as it's lengthy and unrelated to the issue.
</style>
<!--[if lt IE 9]>

        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

    <![endif]-->        

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script>

          $(document).ready(function(){

                $('#login-trigger').click(function(){

                    $(this).next('#login-content').slideToggle();

                    $(this).toggleClass('active');                  



                    if ($(this).hasClass('active')) $(this).find('span').html('&#x25B2;')

                        else $(this).find('span').html('&#x25BC;')

                    })

          });

    </script>
</head>
<body>
<header class="cf">

<nav style="text-align:center"><a href="logout.php">Logout</a></nav>

</header>

<h1 style="text-align:center"> The Animator - BETA</h1>
<hr />
<div id="nav" style="text-align:center">
<ul style="text-align:center">

<li><a href= "#" ><strong>Home</strong></a></li>
<strong><li><a href= "industrial.html" >Industrial</a></li>
<li><a href= "educational.html" >Education</a></li>
<li><a href= "independent.html"  >Independent</a></li>
<li><a href= "emergent.html"  >Emergent</a></li>
<li><a href= "team.html" >Team</a></li>
<li><a href= "project.html"  >Project</a></li>
<li><a href= "budget.html" >Budget</a></li>
<li><a href= "profile.html" >Profile</a></li></strong>

</ul>
</div>
<hr />
<div style="padding-left:19%"><input type="text" value="search" />
<input type="button" value="Search!" name="search"/>
</div>
<div>&nbsp;</div>
<div align="center">
<div><img src="logo.png" width="407" height="345" alt="Logo" usemap="sectors" /></div>

</div>
</div>
<map name="sectors">
  <area shape="rect" coords="72,40,194,165" alt="Industrial" href="industrial.html">
  <area shape="rect" coords="210,38,328,162" alt="Emergent" href="emergent.html">
  <area shape="rect" coords="208,178,331,296" alt="Independent" href="independent.html">
    <area shape="rect" coords="71,177,194,295" alt="Educational" href="educational.html">

</map>
<div>&nbsp;</div>
<div style="text-align:right"></div>
<div>&nbsp;</div>   
<div id="footer"> <hr />
  <p><strong><u><a href="about.html">About The Animator</a> | <a href="contact.html">Contact</a> | <a href="privacy.html">Privacy Policy</a> | <a href="faq.html">FAQ</a></u></strong><u></u></p>
</div>
</body>
</html>

LOGIN PAGE

<?php

error_reporting(E_ALL);
ini_set('display_errors',"On");

include ('database_connection.php');
if (isset($_POST['formsubmitted'])) {
    // Initialize a session:
session_start();
    $error = array();//this aaray will store all error messages


    if (empty($_POST['e-mail'])) {//if the email supplied is empty 
        $error[] = 'You forgot to enter  your Email ';
    } else {


        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {

            $Email = $_POST['e-mail'];
        } else {
             $error[] = 'Your Email Address is invalid  ';
        }


    }


    if (empty($_POST['Password'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $Password = $_POST['Password'];
    }


       if (empty($error))//if the array is empty , it means no error found
    { 



        $query_check_credentials = "SELECT * FROM account WHERE (email='$Email' AND passwords='$Password')";



        $result_check_credentials = mysqli_query($dbc, $query_check_credentials);
        if(!$result_check_credentials){//If the QUery Failed 
            echo 'Query Failed ';
        }

        if (@mysqli_num_rows($result_check_credentials) == 1)//if Query is successfull 
        { // A match was made.





            $_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable

            $_SESSION['email'] = $Email; 

           session_start("username");

            header("Location: members.php");



        }else
        { 

            $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
        }

    }  else {



echo '<div class="errormsgbox"> <ol>';
        foreach ($error as $key => $values) {

            echo '  <li>'.$values.'</li>';



        }
        echo '</ol></div>';

    }


    if(isset($msg_error)){

        echo '<div class="warning">'.$msg_error.' </div>';
    }
    /// var_dump($error);
    mysqli_close($dbc);

} // End of the main Submit conditional.



?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Form</title>





<style type="text/css">
body {
    font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
    font-size:12px;
}
.registration_form {
    margin:0 auto;
    width:500px;
    padding:14px;
}
label {
    width: 10em;
    float: left;
    margin-right: 0.5em;
    display: block
}
.submit {
    float:right;
}
fieldset {
    background:#EBF4FB none repeat scroll 0 0;
    border:2px solid #B7DDF2;
    width: 500px;
}
legend {
    color: #fff;
    background: #80D3E2;
    border: 1px solid #781351;
    padding: 2px 6px
}
.elements {
    padding:10px;
}
p {
    border-bottom:1px solid #B7DDF2;
    color:#666666;
    font-size:11px;
    margin-bottom:20px;
    padding-bottom:10px;
}
a{
    color:#0099FF;
font-weight:bold;
}

/* Box Style */


 .success, .warning, .errormsgbox, .validation {
    border: 1px solid;
    margin: 0 auto;
    padding:10px 5px 10px 60px;
    background-repeat: no-repeat;
    background-position: 10px center;
     font-weight:bold;
     width:450px;

}

.success {

    color: #4F8A10;
    background-color: #DFF2BF;
    background-image:url('images/success.png');
}
.warning {

    color: #9F6000;
    background-color: #FEEFB3;
    background-image: url('images/warning.png');
}
.errormsgbox {

    color: #D8000C;
    background-color: #FFBABA;
    background-image: url('images/error.png');

}
.validation {

    color: #D63301;
    background-color: #FFCCBA;
    background-image: url('images/error.png');
}



</style>

</head>
<body>


<form action="login.php" method="post" class="registration_form">
  <fieldset>
    <legend>Login Form  </legend>

    <p>Enter Your username and Password Below  </p>

    <div class="elements">
      <label for="name">Email :</label>
      <input type="text" id="e-mail" name="e-mail" size="25" />
    </div>

    <div class="elements">
      <label for="Password">Password:</label>
      <input type="password" id="Password" name="Password" size="25" />
    </div>
    <div class="submit">
     <input type="hidden" name="formsubmitted" value="TRUE" />
      <input type="submit" value="Login" />
    </div>
  </fieldset>
</form>
<button onclick="window.location='theanimator.html';">Go Back!</button>
</body>
</html>

LOGOUT

<?php
unset($_SESSION['email']);
header('Location: login.php');
exit;
?>

解决方案

your logout page has to be this:

<?php

//start the session
session_start();
//this will destroy the session that is started
session_destroy();
header('Location: login.php');
exit;
?>

The problem was, you didn't destroy the session so the session exist when you go back to page.

Also you where looking checking on $_SESSION['username'] but you unset $_SESSION['email'] that is never going to work.

这篇关于取消全局会话变量作为注销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆