Jump to content


Photo

not really understanding this not!


  • Please log in to reply
5 replies to this topic

#1 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 13 September 2006 - 02:45 PM

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

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 September 2006 - 02:48 PM

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 authorized by the way ;)

#3 jpadie

jpadie
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 September 2006 - 02:50 PM

if $user is not populated then essentially the function call will look like
if (!$this->authorised())

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". 

#4 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 13 September 2006 - 03:03 PM

OK, here is my autorise function :
<?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;

		}

    }
?>

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

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 September 2006 - 03:09 PM

I would write it like this:
<?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;
}
?>


#6 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 13 September 2006 - 03:14 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users