开发者

Should I use elseif and close the database connection somewhere?

I was wondering if there is a better way to write my code below. Basically two questions, should I use elseif an开发者_StackOverflow社区d do I need to close the connection with the database somewhere?

Thank you

UPDATED CODE

if ($result && mysql_num_rows($result) === 1) {


    $member = mysql_fetch_assoc($result);

    if ($member['disabled']) {
        header("location: not-allowed.php");
        exit();
        }

    if (!$member['verified']) {
        header("location: please-verify.php");
        exit();
        }
}


Do not repeat yourself

if (!($result && mysql_num_rows($result) == 1)) redirect("login-failed.php");
$member = mysql_fetch_assoc($result);
if ($member['disabled']) redirect("not-allowed.php");
if (!$member['verified']) redirect("please-verify.php");
if ( ($member['expires']) && ($member['expires'] <= time()) ) redirect("expired.php");
if ( ($_SESSION['SESS_TOKEN'] == $_POST['token']) ) redirect("member-index.php");

function redirect($location){
    header("location: $location");
    exit();
}

No, if you're using exit or return there is no need for else.

No, it's unnecessary to close mysql connection manually.


if ($result && mysql_num_rows($result) === 1)
{
    ...
}
else die('Wrong username and/or password');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜