Jump to content

Code not working and no error


fear126

Recommended Posts

Hello i am new to think community but look like here i can find solution for my problems by good programmers all around the globe.

Here is my problem

I make a login function and just trying to use it it not giving any error but it also not working rest all function in that class is working fine. 

here is code and output screen.

[user class login function]

public function find($user = null){
		if($user){
			$field = (is_numeric($user)) ? 'uid' : 'username';
			$data = $this->_Database->get('users',array($field,'=',$user));
			if($data->count()){
				$this->_data = $data->first();
				return true;
			}
		}
		return false;
	}
	
	public function login($username = null,$password = null){
			$user = $this->find($username);
		if($user){
			if($this->data()->password === Hash::make($password)){
				if($this->data()->acc_status == '1'){
					Session::put($this->_sessionName,$this->data()->uid);
					$this->_isLoggedIn = true;
					return true;
				}
				else{
					Session::flash('error','Please check your email you need to active your account first.');
				}
			}
		}
		return false;
	}

Here is login.php code 

 

$user = new User();
if(Input::exists()){
		if(Token::check(Input::get('token'))){
		 
			try{
				$login = $user->login(Input::get('username'),Input::get('password'));
				if(!$login){
					
					Session::flash('login_error','Unable to login.');
				}else{
					Session::flash('logged_in','Logged In Success');
					Redirect::to('home.php');
				}
			}catch(Exception $e){
				die($e->getMessage());
			}
		}
}

Screen shot:-
screen6.jpg

I really want this login code to run!.
Thank you
 

Link to comment
Share on other sites

There are a lot of conditions in your code (i.e. if() statements), but not all of them have an else condition that would produce output. In other words, if some condition is not met you have nothing to let you know that it was not met. So, my guess is that one of those conditions is not being met and the logic you expect to run is not being executed. Add some debugging info so you can verify what is or is not happening.

 

This isn't what you would want in "production" code, but should help find the problem. Although you should have error handling for all the conditions - just not as I added it below.

 

 

$user = new User();
if(Input::exists()){
    echo "DEBUG: Input does exist<br>\n";
    if(Token::check(Input::get('token')))
    {
        echo "DEBUG: Token check passed<br>\n";
        try
        {
            $login = $user->login(Input::get('username'),Input::get('password'));
            if(!$login)
            {
                echo "DEBUG: Login error occured<br>\n";
                Session::flash('login_error','Unable to login.');
            }
            else
            {
                echo "DEBUG: Login error did not occur<br>\n";
                Session::flash('logged_in','Logged In Success');
                Redirect::to('home.php');
            }
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }
    else
    {
        echo "DEBUG: Token check did not pass<br>\n";
    }
}
else
{
    echo "DEBUG: Input does not exist<br>\n";
}
Link to comment
Share on other sites

i tried that code and this is what i got

screen7.jpg

 

Input and token both get passed only idk why data not get in login function or login function have some error please check login function i think there is some error which i cant find

Edited by fear126
Link to comment
Share on other sites

i tried and founded that my find function is not getting user from db but still i don't find any problem with code can you please check my find and login function and correct them if wrong or can you come team viewer to my pc and remotely fix problem i really need it to work.

public function find($user = null){
		if($user){
			$field = (is_numeric($user)) ? 'uid' : 'username';
			$data = $this->_Database->get('users',array($field,'=',$user));
			if($data->count()){
				$this->_data = $data->first();
				return true;
			}
		}
		return false;
	}
	
	public function login($username = null,$password = null){
			$user = $this->find($username);
		if($user){
			if($this->data()->password === Hash::make($password)){
				if($this->data()->acc_status == '1'){
					Session::put($this->_sessionName,$this->data()->uid);
					$this->_isLoggedIn = true;
					return true;
				}
				else{
					Session::flash('error','Please check your email you need to active your account first.');
				}
			}
		}
		return false;
	}
Link to comment
Share on other sites

What debugging code did you create and what were your results? I can't test your code as I don't have everything else that goes along with it. If you had implemented debugging lines like I provided above, you should have narrowed the problem down further. I'm not trying to be difficult, I'm trying to get you to learn.

 

The find method has a flaw in that some 'values' can return false. Just run the query regardless of what is passed (assuming you are properly handling the data to protect against SQL Injection). Also, dynamically selecting which field to search is problematic. Are you preventing users from using a numeric value as their username? If not, the code would fail.

 

 

public function find($user = null)
{
    echo "DEBUG: the value '$user' was passed to find method.<br>\n";
    $field = (is_numeric($user)) ? 'uid' : 'username';
    echo "DEBUG: Search will be performed against the '$field' field.<br>\n";
    $data = $this->_Database->get('users',array($field,'=',$user));
    echo "DEBUG: Search returned " . $data->count() . " results.<br>\n";
    if($data->count())
    {
        $this->_data = $data->first();
        return true;
    }
    return false;
}
Link to comment
Share on other sites

sorry to jump in here, but this example code you found is poorly written. it's only claim to fame is it shows up on the first page of web search results or it's being suggested by php programming courses. using it, assuming you can even get it to work, isn't going to help you create web pages or learn how to program.

 

you should just learn to use the underlying framework and write you own code.

 

in short, if you don't have the programming skills to troubleshoot why this example code doesn't work and it doesn't have code already in it to tell you why it isn't working, you need to contact the author of the code to have him/her troubleshoot and fix the problems in it.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.