Php 取消全局会话变量作为注销按钮
嘿,Stackoverflow用户 因为我以前在解决一个问题时能得到一些惊人的帮助,所以我被困的时间比我记忆中的要长,我想我会用这个来对付你 使用对用户进行身份验证并终止会话的登录系统,但目前无法识别分配给会话的变量。单击“注销”按钮后,一切看起来都正常工作,但当直接连接到成员页面时,通过在地址栏中键入该页面将加载该页面,而不是重定向到登录页面 Members.phpPhp 取消全局会话变量作为注销按钮,php,html,sql,Php,Html,Sql,嘿,Stackoverflow用户 因为我以前在解决一个问题时能得到一些惊人的帮助,所以我被困的时间比我记忆中的要长,我想我会用这个来对付你 使用对用户进行身份验证并终止会话的登录系统,但目前无法识别分配给会话的变量。单击“注销”按钮后,一切看起来都正常工作,但当直接连接到成员页面时,通过在地址栏中键入该页面将加载该页面,而不是重定向到登录页面 Members.php <?PHP session_start(); if (!isset($_SESSION['username'])) { h
<?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('▲')
else $(this).find('span').html('▼')
})
});
</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> </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> </div>
<div style="text-align:right"></div>
<div> </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>
动画师贝塔
//CSS已被删除,因为它冗长且与问题无关。
$(文档).ready(函数(){
$(“#登录触发器”)。单击(函数(){
$(this).next(“#登录内容”).slideToggle();
$(this.toggleClass('active');
if($(this.hasClass('active'))$(this.find('span').html('▲;'))
else$(this.find('span').html('▼;'))
})
});
动画师-贝塔
|| |
登录页面
<?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>
您的注销页面必须如下所示:
<?php
//start the session
session_start();
//this will destroy the session that is started
session_destroy();
header('Location: login.php');
exit;
?>
问题是,您没有破坏会话
,因此当您返回页面时,会话存在
您也可以查看查看$\u会话['username']
,但您取消设置的$\u会话['email']
永远不会工作。不幸的是,这不起作用。如果直接加载到“注销”按钮,则在单击“注销”按钮后不应加载“成员”页面。仍然不应加载。即使使用未设置的$\u session['username']也不适用于“成员”页面如果删除此项会发生什么:session\u start(“username”)代码>你不需要它,因为会话已经启动了。我在想,但是稍后在配置文件页面上会需要该会话,该页面用于比较sql数据库的结果,以匹配用户名和电子邮件+名称+密码等。我现在明白了,我们没有启动会话。添加session_start()代码>在会话之前销毁
另请参见答案中的编辑。现在它应该可以工作了:)
<?php
//start the session
session_start();
//this will destroy the session that is started
session_destroy();
header('Location: login.php');
exit;
?>