我正在php中实现注销功能。当我单击注销链接时,会话被销毁(使用未设置($ _ SESSION [' user'])和session_destroy())。在此之后,页面被重定向到登录屏幕。我以前尝试过这个作为php中的独立应用程序并且它正常工作。但是当我在我的实际代码中实现它时,只有一半部分正在工作,即会话被破坏但后退按钮显示用户配置文件页面。
userprofile.php
<?php
session_start();
if(!isset($_SESSION['CurrentUser']) && $_SESSION['CurrentUser']="")
{
header('Location:/login.html');
}
else{
if(isset($_POST['submit']))
{
include('Config.php');
$UserId=$PostContent=$Visibilty="";
$Vi=$_POST['Vi'];
$Us=$_POST['Us'];
$Po=$_POST['Po'];
$CreateDate = date("Y/m/d");
$insert="insert into Post(Vi,Po,Us,CreateDate) values('".$Vi."','".$Po."','".$Us."','".$CreateDate."')";
$insertresult=mysql_query("$insert");
if($insertresult)
{
header('Location:/userprofile.php');
}
else
{
echo "problem inserting data";
}
}
if(isset($_SESSION['CurrentUser']))
{
$user = $_SESSION["CurrentUser"];
//echo $user;
}
include("Config.php");
$select = "select Post.*, concat(registration.Firstname,' ',registration.Lastname) as Name, about.ProfilePic from post LEFT JOIN about On Post.UserId=About.UserId inner join registration on registration.Id = post.UserId where post.UserId ='".$user."' order by post.PostId desc LIMIT 5";
$selectResult = mysql_query($select);
//echo $selectResult;
include("refrences.php");
?>
<style>
body
{
background-color:lightgrey;
}
li.hover a:hover i.hover
{
background-color:pink;
}
</style>
<body>
<?php
include("Nav.php");
?>
<div class="container" style="background-color:whitesmoke;">
<form action="" method="post" class="form-horizontal">
<div class="col-md-8 col-md-offset-3">
<i class="fa fa-share-square-o" style="color:black"> Status </i>
<i class="fa fa-image" style="color:black"> Add Photo</i>
<i class="fa fa-file-photo-o" style="color:black">  Add Album</i>
</div>
<input type="hidden" name="UserId" value=<?php echo $user;?>>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<textarea class="form-control" rows="2" name="PostContent" placeholder="What's on your mind???..."></textarea>
</div>
<div class="col-md-7 col-md-offset-3">
<div class="col-md-5">
<i class="fa fa-user-plus" style="color:black">Tag Friends</i>
<i class="fa fa-map-marker" style="color:black">  Location</i>
<i class="fa fa-smile-o" style="color:black"> Symbols</i>
</div>
<div class="col-md-2">
<label class="control-label" style="">Share with</label></div>
<div class="col-md-1">
<select class="form-control" id="select" name="Visibilty">
<option value="Friends">Friends</option>
<option value="Public">Public</option>
</select></div>
<div class="col-md-2 col-md-offset-1">
<input type="submit" value="Post" name="submit" class="btn btn-success">
</div>
</div>
</form>
<?php
if(mysql_num_rows($selectResult) > 0)
{
while($fetch = mysql_fetch_array($selectResult))
{
?>
<div class="col-md-8 col-md-offset-2 well" style="background-color:white;">
<div class="col-md-2 thumbnail">
<img src="ProfilePic\<?php echo $fetch['ProfilePic']; ?>" alt="<?php echo $fetch['ProfilePic']; ?>">
</div>
<div class="col-md-3">
<p><a href="#"><b style="color:darkred;"><?php echo $fetch['Name']; ?></b></a></p>
<span><?php echo $fetch['PostContent']; ?></span></br>
<i class="fa fa-thumbs-o-up">Like,</i>
<i class="fa fa-share">share</i>
<span><?php echo $fetch['Visibilty']; ?></span>
<span><?php echo $fetch['CreateDate']; ?></span>
</div>
</div>
<?php
}
}
else
{
?>
<div class="col-md-8 col-md-offset-2">
<div class="alert alert-warning text-center">
Nothing to share..!!
</div>
</div>
<?php
}
?>
<div class="col-md-3 col-md-offset-5">
<input type="submit" value="See More......" name="submit" class="btn btn-success">
</div>
</div>
</body>
<?php
}
?>
答案 0 :(得分:0)
如何破坏代码中的会话?例如,如果您正在使用另一个文件(即logoff.php),请确保包含session_start();在摧毁会议之前。
例如:
<?php
session_start();
session_unset();
session_destroy();
unset($_SESSION);
?>
此外,您的代码在第2行有一个错误:
$_SESSION['CurrentUser']=""
应该是:
$_SESSION['CurrentUser']==""