如何在php类中使用return关键字

时间:2014-03-11 06:53:34

标签: php

我正在使用此脚本来验证我的登录表单。如果密码不匹配,那么我正在使用错误(即$ war)。如果密码不匹配,即使它显示错误。我只想在我的班级中使用return true和false关键字,但是dnt知道如何使用。请帮助(混淆)。

 function Ucheck($uname,$upass)
    {
     $this->u_name = $uname;    
     $this->u_pass= $upass;  
     $check = "select * from ut  WHERE u_name ='$uname' and u_pass ='$upass'";
     if ($uresult= mysqli_query($this->con,$check))
        {
             $urows= mysqli_num_rows($uresult);
        }
     else
        {
             die (mysqli_error($this->con));   
        }           
     if ($urows !== 1)
        {
        //header('Location:http://yahoo.com/');
        $war="PLEASE enter username and password again.";    
         echo $war; 
        }
     else
       {
           header('Location:http://google.com/');
       }
 }

1 个答案:

答案 0 :(得分:1)

你必须像这样打电话......

$status = Ucheck('Jack','PasswordofJack');
if($status)
{
 header('Location:http://google.com/');
}
else
{
 echo "PLEASE enter username and password again.";
}

像这样重写你的功能......

function Ucheck($uname,$upass)
{
    $this->u_name = $uname;
    $this->u_pass= $upass;
    $check = "select * from ut  WHERE u_name ='$uname' and u_pass ='$upass'";
    if ($uresult= mysqli_query($this->con,$check))
    {
        $urows= mysqli_num_rows($uresult);
    }
    else
    {
        die (mysqli_error($this->con));
    }
    if ($urows !== 1)
    {
       return false; // Added a return keyword
    }
    else
    {
        return true; // Added a return keyword
    }
}