$_SESSION 启用内容 - PHP [英] $_SESSION enable content - PHP

查看:39
本文介绍了$_SESSION 启用内容 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 页:-登录-家- 另一页

I have 3 pages: -login -home -another page

在登录一中,我初始化了$_SESSION:

In the login one, I initialize the $ _SESSION:

$_SESSION['admin'];

提交登录表单后,重定向到主页在文件顶部,我的代码是:

after submit login form, redirecting to home in the top of file, my code is:

home.php:

<?php session_start(); ?>
<?php if(isset($_SESSION['admin'])) {?>

write content if $_SESSION is true

<?php }else{ ?>

write content if $_SESSION it's false

<?php } ?>

这一次是和一次都很好,如果然后从家里,我去另一个页面,总是按照这个代码,我总是得到错误的结果

and this works well once yes and no once, if then from the home, I go to the other page, always following this code, I always get the false result

如何在登录页面中初始化 $_SESSION 以使其始终有效?

How do I initialize the $ _SESSION in the login page to make it always valid?

我还以为是未完成的$_SESSION问题

I thought it was also an unfinished $ _SESSION problem

logout.php:

logout.php:

<?php
session_unset('admin');
header("location: index.php?sd=Y");
?>
<script type="text/javascript">
window.location = "index.php";
</script>   

推荐答案

1st 如果用户输入的用户名和密码与带有 $_SESSION 的数据库记录匹配,您应该保存您想要进一步使用的用户凭据

1st You should save user credentials that you wants for further usage if User inputs for username and password matched with DB records with $_SESSION

    include("..\includefiles\db.php");

    $email =$_POST["email"];
    $password = $_POST["password"];

    $sql = "SELECT * FROM member WHERE email ='$email' AND password =  '$password'";
    $result = mysqli_query ($con,$sql);
    if($row = mysqli_fetch_array($result)){


     $_SESSION['ID'] = $row['id'];
     $_SESSION['NAME'] = $row['name'];
     $_SESSION['ROLE'] = $row['role'];

     if($_SESSION['ROLE']=='a'){

      header("Location: ..\dashBoard.php");
    }else{
      header("Location: ..\index.php");
    }

保存会话后,您所要做的就是使用

After saving sessions all you have to do is to start the session with

session_start()

session_start()

在每个页面中的功能.请注意,session_start() 函数必须是文档中最重要的 功能.在任何 HTML 标记之前.然后检查会话的可用性,如果会话未设置重定向到登录页面.见下面的代码.

function in each page. Please note that session_start() function must be the very first thing in your document. Before any HTML tags.And then check the availability of the session and if session not set redirect to the login page. See the below code.

 <?php
    session_start();

   if(isset($_SESSION["ROLE"])){    

   }else{
           header("Location: login.php");
       }

   ?>   
      <!DOCTYPE html>

希望你能从中有所收获.

Hope that you may able to grab something from this.

这篇关于$_SESSION 启用内容 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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