Jump to content

Archived

This topic is now archived and is closed to further replies.

scottybwoy

not really understanding this not!

Recommended Posts

Another quicky for the day, what is this not doing in this function :

if (! $this->authorise($user))

does it mean that if the user is not autorised (depending on the function authorise) what happens if there is not yet a value for $user?

Thanks

Share this post


Link to post
Share on other sites
It checks if $this->authorise($user) not returns true (returns false). So if $this->authorise returns true if the user is authorized then it checks if the user is not authorized.

What happens if $user is null, empty, false or something similar depends on what is inside the function.

It's spelled authori[b]z[/b]ed by the way ;)

Share this post


Link to post
Share on other sites
if $user is not populated then essentially the function call will look like
[code]
if (!$this->authorised())
[/code]

you need to look at the authorised method to see what will happen if a value is not passed. if it is nicely written then it will return false.  if the exception is not properly handled it may fail with an error.  alternatively the class may be written so that it is impossible for the $user variable not to be populated with something.

typically a function like this will return true or false but it can return anything.  assuming true/false: yes (to your first question) the antecedent (!) on the method call will mean "take the following action if the user is not authorised". 

Share this post


Link to post
Share on other sites
OK, here is my autorise function :
[code]
<?php
function authorise($user)
    {
echo $user;

if ($user = TRUE)
{
$stmnt = "SELECT USER_ID FROM users WHERE uNAME = $user";
$result = mssql_query($stmnt);
if ($result != TRUE)
{
echo "You are not entered in the Database, please see the Administrator";
exit;

} else {

$success = TRUE;

}

} else {

$success = FALSE;
return $success;

}

    }
?>
[/code]

Is there a better way of writing it? What I want it to do is get the user to report to admin f they are not already in the database and if they are already in the database just go passed the original function to load the page.  Thanks

Share this post


Link to post
Share on other sites
I would write it like this: [code]<?php
function authorise($user)
{
echo $user;

if($user == TRUE)
{
if(!mssql_query("SELECT USER_ID FROM users WHERE uNAME = {$user}"))
{
echo "You are not entered in the Database, please see the Administrator";
exit;
}
else {
$success = TRUE;
}
}
else {
$success = FALSE;
}

return $success;
}
?>[/code]

Share this post


Link to post
Share on other sites
Cheers, never knew you could use ! on the query, was stupid of me to put my return directly under the FALSE statement,  Thanks Daniel0

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.