2013-05-11 219 views
0

我有一個存儲過程做一個登錄過程,並在不同的條件下返回不同的值在相同的結構。PHP和SQL Server存儲過程執行

這是我的代碼:

create PROCEDURE [dbo].[I_SPCheckUserLogin] 
@username nvarchar(50), 
@password varchar(max) 

AS 
Begin 
    declare @incorrect_pass_msg nvarchar(MAX) = N'کاربر گرامی کلمه عبور نادرست است'; 
    declare @login_success_pass_msg nvarchar(MAX) = N'کاربر گرامی عملیات ورود با موفقیت انجام شد'; 
    declare @user_not_exists_msg nvarchar(MAX) = N'چنین کاربری یافت نشد'; 
    declare @lock_user_msg nvarchar(MAX) = N'کاربر گرامی به دلایل امنیتی شما قادر به ورود نمی باشید'; 
    declare @change_pass_msg nvarchar(MAX) = N'به دلایل امنیتی باید کلمه عبور تغییر یابد'; 

    declare @error_number int = 0; 
    declare @msg nvarchar(100); 
    declare @version nvarchar(50); 
    declare @rule_lock_login_time int; 
    declare @rule_attempt_login int; 
    declare @rule_expire_days int; 
    select @version = ValueI from I_Rules where RuleName = 'Version'; 
    select @rule_lock_login_time = ValueI from I_Rules where RuleName = 'LockLoginTime'; 
    select @rule_attempt_login = ValueI from I_Rules where RuleName = 'AttemptToLogin'; 
    select @rule_expire_days = ValueI from I_Rules where RuleName = 'PasswordExpireDays'; 
    declare @md5_number int; 

    select top 1 @md5_number = Value from SplitString(@version,'.') order by Id desc; 
    declare @m_password nvarchar(MAX); 
    set @m_password = (select dbo.Md5Generator(@password , @md5_number)); 

     declare @uid int , 
      @uname nvarchar(max), 
      @upass nvarchar(max), 
      @ulast_login_date nvarchar(10), 
      @ulast_login_time nvarchar(10), 
      @ulock_login_time nvarchar(10), 
      @umust_change_pass bit, 
      @upass_never_expire bit, 
      @uattempt_login int, 
      @ulast_change_pass_date nvarchar(10); 

     select @uid = Id, 
      @uname = Username, 
      @upass = [Password], 
      @ulast_login_date = LastLoginDate, 
      @ulast_login_time = LastLoginTime, 
      @ulock_login_time = LockLoginTime, 
      @umust_change_pass = UserMustChangePassword, 
      @upass_never_expire = PasswordNeverExpire, 
      @uattempt_login = AttemptToLogin, 
      @ulast_change_pass_date = LastChangePasswordDate 
     from I_Users 
     where Username = @username and 
      IsActive='true'; 


     declare @server_time nvarchar(10), 
      @server_date nvarchar(10); 

     select @server_date = [Date], 
      @server_time = [Time] 
     from dbo.GetShamsiDateTime(); 

     if(@uname is null) 
      begin 
       set @error_number = 1; 
       set @msg = @user_not_exists_msg; 
       select @error_number error , @msg [message]; 
       return; 
      end 
     else 
      begin 
      if(@ulock_login_time is not null) 
      begin 
       if(@server_time <= (select dbo.AddMinuteToTime(@ulock_login_time, @rule_lock_login_time))) 
       begin 
        set @error_number = 1; 
        set @msg = @lock_user_msg; 
        select @error_number error , @msg [message]; 
        update I_Users Set [email protected]_time Where [email protected]; 
        return; 
       end 
       else 
       begin 
        update I_Users 
         set LastLoginDate = @server_date, 
          LastLoginTime = @server_time, 
          LockLoginTime = null, 
          AttemptToLogin = 0 
         where Id = @uid; 
        if(@@ERROR !=0) 
        begin 
         select 3 error , 'update faild' [message]; 
         return; 
        end 
       end 
      end 
      if(@upass != @m_password) 
      begin 
       declare @tmp table(uattemp_login nvarchar(max)); 
       update I_Users 
       set AttemptToLogin = AttemptToLogin + 1 
       output inserted.AttemptToLogin into @tmp 
       where Id = @uid; 
       if(@@ERROR !=0) 
       begin 
        select 3 error , 'update faild' [message]; 
        return; 
       end 

       set @uattempt_login = (select uattemp_login from @tmp); 

       if @uattempt_login = @rule_attempt_login 
       begin 
        update I_Users set LockLoginTime = @server_time where [email protected] 
        if(@@ERROR !=0) 
        begin 
         select 3 error , 'update faild' [message]; 
         return; 
        end 
       end 
       set @error_number = 1; 
       set @msg = @incorrect_pass_msg; 
       select @error_number error , @msg [message]; 
       return; 
      end 
      else 
      begin 
       if @umust_change_pass = 'true' 
       begin 
        set @error_number = 2; 
        set @msg = @change_pass_msg; 
        select @error_number error , @msg [message]; 
        return; 
       end 
       else 
       begin 
        if @upass_never_expire = 'false' 
        begin 
         if @server_date> (select [date] from AddDaysToDate_Custom((select dbo.ShamsitoMiladi(@ulast_change_pass_date)),@rule_expire_days)) 
         begin 
          set @error_number = 2; 
          set @msg = @change_pass_msg; 
          select @error_number error , @msg [message]; 
          return; 
         end 
        end 
        update I_Users 
        set LastLoginDate = @server_date, 
         LastLoginTime = @server_time, 
         LockLoginTime = null, 
         AttemptToLogin = 0 
        where Id = @uid; 
        if(@@ERROR !=0) 
        begin 
         select 3 error , 'update faild' [message]; 
         return; 
        end 
        set @error_number = 0; 
        set @msg = @login_success_pass_msg; 
        select @error_number error , @msg [message]; 

        return; 
       end 
      end 
     end 
end 

的問題是,當執行這樣這個存儲過程:

$username = trim($username); 
$password = trim($password); 
$dbconn = new db_connection(); 


$select_statement = "execute I_SPCheckUserLogin '{$username}','{$password}'"; 
echo $select_statement; 
$result = $dbconn->do_sql_command($select_statement); 

$count = 0; 

$row = $dbconn->fetch_array($result); 


if ($row['error'] == '0') { 
echo '<br/>'; 

    $this->load->library('session'); 
    $ci =& get_instance(); 
    $data = array(); 
    $data['username'] = $username; 
    $data['password'] = $password; 
    $data['system_id'] = $system_id; 
    $ci->session->set_userdata('user_info', $data); 
    redirect('control_panel/index'); 
} else 
    echo $row['message']; 

大量測試後的問題是執行的更新語句時這個存儲過程其餘的存儲過程不是從執行php 而是在sql server管理工作室執行像魅力

對不起我的英語

+0

你在存儲過程中完成整個登錄過程的原因是什麼? – CodeZombie 2013-05-15 13:37:57

+0

這不僅僅是一個登錄過程,我會做更多的事情,但我想要做大部分存儲過程的工作,因爲這個存儲過程將被多次使用,我以這種方式看到它更好 – JUSPER 2013-06-10 10:18:52

回答

0

耽擱OK抱歉,但我找到了解決辦法

我一定要把這是命令:SET NOCOUNT ON

SET NOCOUNT ON加入是爲了避免額外的結果集 干擾用SELECT語句。

它解決了我的問題