dongyan5706 2017-10-14 04:13
浏览 329
已采纳

PHP在多个页面中使用SESSION

I have 1 SESSION variable that will load when a login form is inserted and it passes the test. But, the variable will only work in one page and when I click on a different page that includes the same file which gives me the SESSION, it doesn't work. It will only work for pages that are linked to the form. I am using the post method. sample.php <- site that is in action="sample.php" therefore its linked.

Beginning code for sample.php

<?php
session_start(); 
require 'php/login_admin.php';
if (isset($_SESSION['admin']))
    echo ' all html code ';

Code for login_admin.php

if ($username == $row['username'] && $password == $row['password'])
        {
            session_set_cookie_params(3000, "/");
            $_SESSION['admin'] = 'open';

        } else {
            session_close();
            echo "Wrong password and username!";
        }

NOTE I have this same set up for all pages and I do not know why only the pages linked directly to the form in the action attribute work.

  • 写回答

1条回答 默认 最新

  • doulun0651 2017-10-14 04:22
    关注

    On all your OTHER pages you only need to test for the admin session and if that fails then redirect to the login page... or display it... whatever you decide. But let's assume we go to a dedicated admin login page for fun...

    So on All your other pages...except the login page...

    <?php
    session_start(); 
    // Is the admin logged in? 
    if (!isset($_SESSION['admin']))
    {
       header("location:admin_login.php");
       exit();
    }
    
    echo ' all html code ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口