-1

我有一个问题是我在主页上并且我登录然后我转到另一个页面并且我不再看到存储的会话,但是当我点击登录时它说我已经登录了。但是如果我去另一个目录,例如 /admin/ 会话数据仍然存在。任何解决方案?

登录.php

<?php 
session_start(); 
require_once('connections/Main.php');

if($_SESSION['username']) {

    echo '<div class="error_message">Attention! '.$_SESSION['username'].'. You  are already logged in.</div>';
    echo "<br />";
    echo "Go <a target='top' href='index.php'>back</a> to the page you were viewing before this.</li>";

    exit();
}

// Has an error message been passed to login.php?
$error = $_GET['e'];

if($error == 1) {
    $error = '<div class="error_message">Attention! You must be logged in to view this page.</div>';
}

// Only process if the login form has been submitted.

if(isset($_POST['login'])) {

    $username = $_POST['username']; 
    $password = $_POST['password']; 

        if (empty($username) || empty($password)) { 
        $error = '<div class="error_message">Attention! Please enter your Username and Password.</div>';
    } else { 

     mysql_select_db($database_Main);
    // Add slashes to the username and md5() the password 
    $user = mysql_real_escape_string(addslashes($_POST['username'])); 
    $pass = mysql_real_escape_string(md5($_POST['password'])); 


    $sql = "SELECT * FROM  users WHERE username = '$user' AND password= '$pass' OR email = '$user' AND password= '$pass' "; 

    $result = mysql_query($sql);

    // Check that at least one row was returned 
    $rowCheck = mysql_num_rows($result); 

    if($rowCheck > 0) { 
    while($row = mysql_fetch_array($result)) { 

      // Start the session and register a variable 
      if(isset($_POST['remmemberme'])){
          setcookie(kitbusername, $user,  time()+(3600*24*30));
      }
      $_SESSION['username'] = $user;
      //session_register('username'); 

      echo '<script>parent.changeURL("index.php"); </script>';

      } 

      } else { 

      // If nothing is returned by the query, unsuccessful login code goes here... 

      $error = '<div class="error_message">Attention! Incorrect username or password.</div>'; 
      } 
    }
}
echo $error; ?>

每个页面上的 header.php

<div id="loginoutbox">
<?php session_start(); 
  if($_SESSION["username"]) { ?>
<div>
  Hello <?php echo $_SESSION['username']; ?>! | </div>
<a id="register" href="http://www.kdogisthebest.webege.com/admin/" class="button">Dashboard</a> |
<a id="signin" href="#fallr-confirmlike" class="button">Logout</a>      
<?php }else{ ?>
<div style="display: inline-block; font-size: 14px; padding-left: 20px;">Hello Guest!</div>
<a id="register" href="#fallr-register" class="button">Register</a> |
<a id="signin" href="#fallr-signin" class="button">Login</a>
<?php } ?>
</div>

演示 ---> http://www.kdogisthebest.webege.com/index.php

用户名 = 演示用户

密码 = 演示用户

4

2 回答 2

1

<?php session_start();需要任何输出之前。将其放在页面顶部。

于 2012-07-09T15:45:54.980 回答
0

好的,在使用 var_dump 转储 $_SESSION 变量后,我看到更改页面后,变量显示为 NULL。我进行了研究并进行了研究,我认为最好得出结论,它是导致它以这种方式运行的 php 服务器配置。

于 2012-07-09T16:41:13.033 回答