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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?