在会话变量中存储空值

时间:2013-07-28 23:40:27

标签: php session php4

我有两个PHP个文件,我想在其中存储session变量id,就像这样

if(authentification($login,$mdp) == 0)
                {?>
                <script>alert('logging failed');</script>
                <?php
                header('Location: Contacts.php'); 
                }
                else{
                session_start();
                $_SESSION['login'] = $login;
                $_SESSION['mdp'] = $mdp;
                $a = authentification($login,$mdp);;
                $_SESSION['id'] = $a;
                 header('Location: space.php'); 

                }

在文件space.php

session_start(); 
$list = Get_companies_by_user($_SESSION['id']);
function.php

中的

function authentification($login,$mdp){

                parametrs();    

                $Log_query=mysql_query(
                    "   
                        SELECT user_id 
                        FROM  reg_users
                        WHERE username ='$login'
                        AND   password   ='$mdp'

                        ") or die(mysql_error());

                        if ($Log_query == true && mysql_num_rows($Log_query) >0) {

                          while ($Res_user = mysql_fetch_array($Log_query) ) {
                        return $Res_user;

                        }
                        }
                        else return 0;

}

问题在于变量$_SESSION['id']的值:为什么在space.php中它为空?我该如何解决这个错误?

1 个答案:

答案 0 :(得分:1)

尝试类似:

function authentification($login,$mdp){
    $Log_query = mysql_query("SELECT user_id FROM  reg_users WHERE username ='$login' AND   password   ='$mdp'") or die(mysql_error());

    //The user should be unique, not more than 0
    if($Log_query && mysql_num_rows($Log_query) === 1){
        while ($Res_user = mysql_fetch_array($Log_query) ) {
            $user = $Res_user['user_id'];
        }
        return $user;
    }else{
        return 0;
    }
}

您还应该使用:

转义$login$mdp
mysql_real_escape_string($var);