PHP会话不起作用

时间:2012-02-28 01:59:37

标签: php ajax session session-cookies

这一次,我需要你帮助解决与php相关的问题。我的用户脚本没有按预期工作,它应该保持记录3周,但这只是失败,在不到60分钟后会话被销毁,我需要再次登录,任何建议?

我的代码:

<?php
if (!isset($_SESSION)) session_start(); 

mysql_connect("YOU", "DONT", "NEED") or die("database connection failed");
mysql_select_db("THIS!!!") or die("database selection failed");

$user = $_POST['username'];
$pass = $_POST['password'];
$remember = $_POST['remember'];

$token = $_POST['login-token'];
$error;

        if(isset($_SESSION['username'])) {
            $error = $error." :erlgd:";
        }
        if(empty($user)){
            $error = $error." :erusr:";
        }
        if(empty($pass)){
            $error = $error." :erpwd:";
        } else
        $password = md5($pass);

        if(empty($error)){
            $sql = "SELECT * FROM login_users WHERE username='$user' AND password='$password'";
            $result = mysql_query($sql);
            $count = mysql_num_rows($result);
            if($count == 0){
                $sql = "SELECT * FROM login_users WHERE email='$user' AND password='$password'";
                $result = mysql_query($sql);
                $count = mysql_num_rows($result);
                if($count == 0){
                    $error = $error.":erwrg:";
                }
            }

        }


    // Once everything's filled out


        // Just double check there are no errors first
        if($error == '') {
            while($row = mysql_fetch_array($result)) {
                $minutes = 10080;

                if($minutes == 0)
                    ini_set('sesion.cookie_lifetime', 0);
                else
                    ini_set('session.cookie_lifetime', 60 * $minutes);
                session_regenerate_id();

                $sql = "SELECT * FROM login_activate WHERE username='$user'";
                $count = mysql_num_rows(mysql_query($sql));
                if ($count > 0)
                    $_SESSION['activate'] = 1;
                else
                    $_SESSION['activate'] = 0;

                $_SESSION['restricted'] = $row['restricted'];
                $_SESSION['name'] = $row['name'];
                $user_level = unserialize($row['user_level']);
                $_SESSION['user_level'] = $user_level;
                $sql = "SELECT level_disabled FROM login_levels WHERE level_level = '$user_level'";
                $disRow =  mysql_fetch_array(mysql_query($sql));
                $_SESSION['level_disabled'] = $disRow['level_disabled'];

                if(!empty($remember)) {
                    ini_set('session.cookie_lifetime', 60*60*24*100); // Set to expire in 3 months & 10 days
                    session_regenerate_id();
}
                // And our magic happens here ! Let's sign them in
                $_SESSION['username'] = $row['username'];

                unset($_SESSION['token']);
                echo "success";
                // Redirect after it's all said and done

            }

        }else{
            echo "error:".$error;
        }



?>

谢谢!

PS:作为附加数据,这是一个共享服务器。

3 个答案:

答案 0 :(得分:1)

查看/etc/php5/apache2/php.ini中的session.gc_maxlifetime

要么在那里更新,要么尝试将ini_set('session.gc_maxlifetime', 60*60*24*7*3);放在脚本的顶部

将其增加到更大的

基本上,这是服务器在清除会话文件之前等待的时间

答案 1 :(得分:1)

您需要使用Cookie来支持remember me类型的行为。当在服务器上销毁会话时,cookie将作为催化剂,以便当他们重新访问您的站点并在幕后自动登录时,以同一用户的身份开始新会话。

Google搜索会返回一些非常好的结果,最明显的是reference到实际的Cookie代码(搜索autologin.php)。

答案 2 :(得分:0)

通常人们使用session_set_cookie_params()来更改会话cookie的生命周期。你应该在调用第一个session_start()之前设置它,我相信。默认设置通常设置为0(即用户关闭浏览器窗口时)。

http://www.php.net/manual/en/function.session-set-cookie-params.php

您还需要更改会话文件的垃圾回收最大生命周期。在会话开始之前,请尝试:

ini_set(’session.gc_maxlifetime’, $lifetime_in_seconds);