强制登录用户仅查看已登录的页面

时间:2015-11-17 04:17:15

标签: php redirect login logout

我有一些简单的登录页面。我试图将代码放到页面login.php以尝试是否有效。但是,它没有。

if(!isset($_SESSION['UserData']['Username'])){
    header("location:index.php");
}

我要做的是为登录用户隐藏login.php。换句话说,当他们处于登录状态时,他们会尝试访问login.phplogin.php会自动将他们重定向到指定的登录页面。这意味着login.php页面仅适用于未登录的用户。这是我在login.php页面中的代码。

<?php session_start(); /* Starts the session */
    /* Check Login form submitted */    
    if(isset($_POST['Submit'])){
        /* Define username and associated password array */
        $logins = array('Test1' => 'test1','Test2' => 'test2','Test3' => 'test3');

        /* Check and assign submitted Username and Password to new variable */
        $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
        $Password = isset($_POST['Password']) ? $_POST['Password'] : '';

        /* Check Username and Password existence in defined array */        
        if (isset($logins[$Username]) && $logins[$Username] == $Password){
            /* Success: Set session variables and redirect to Protected page  */
            $_SESSION['UserData']['Username']=$Username; <-- this line amended to display username properly.
            header("location:index.php");
            exit;
        } else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
?>
<!doctype html>
<html>
<head>
<title>Login Page</title>
<link href="style.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' />
</head>
<body>
<section class="pd">
<div class="login">
<form action="" method="post" name="Login_Form">
    <h1>Login</h1>
      Username:<br/>
      <input name="Username" type="text" class="Input">
      <div class="formgap"></div>
      Password:<br/>
      <input name="Password" type="password" class="Input">
      <div class="formgap"></div>
      <?php if(isset($msg)){?><?php echo $msg;?> 
      <div class="formgap"></div>
      <?php } ?> 
      <div class="formgap"></div>
      <input name="Submit" type="submit" value="Login" class="Button3">
</form>
</div><!--login-->
</section>
</body>
</html>

登录后我尝试访问login.php。我仍然可以。 有什么建议吗?谢谢!

4 个答案:

答案 0 :(得分:1)

只需在login.php

上查看即可
if(isset($_SESSION['UserData']['Username']) && !empty($_SESSION['UserData']['Username'])){
    header("location:index.php");
}

如果用户已登录,则会将用户重定向至index.php

答案 1 :(得分:1)

这就够了,试试这个:

if(!empty($_SESSION['UserData']['Username'])){
    header("location:index.php");
}

答案 2 :(得分:1)

你要检查是否设置了userdata。试试这个

<?php 
 session_start(); /* Starts the session */
/* Check Login form submitted */    
if(isset($_SESSION['UserData']['Username'])){
     header("location:im.php");
        exit;
}
elseif(isset($_POST['Submit'])){
    /* Define username and associated password array */
    $logins = array('Test1' => 'test1','Test2' => 'test2','Test3' => 'test3');

    /* Check and assign submitted Username and Password to new variable */
    $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
    $Password = isset($_POST['Password']) ? $_POST['Password'] : '';

    /* Check Username and Password existence in defined array */        
    if (isset($logins[$Username]) && $logins[$Username] == $Password){
        /* Success: Set session variables and redirect to Protected page  */
        $_SESSION['UserData']['Username']=$logins[$Username];
        header("location:im.php");
        exit;
    } else {
        /*Unsuccessful attempt: Set error message */
        $msg="<span style='color:red'>Invalid Login Details</span>";
    }
}
?>
<!doctype html>
<html>
<head>
<title>Login Page</title>
<link href="style.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">

<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' />
</head>
<body>
<section class="pd">
<div class="login">
<form action="" method="post" name="Login_Form">
 <h1>Login</h1>
  Username:<br/>
  <input name="Username" type="text" class="Input">
  <div class="formgap"></div>
  Password:<br/>
  <input name="Password" type="password" class="Input">
  <div class="formgap"></div>
  <?php if(isset($msg)){?><?php echo $msg;?> 
  <div class="formgap"></div>
  <?php } ?> 
  <div class="formgap"></div>
  <input name="Submit" type="submit" value="Login" class="Button3">
</form>
</div><!--login-->
</section>
</body>
</html>

答案 3 :(得分:0)

如果您在限制页面上进行会话,那么只需像这样检查

<?php 
     if(!isset($_SESSION['UserData']['Username'])){
         ?>
        <script>
        window.location='index.php';
        </script>
        <?php
     }
?> 

使用javascript重定向而不是php标头函数来重定向。

相关问题