douchen7324 2016-10-02 16:10
浏览 67
已采纳

如何在php中设置登录和注销会话?

I have created session and destroying session in logout.php but if i entered in url(http://localhost/demo/home.php)it showing loggedin.It should be redirect on index.php or display page not found.

What i am achieving- I have login section and there is no issue in that.I am able to login with my credentials and page is redirecting on home.php successfully.From home.php i have logout link and i clicked on that page is redirecting on index.php but if i entered home.php showing loggedin.. Please help me in this.

index.php

session_start();

$username = $_POST['username'];
 $password = $_POST['password'];

$sel_user = "SELECT * FROM admin WHERE Username='$username' and Password='$password'";
$run_user = mysqli_query($conn, $sel_user);

$check_user = mysqli_num_rows($run_user);

if($check_user>0){

echo "<script>window.open('home.php','_self')</script>";
$_SESSION['user_email']=$username;

}

else {

$msg="Username and Password is incorrect.";


}

Home.php

<h2>Home page</h2>
<a href="logout.php">logout</a>

logout.php

       <?php
         session_start();
        if(session_destroy())
                {
                header("Location: index.php");
                }
                ?>
  • 写回答

2条回答 默认 最新

  • dongzhan9100 2016-10-02 16:34
    关注

    Your home.php should check if user is logged in or not. Just add if statement at the top.

    Something like:

    if (isset($_SESSION['user_email']) == FALSE){
         header("Location: index.php");
    } 
    

    also your logout.php just creates session and then checks if it's destroyed. For that you could just:

    unset($_SESSION['user_email']);
    

    and your home.php would just redirect, since this var is not declared anymore.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化