停止直接访问.html页面,重定向到login.php

时间:2015-08-04 16:33:44

标签: php session reference

如果会话已经发生,我一直在寻找答案,以便在根目录(或外部)中制作所有其他.html页面。如果没有直接访问者返回index.php(登录名为,请在public_html中设置)。 有没有人为此工作?如果是这样,我会把"如果没有会话重定向"? 因此,如果有人直接访问www.site.com/here.html,那么php会运行并查看会话是否已经设置,如果没有重定向到www.site.com/login.php。

2 个答案:

答案 0 :(得分:0)

使用此:

<?php
if(!session_id()){
    header('Location: /index.php');
    exit;
}
?>

只需将其放在html文件的顶部

即可

或者为安全起见,检查会话中的索引是否设置为:

if(!isset($_SESSION['user'])){
    // redirect code
}

为此,在用户登录时,您需要设置

$_SESSION['user'] = 'something'; // a real value here!

答案 1 :(得分:0)

好的亚历克斯,这就是我想出来的但尚未运行它。只是在寻找我是否能够接近。 当然,我不是,这是我第一次将这种功能添加到网站。

<?php
    function menuOptions($menuOptions)
  {
    $menu1 = array(
        'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
        'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
        'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
        'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
        'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
        'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
    );
    $menu2 = array(
        'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
        'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
        'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
        'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
        'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
        'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),   
        'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
        'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
        'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
    );
    $menu3 = array(
        'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
        'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
        'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
        'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
        'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
        'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),   
        'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
        'MarketingExpenseReports' =>('text'=>"Marketing Expense Reports", 'MarketingExpenseReports.php'),
        'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
        'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
    );
    $menuAdmin = array(
        'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
        'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
        'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
        'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
        'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
        'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),   
        'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
        'MarketingExpenseReports' =>('text'=>"Marketing Expense Reports", 'MarketingExpenseReports.php'),
        'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
        'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
    );
}


    function aprovedusers($aprovedusers)
{
    // setting aproved users list 1 for sales department
    $aprovedusers1=array("user1");

    // setting aproved users list 2
    $aprovedusers2=array("user1","user2");

    // setting aproved users list 3 for marketing department
    $aprovedusers3=array("user1","user2","user3");

    // setting aproved users list admin for admin
    $aprovedusersAdmin=array("Admin1");

    $msg1 = echo "hello";
    $msg2 = echo "must login";
    $msg3 = echo "hello Marketing";
    $msg4 = echo "hello Admin";

}

function getloggedin($userloggedin)
{
    if($_SESSION['userlogin'] == ''){
    echo $msg1 'userlogin';
    } else {
    header("Location: logout.php");
}
echo $msg2;




  echo '<div id="'. $userlogin .'">';

 ?>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Markting Home</title>
 </head>
 <header>
   <div id="Menu">
    <div id="<?php echo $userloggedin ?>" >
        <?php //show only approved menu for what menu user is approved for ?>
        <div id="<?php echo $menuOptions ?>" >
            <div><?php echo $menu1?></div>
            <div><?php echo $menu2?></div>
            <div><?php echo $menu3?></div>
            <div><?php echo $menuAdmin?></div>
        </div>
    </div>
   </div>
     <body>
       <div id="content">
     </div>
   </body>
  </header>

如果你有任何指针可以做得很好的话,我会整晚都在努力学习!