会话在页面上检索

时间:2011-09-29 09:45:41

标签: php session

我遇到与登录有关的大问题。实际上有一段时间它工作正常,但有些时候没有工作。 我有 setting.php ,其中session_start()正在执行。此文件包含在所有文件中。另一个文件是config.php,其中定义了所有变量,包括会话名称

$siteConfig_session_name = 'dhoombox';

然后创建了一个类用户,其中有如下函数userlogin: -

function user_front_end_login($_POST,$checkout_page,$check){
    $errorMessage = '';
    $userName = trim($_POST['username']);
    $password = md5(trim($_POST['password']));
$user_login = mysql_query("SELECT user_id, email_id,username, password , firstname, lastname, status FROM tbl_users WHERE  username= '".$userName."' AND password = '".$password."' ");
    $select_user = mysql_fetch_array($user_login);
if( mysql_num_rows($user_login)==1){
    $user_status = $select_user['status'];
    //setAdminError('<span class="errormsg">Information has not been deleted!</span>');
        if( $user_status==1){

                 $this->setUserLoginSession($select_user['user_id'], $select_user['email_id'], $select_user['username'],$select_user['firstname'],$select_user['lastname']);


                    if($checkout_page==1){
                    siteRedirect("billing_info.php");
                    }
                    else{

                     print_r($_SESSION);
                     siteRedirect("index.php");
                    }

            }else{
                setAdminError('<span class="errormsg" style="color:#FF0000;">Sorry your account is not activated.</span>');     siteRedirect("login.php");
            }
    }else{
               setAdminError('<span class="errormsg" style="color:#FF0000;">Username or password mismatch</span>');
                    if($checkout_page==1){
                    siteRedirect("checkout.php");
                    }
                    else{                   
                    siteRedirect("login.php");
                    }       
            }
} 

$ this-&gt; setUserLoginSession($ select_user ['user_id'],$ select_user ['email_id'],$ select_user ['username'],$ select_user ['firstname'],$ select_user ['lastname']) ;如下: -

function setUserLoginSession($uid, $email, $un, $fn,$ln) {
    global $siteConfig_session_name;

     $_SESSION['user_id']   = $uid;
     $_SESSION['email_id']  = $email;
     $_SESSION['username']  = $un;
     $_SESSION['firstname'] = $fn;
     $_SESSION['lastname']  = $ln;
}

有些时候它工作正常,有些时候没有。

1 个答案:

答案 0 :(得分:0)

你有什么问题?你收到错误了吗? 必须在每个要使用会话变量的页面中调用session_start(),不能从include中启动会话。

<?php
session_start();
include'your_include.php';
user_front_end_login(post,checkout_page,check);
?>

<?php
session_start();
function user_front_end_login($_POST,$checkout_page,$check){
$errorMessage = '';
$userName = trim($_POST['username']);
$password = md5(trim($_POST['password']));
$user_login = mysql_query("SELECT user_id, email_id,username, password , firstname, lastname, status FROM tbl_users WHERE  username= '".$userName."' AND password = '".$password."' ");
$select_user = mysql_fetch_array($user_login);
if( mysql_num_rows($user_login)==1){
$user_status = $select_user['status'];
//setAdminError('<span class="errormsg">Information has not been deleted!</span>');
    if( $user_status==1){

             $this->setUserLoginSession($select_user['user_id'], $select_user['email_id'], $select_user['username'],$select_user['firstname'],$select_user['lastname']);


                if($checkout_page==1){
                siteRedirect("billing_info.php");
                }
                else{

                 print_r($_SESSION);
                 siteRedirect("index.php");
                }

        }else{
            setAdminError('<span class="errormsg" style="color:#FF0000;">Sorry your account is not activated.</span>');     siteRedirect("login.php");
        }
}else{
           setAdminError('<span class="errormsg" style="color:#FF0000;">Username or password mismatch</span>');
                if($checkout_page==1){
                siteRedirect("checkout.php");
                }
                else{                   
                siteRedirect("login.php");
                }       
        }
} 
?>