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 ';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据