欢迎登录用户问题

时间:2012-12-07 18:34:24

标签: php

我正在尝试使用PHP登录系统在限制区域中向用户添加欢迎。我使用此代码将usernale从第一个用户登录页面转移到受限制的页面,但它没有工作

if ( (isset($_POST['username'])) || (isset($_POST['password'])) { 
                   $user = $_SESSION['username'];
      } 

以下是我正在使用的文件,你能看看它们,让我知道我做错了什么吗?

<form id="login-form" method="post" action="includes/login.inc.php"> 
<fieldset> 
  <legend>Login to Web Site</legend> 
   <label for="username"> 
    <input type="text" name="username" id="username" />Username: 
  </label> 
  <label for="password"> 
    <input type="password" name="password" id="password" />Password: 
  </label> 
  <label for="submit"> 
    <input type="submit" name="submit" id="submit" value="Login" /> 
  </label> 
</fieldset> 

我有一个php登录文件,如下所示:

<?php 
 require_once('config.inc.php'); 
 require_once('functions.inc.php'); 

 // Start session 
 session_start(); 

 // Check if user is already logged in 
 if ($_SESSION['logged_in'] == true) { 
          // If user is already logged in, redirect to main page 
          redirect('../index.php'); 
  } else { 
          // Make sure that user submitted a username/password and username only consists of alphanumeric chars 
          if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR 
               (!ctype_alnum($_POST['username'])) ) { 
                        redirect('../login.php'); 
          } 

          // Connect to database 
          $mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 

          // Check connection 
          if (mysqli_connect_errno()) { 
                        printf("Unable to connect to database: %s", mysqli_connect_error()); 
                        exit(); 
          } 

          // Escape any unsafe characters before querying database 
          $username = $mysqli->real_escape_string($_POST['username']); 
          $password = $mysqli->real_escape_string($_POST['password']); 

          // Construct SQL statement for query & execute 
          $sql              = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'"; 
          $result = $mysqli->query($sql); 

          // If one row is returned, username and password are valid 
          if (is_object($result) && $result->num_rows == 1) { 
                        // Set session variable for login status to true 
                        $_SESSION['logged_in'] = true; 
                        redirect('../index.php'); 
          } else { 
                        // If number of rows returned is not one, redirect back to login screen 
                        redirect('../login.php'); 
          } 
 } 
 ?> 

在我的限制页面上,我有:

<?php 
 // Start session 
 session_start(); 
 if ( (isset($_POST['username'])) || (isset($_POST['password'])) { 
                       $user = $_SESSION['username'];
          } 
  require_once('includes/functions.inc.php'); 
 if (check_login_status() == false) { 
          redirect('login.php'); 
  } 
 ?> 
 <!DOCTYPE html> 
<html> 
<body>
<div id="page">

  <a class="welcome">
    Welcome:  <?php echo $user; ?>
  </a>
 </div>

1 个答案:

答案 0 :(得分:1)

使用 $ _SESSION ['username'] = $ _POST ['username'];

然后回显$ _SESSION ['用户名']

修改

<?php 

// Start session 
session_start(); 

if (isset($_POST['username']) || isset($_POST['password'])) { 
    require_once('includes/functions.inc.php'); 
    if (check_login_status() == false) { 
        redirect('login.php');
    }
    $_SESSION['username'] = $_POST['username'];
} 

?> 

<!DOCTYPE html> 
<html> 
<body>
<div id="page">

  <a class="welcome">
    Welcome:  <?php echo $_SESSION['username']; ?>
  </a>
 </div>