duankang8114 2012-07-09 15:44
浏览 66
已采纳

会话数据在页面之间丢失

I'm having a problem were Im on the main page and I login then I go to a different page and I don't see the stored session anymore, but when i click login it says Im already logged in. But if I go to a different directory such as /admin/ the Session data is still there. Any Solutions?

login.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 on every page

<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>

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

Username = demouser

Password = demouser

  • 写回答

2条回答 默认 最新

  • doukan4795 2012-07-09 16:41
    关注

    Ok after dumping the $_SESSION variable with var_dump i see that After changing pages the variable shows up as NULL. I goggled and did research and I think its best to conclude that it is a php server configuration that is causing it to act this way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员