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 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件