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

报告相同问题?