$ _Session“并发症";在登录和注销php [英] $_Session "complication" on login and logout php

查看:73
本文介绍了$ _Session“并发症";在登录和注销php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学做一个数据库项目,而我在这里遇到了一个问题. 我试图在没有会话时显示登录",而在有会话时显示注销". 但是实际上即使登录后,它仍然显示我登录",并且我尝试了print_r($ _ SESSION);它告诉我变量未定义

I'm doing a database project for university and I'm strugling with a problem in here. I'm trying to show "log in" when there's no session and "log out" when there is a session. But actually even after I log in it still shows me "log in" and I tried to print_r($_SESSION); and it tells me that the variable is undefined

我有一个注销文件:

<?php
session_start();
session_destroy();
header("Location:index.php");
?>  

和一个init.php文件:

and a init.php file:

<?php
session_start();
?> 

当我登录时会调用

init.php.

init.php is called when I log in.

这是index.php的一部分:

Here is a part of index.php:

<?php
    require 'db/connect.php';
    require 'functions/security.php';
    ?>

    <html>
        <head>
            <title>Home</title>
            <meta charset="UTF-8">
            <link rel="stylesheet" href="css/common.css">
        </head>
        <body>
            <div id="linking">      
    <?php
    //print_r($_SESSION);
    if (session_status() === PHP_SESSION_NONE) {
        ?>
                    <a href="login.php">Log In</a>           
                    <?php
                } else {
                    ?>
                    <a href="logout.php">Log Out</a>
                    <?php
                }
                ?>
                <a href="new_customer.php">Register</a>
                <a href="trips.php">Search for trips</a>
            </div>
            <hr>
            <section id="section">

推荐答案

在登录页面上登录成功后.

when your login is success on login page.

session_start();
$_SESSION['user_logged_in'] = true;

在您的注销页面

session_start();
unset($_SESSION['user_logged_in']);   
session_destroy();

在您的主页上

<?php 
   session_start();
   if(isset($_SESSION['user_logged_in'])) {
 ?>
   < a href='logout.php'>Logout</a>
<?php  
   }
else {
?>
    < a href='login.php'>Login</a>
<?php 
  }
 ?> 

这篇关于$ _Session“并发症";在登录和注销php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆