Jump to content


Custom Function Problem


2 replies to this topic

#1 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 22 August 2007 - 05:04 PM

Hey,

I didn't want to come here asking for help, I wanted to try for myself to sort it, but yeah... I've tried everything I can think of and am getting nowhere.

//--------------------
// Login Function
//--------------------
function login($username, $password, $remember) {
    // Connect to the database
    global $mysql;
    // First we need to check that the user info submitted is correct!
    // Errors will be sotred in an array, which will be stored in a variable
    $errors = array();
    // Query used throughout the function
    $query = mysql_query("SELECT * FROM members WHERE username = '".$username."'") or die(mysql_error());
    // Fetch info from query
    $user = mysql_fetch_object($query);
    
    // Username checking
    if(empty($username)) {
        $errors[] = "Username field left empty.";
    }
    else {
    // Check if username exists
    if(mysql_num_rows($query) == 0) {
        $errors[] = "Username does not exist.";
    }
    }
    // Password checking
    if(empty($password)) {
        $errors[] = "Password field left empty.";
    }
    else {
    // Check if passwords match
    if($password != $user->password) {
        $errors[] = "Password is incorrect.";
    }
    }

    // Check if user wants to remember their login
    if($remember) {
        setcookie('user_id', $user->id, time()+60*60*24*365, '/'); // Cookie set for 1 year
    }
    if($errors) {
      return false;
    }
    else {
      return true;
    }
}

That' my login function that I wrote... it does the job, so I'm satisfied with it. Now as you can see, the errors are put into an array. Also to point out, I am using Smarty for my site, thought I'd give it a try, so far so good, though it appears that when I use $smarty->assign('blah', 'r0r'); I get an error: "Call to a member function assign() on a non-object" though that's no my issue.

I want to be able to grab the $errors variable with the errors in and put it in a Smarty assign function, but I can't seem to get a hold of the damn thing. The closest I came was:

if($errors) {
		 print_r($errors);
	   }
	   else {
		 return true;
 }

So my question really, is how can I get the $errors variable to work outside of the function?

Sorry if I'm not making sense, when it comes to TALKING about code, I'm not so bright.

Thanks for looking.

- Chris.

Edited by .CJ, 22 August 2007 - 05:05 PM.


#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 22 August 2007 - 05:20 PM

Make it a global variable. Also would be how you solve your call to an undefined object problem, assuming you were trying to access $smarty from the scope of the function itself.

$errors = array();
function login($username, $password, $remeber){
  global $errors, $mysql, $smarty;
  // Other code and crap...
}

var_dump($errors);

I would personally have it actually return the array, so you could then store the call in a variable, check if the variable was exactly equivalent to the boolean true (in which case echo your success or whatever), and otherwise print out the variable that would now contain the returned errors.

#3 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 22 August 2007 - 05:40 PM

Cheers DS, that's helped loads.

Now to work out the smarty side to it... smarty, something I'm not with this (ATM) :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users