登录后php会话数据丢失

时间:2014-03-21 06:23:06

标签: javascript php session-variables

用户登录后,数据会发布到包含以下代码的 login_db.php 文件中         它检查登录并将其存储到会话变量中。         根据登录类型,self.location行将其带到不相关的位置。

在每个location1.php中,location2.php和loction3.php session_start写在每个文件的第一行。

<?php
extract($_POST);
include_once 'login/utils/conn.php';
session_start();
if(isset($_POST['login']))
{ 
     $name = $_POST['name'];
     $password = $_POST['password'];
     $sql = "SELECT ad_pk,ad_uname,ad_pwd,ad_type FROM adminlogin WHERE ad_uname ='$name' AND ad_pwd ='$password'";
     $result = mysql_query($sql);

     if($res = mysql_fetch_array($result))
     {  
        //print_r($_POST);exit;
        if ($res["ad_type"] == 'telecall') 
        {
            $_SESSION["ad_uname"] = $res["ad_type"];
            $_SESSION["adminid"] = $res["ad_pk"];
            echo $dt=date("Y-m-d",time());
            $pk=$res['ad_pk'];
            $uk=$res['ad_uname'];
            echo  $sql_ins_log = "insert into login_info(user_id,user_name,login_date)values('$pk','$uk','$dt')";
            $result_log = mysql_query($sql_ins_log);
            echo "<script>self.location='location1.php';</script>";
        }
        else if ( $res["ad_type"] == 'admin') 
        {
            $_SESSION["ad_uname"] = $res["ad_type"];
            $_SESSION["adminid"] = $res["ad_pk"];
            echo $dt=date("Y-m-d",time());
            $pk=$res['ad_pk'];
            $uk=$res['ad_uname'];
            echo  $sql_ins_log = "insert into login_info(user_id,user_name,login_date)values('$pk','$uk','$dt')";
            $result_log = mysql_query($sql_ins_log);
            echo "<script>self.location='location2.php';</script>";

        }
        else if ( $res["ad_type"] == 'bussuser') 
        {
               $_SESSION["ad_uname"] = $res["ad_type"];
               $_SESSION["adminid"] = $res["ad_pk"];
               echo $dt=date("Y-m-d",time());
               $pk=$res['ad_pk'];
               $uk=$res['ad_uname'];
               echo  $sql_ins_log = "insert into login_info(user_id,user_name,login_date)values('$pk','$uk','$dt')";
               $result_log = mysql_query($sql_ins_log);
               echo "<script>self.location='location3.php';</script>";
        }
        else
        {
                echo "<script>alert('Incorrect username or password');self.location='login.php';</script>";
        } 
    } 
}
?>

在这里,我可以在print_r($ _ SESSION);

上看到会话数据

执行self.location后,控制将转到3个不同的页面。 在页面开头写入session_start行。  会话数据仍然丢失,登录时会显示“拒绝访问”消息。

<?
session_start();
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//echo "helllooo".$_SESSION["ad_uname"];
print_r($_SESSION);// line returns empty array

if($_SESSION["ad_uname"]=='telecall')// if type of logged in user telecall show a different form
{
?>
   // some form displayed and further actions executed.
<? 
} 
else
{
   echo "<script>alert('Access Denied');self.location='login.php';</script>";
}
?>

会话ID似乎改变了..不知道为什么。任何人都可以指导我哪里出错了?为什么session_id在login_db.php文件和location1 / 2 / 3.php文件中返回2个不同的id?

2 个答案:

答案 0 :(得分:0)

Session_start();需要成为页面上的第一件事。在您的第一段代码中看起来并非如此,因此数据永远不会进入会话变量:您不会丢失会话数据,而您永远不会将其放在首位

答案 1 :(得分:0)

Please add session_start at the start of the page.

if still there is a problem with sessions please check phpinfo() for php settings.

    Check session are enabled or not.

    Below are session values for default settings :- 

    **session**
    Session Support     enabled
    Registered save handlers    files user sqlite
    Registered serializer handlers  php php_binary wddx

    Directive   Local Value Master Value
    session.auto_start  Off Off
    session.bug_compat_42   On  On
    session.bug_compat_warn On  On
    session.cache_expire    180 180
    session.cache_limiter   nocache nocache
    session.cookie_domain   no value    no value
    session.cookie_httponly Off Off
    session.cookie_lifetime 0   0
    session.cookie_path /   /
    session.cookie_secure   Off Off
    session.entropy_file    no value    no value
    session.entropy_length  0   0
    session.gc_divisor  100 100
    session.gc_maxlifetime  1440    1440
    session.gc_probability  1   1
    session.hash_bits_per_character 5   5
    session.hash_function   0   0
    session.name    PHPSESSID   PHPSESSID
    session.referer_check   no value    no value
    session.save_handler    files   files
    session.save_path   \xampp\tmp  \xampp\tmp
    session.serialize_handler   php php
    session.use_cookies On  On
    session.use_only_cookies    Off Off
    session.use_trans_sid   0   0