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 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线