2015-04-22 101 views
0

我在php中實現註銷功能。當我點擊註銷鏈接時,會話被銷燬(使用unset($ _ SESSION ['user'])和session_destroy())。之後,頁面被重定向到登錄屏幕。我之前在php中作爲一個獨立的應用程序嘗試過,它工作正常。但是當我在我的實際代碼中實現它時,只有一半正在工作,即會話被破壞,但後退按鈕顯示用戶配置文件頁面。點擊後退按鈕,用戶資料頁面打開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"> &nbsp;Status </i>&nbsp;&nbsp; 
<i class="fa fa-image" style="color:black">&nbsp;Add Photo</i>&nbsp;&nbsp; 
<i class="fa fa-file-photo-o" style="color:black">&nbsp 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>&nbsp; 
<i class="fa fa-map-marker" style="color:black">&nbsp Location</i>&nbsp; 
<i class="fa fa-smile-o" style="color:black">&nbsp; 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

你需要某種形式的檢查增加每個頁面(或框架)確保用戶在訪問頁面時登錄。 – Frank

+0

$ _SESSION ['CurrentUser'] =「」缺少一個額外的=,它應該看起來像$ _SESSION ['CurrentUser'] ==「」,否則你只需使用exit();爲它的良好做法賦值 – Epodax

+0

。標題重定向後 – user1844933

回答

0

你如何破壞你的代碼的會議?例如,如果您正在使用其他文件(即logoff.php),請確保包含session_start();在銷燬會話之前。

例如:

<?php 
session_start(); 
session_unset(); 
session_destroy(); 
unset($_SESSION); 
?> 

而且,你的代碼有第2行中的錯誤:

$_SESSION['CurrentUser']="" 

應該是:

$_SESSION['CurrentUser']=="" 
相關問題