代码之家  ›  专栏  ›  技术社区  ›  littlegreen

在本地主机上使用wampserver 2.0进行页面加载时,PHP会话数据丢失

  •  2
  • littlegreen  · 技术社区  · 15 年前

    我在我的网站上有一个使用$\u会话变量的PHP身份验证系统。

    表单向文件“login.php”提交用户名和密码。处理方法如下:

    <?php include '../includes/sessionstart.inc.php'; ?>
    <?php ob_start(); ?>
    
    if($_POST){
        $q = mysql_query("SELECT id, company FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."' AND password = '".md5($_POST['password'])."'");
        if(mysql_num_rows($q) >= 1){
            $f = mysql_fetch_Array($q);
            $_SESSION['company'] = $f['company'];
            $_SESSION['id'] = $f['id'];
            $_SESSION['logedin'] = true;
            session_write_close();
    
            ob_clean();
            header("Location: index.php");
    
    }
    

    然后,加载index.php并检查“logedin”是否为真。

    <?php include '../includes/sessionstart.inc.php'; ?>
    <?php if(!isset($_SESSION['logedin'])) header('Location: login.php'); ?>
    

    在我的生产服务器上,它继续运行,但在我的wampserver上,它恢复为login.php。我注意到wampserver页面加载非常慢,这可能与它有关。这就是为什么我将会话写入关闭包括在内,以确保在页面切换之前保存会话数据,但这没有帮助。

    session\u start.inc.php的内容很简单:

    <?php
        session_start();
    ?>
    

    我以前在里面有更多的代码,但现在只是这个。在我开始使用include文件之前,这个问题也存在。

    有人知道我做错了什么吗?为什么wampserver不将我的会话数据传输到下一个php文件?

    7 回复  |  直到 9 年前
        1
  •  6
  •   ajtrichards    12 年前

    $_SESSION

    C:\wamp\bin\apache\apache2.4.2\bin\php.ini
    session.cookie_domain =
    session.use_cookies = 1
    session.save_path = "c:\wamp\tmp"   ;ensure the \ is used not /
    

    <?PHP
    session_start();
    $_SESSION['SESS_MEMBER_ID'] = 'stored variable';
    session_write_close();
    header("location:print.php");
    ?>
    

    <?PHP
    session_start();
    var_dump($_SESSION);
    ?>
    

    var_dump()

    c:\wamp\tmp

        2
  •  2
  •   Martijn    15 年前

    logedin

    <?php
      include 'login.inc.php';
    
      if(authorized()) {
        // put some more script here, if needed
        ?>
        // put some plain HTML here  
        <?php
      }
    ?>
    

    login.inc.php authorized $_SERVER['PHP_SELF'] login_submit

        3
  •  1
  •   Álvaro González    15 年前

    if($_POST){...}
    

    if( isset($_POST['username']) && isset($_POST['password']) ){...}
    

    exit() header()

        4
  •  1
  •   staticboy    13 年前

    session.use_only_cookies php.ini

    session.use_only_cookies = 1
    

    session.use_only_cookies = 0
    

    <?php
    // page1.php
    
    ini_set('session.use_cookies', '0');
    session_start();
    
    $_SESSION['time'] = time();
    
    echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
    ?>
    

    C:\wamp\tmp\sess_0rkdlonl5uia717rf03d4svs16

    page2.php?PHPSESSID=0rkdlonl5uia717rf03d4svs16

    <?php
    // page2.php
    
    ini_set('session.use_cookies', '0');
    session_start();
    
    echo date('Y m d H:i:s', $_SESSION['time']);
    
    echo '<br /><a href="page1.php?' . SID . '">page 1</a>';
    ?>
    

    session_start();

    ini_set('session.use_only_cookies', '0');
    

    ; This option forces PHP to fetch and use a cookie for storing and maintaining
    ; the session id. We encourage this operation as it's very helpful in combatting
    ; session hijacking when not specifying and managing your own session id. It is
    ; not the end all be all of session hijacking defense, but it's a good start.
    ; http://php.net/session.use-only-cookies
    session.use_only_cookies = 0
    

        5
  •  0
  •   littlegreen    14 年前

    <?php
    ini_set('session.cookie_domain', (strpos($_SERVER['HTTP_HOST'],'.') !== false) ? $_SERVER['HTTP_HOST'] : '');
    ?>
    

    who posted it here

        6
  •  0
  •   Alexander Tobias Bockstaller    10 年前

    session_regenerate_id(true);
    

        7
  •  0
  •   user3157444    9 年前