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 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类