How to debug using an exception and put into file_put_contents

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 968 Views - Last Post: 27 September 2019 - 11:54 AM Rate Topic: -----

#1 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 11:56 AM

I have a login framework, which I have updated using the User class, update function. The update I made is that I send a timestamp to the user db table to store the last login timestamp. Since making this change, I have been having some login issues.

The original login issue was that I essentially had to type in my user credentials twice to be able to login (redirect to login part of site). After making some changes to the function, in an attempt to debug, the login only takes one attempt, but the query update no longer works.

My latest change was putting a try/catch into the update function. Changing this has stopped the updated timestamp to the users table. The file_put_contents file is not creating nor any other error file.

For the login code, this the line of code that updates the database: $loginTime = $user->update(array('last_login' => $dateNow));

Any ideas?

User Class, Update function:

public function update($fields = array(), $id = null) {
		try {
			$date = new DateTime();
			
			if(!$id && $this->isLoggedIn()) {
				$id = $this->data()->id;
				//$id = $this->_data->id;
			}
		}
		catch(Exception $e) {
			if(!$this->_db->update('users', $id, $fields)) {
				//file_put_contents('debug_log', "\n[{$date->format('Y-m-d H:i:s')}] " . print_r($data, 1));
				throw new Exception('There was a problem updating!');
			}
			file_put_contents('debug_user_update', $date->format('Y-m-d H:i:s'). print_r($e->getMessage()) );
			echo 'Message: ' .$e->getMessage();
		}
	}



Here is the login code:

if(Input::exists()) {
	if(Token::check(Input::get('token'))) {

		$validate = new Validate();
		$validation = $validate->check($_POST, array( 
			'username' => array('required' => true), 
			'password' => array('required' => true) 
		));
		if($validation->passed()) {
			$user = new User();
			$cookieName = Config::get('remember/cookie_name');
			/*$login = true;
			if(Cookie::exists($cookieName)) {
				if( $user->data()->password === Cookie::get($cookieName)){
					echo $logged = '<span class="signinpanel">' . "You've been automatically logged in." . '</span>';
				}
				else {
					echo $logged = '<span class="signinpanel">' . "You could not be automatically logged in." . '</span>';
					//Cookie::delete($cookieName);
				}
			}
			else { */
			$dateNow = date("Y-m-d h:i:sa");
			$remember = (Input::get('remember') === 'on') ? true : false;
			$login = $user->login(Input::get('username'), Input::get('password'), $remember);
			$loginTime = $user->update(array('last_login' => $dateNow));
			}
			if($login) {
				Redirect::to('/admin/index');
				$loginTime;
			} else {
				$tryagain = '<span class="signinpanel">' . "The information you entered did not match our records.<br>Please try again." . '</span>';  
			}
	} else {
		foreach($validation->errors() as $error) {
				echo $error, '<br>';
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to debug using an exception and put into file_put_contents

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 12:39 PM

Quote

My latest change was putting a try/catch into the update function. Changing this has stopped the updated timestamp to the users table. The file_put_contents file is not creating nor any other error file.

This is because all of your code that does any work only runs if the first part of the code throws an exception.
Was This Post Helpful? 1
  • +
  • -

#3 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 12:47 PM

Gotcha. I put the other if statement into the try part. The file is created, but the only contents within it is the date. Nothing populated for this:

print_r($e->getMessage()
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 01:43 PM

And what is $e, the exception that you didn't catch? Why are you trying to catch an exception at all, do you expect there to be one? Why did you add the try/catch block, will isLoggedIn or data ever throw an exception? If not, the try/catch is pointless.
Was This Post Helpful? 1
  • +
  • -

#5 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 01:50 PM

Maybe it is pointless then. I am just trying to learn how to debug the code so I can figure out the issue. The isLoggedIn doesn't relate to the issue. It was made for a cookie, automatic login that I could never get to work.

I don't know how else to debug the exception being thrown to see what is the issue.

Since changing the code the original issue is present where I have to login twice to get the redirect to work. I'm not sure if this will help. It is the login function.

public function login($username = null, $password = null, $remember = false) {
        $date = new DateTime();
	    //file_put_contents('debug_log_U', "\n[{$date->format('Y-m-d H:i:s')}] Sent pass: " . $password);
		if(!$username && !$password && $this->exists()) {
			Session::put($this->_sessionName, $this->data()->id);
		} else {
			$user = $this->find($username);

            //file_put_contents('debug_log_U', "\n[{$date->format('Y-m-d H:i:s')}] Stored pass:" . print_r($this->_data->password, 1) . "\nSent pass: " . $password);
			if($user) {
				if(password_verify($password, $this->_data->password)) {
				//if(Auth::check($this->data()->password, $password)){
					Session::put($this->_sessionName, $this->_data->id);

					if($remember) {
						$hash = Hash::unique();
						$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
						
						if(!$hashCheck->count()) {
							$this->_db->insert('users_session', array(
								'user_id' => $this->_data->id,
								'hash' => $hash
							));
						} else {
							$hash = $hashCheck->first()->hash;
						}
						Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
					}
					return true;
				}
			}
		}
		return false;
	}

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 25 September 2019 - 03:18 PM

Quote

I don't know how else to debug the exception being thrown to see what is the issue.

Well, start by showing the actual error message from the exception.

What specifically happens when you run this? If it doesn't redirect, what exactly does it do?
Was This Post Helpful? 1
  • +
  • -

#7 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 06:44 AM

This is what it reporting on the page.

Quote

There was a problem updating!Message: There was a problem updating!
Warning: Cannot modify header information - headers already sent by (output started at public_html/classes/User.php:44) in /public_html/classes/Redirect.php on line 14


On the first login attempt, whether following a log out or just going to the site to login, after entering the credentials and clicking login the error message above shows. Then once you enter the information in again and click login you are redirected.


This is the redirect class:
(Line 14 is header('Location:' . $location);)

class Redirect {
	public static function to($location = null) {
		if($location) {
			if(is_numeric($location)) {
				switch($location) {
					case 404;
						header('HTTP/1.0 404 Not Found');
						include 'includes/errors/404.php';
						exit();
					break;
				}
			}
			header('Location:' . $location);
			exit();
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2943
  • View blog
  • Posts: 11,435
  • Joined: 03-December 12

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 09:35 AM

do your files look like this?

<?php

class something {

}


?>


Remove the closing php tag.
Was This Post Helpful? 1
  • +
  • -

#9 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 09:47 AM

Quote

On the first login attempt, whether following a log out or just going to the site to login, after entering the credentials and clicking login the error message above shows.

That's your own error message, so where in the code are you deciding to show that, and why? Incidentally, outputting that error message is the reason why the header redirect fails. It's trying to redirect, but can't because the headers have already been sent because you printed that error message.
Was This Post Helpful? 1
  • +
  • -

#10 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 10:04 AM

View Postastonecipher, on 26 September 2019 - 09:35 AM, said:

do your files look like this?

<?php

class something {

}


?>


Remove the closing php tag.


It was like that in the redirect class file. I removed it.

View PostArtificialSoldier, on 26 September 2019 - 09:47 AM, said:

Quote

On the first login attempt, whether following a log out or just going to the site to login, after entering the credentials and clicking login the error message above shows.

That's your own error message, so where in the code are you deciding to show that, and why? Incidentally, outputting that error message is the reason why the header redirect fails. It's trying to redirect, but can't because the headers have already been sent because you printed that error message.


I just took out the try/catch and removed the throw new exception. I replaced it with a simple echo. Now the error seems to be pointing at that. When removing the echo statement, the redirect works every time.

However, now the timestamp update query is very hit and miss. It worked the first time, but since then I have tried logging in and out multiple times and it hasn't updated.

public function update($fields = array(), $id = null) {
			$date = new DateTime();
			
			if(!$id && $this->isLoggedIn()) {
				$id = $this->data()->id;
				//$id = $this->_data->id;
			}
			if(!$this->_db->update('users', $id, $fields)) {
				//file_put_contents('debug_log', "\n[{$date->format('Y-m-d H:i:s')}] " . print_r($data, 1));
				//throw new Exception('There was a problem updating!');
				echo "There was a problem updating!";
				file_put_contents('debug_user_update', $date->format('Y-m-d H:i:s'). print_r("There was a problem updating") );
			}
			//file_put_contents('debug_user_update', $date->format('Y-m-d H:i:s'). print_r($e->getMessage()) );
			//echo 'Message: ' .$e->getMessage();
	}

Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 10:58 AM

Print out the variables that you're sending to that update method to see what you're telling it to do.
Was This Post Helpful? 1
  • +
  • -

#12 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 12:29 PM

Printing anything broke the code. Instead I tried sending the $login variable to a file. Nothing is printed for this variable though.

Then in my general error log file, I get this:


Quote

[26-Sep-2019 19:01:59 UTC] PHP Fatal error: Uncaught Error: Call to a member function format() on null in public_html/login.php:38
Stack trace:
#0 {main}
thrown in public_html/login.php on line 38
[26-Sep-2019 19:02:32 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at public_html/login.php:39) in public_html/classes/Redirect.php on line 14


$date = new DateTime();
			$dateNow = date("Y-m-d h:i:sa");
			$remember = (Input::get('remember') === 'on') ? true : false;
			$login = $user->login(Input::get('username'), Input::get('password'), $remember);
			//var_dump($login);
			//print $dateNow;
			$loginTime = $user->update(array('last_login' => $dateNow));
			}
			if($login) {
				$loginTime;
				file_put_contents('debug_login', $date->format('Y-m-d H:i:s'). print_r($login) );
				Redirect::to('/admin/index');
			} else {
				$tryagain = '<span class="signinpanel">' . "The information you entered did not match our records.<br>Please try again." . '</span>';  
			}


(Line 38 is this: $loginTime; )

So based on the error's location and what is says, can I assume that the $loginTime variable is not being produced? My attempt to echo $loginTime did not result in anything populating for it.
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2943
  • View blog
  • Posts: 11,435
  • Joined: 03-December 12

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 12:31 PM

Is there a try catch in the update method that could possibly shed light on if it is failing?
Was This Post Helpful? 1
  • +
  • -

#14 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 591
  • Joined: 30-April 15

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 12:36 PM

I had one in there before, but nothing was populating in the file_put_contents output, other than the date.

I just added it back in; still nothing is populating for $e. Also, the file is not being created.

public function update($fields = array(), $id = null) {
		try {
			$date = new DateTime();
			
			if(!$id && $this->isLoggedIn()) {
				$id = $this->data()->id;
				//$id = $this->_data->id;
			}
			if(!$this->_db->update('users', $id, $fields)) {
				//file_put_contents('debug_log', "\n[{$date->format('Y-m-d H:i:s')}] " . print_r($data, 1));
				//throw new Exception('There was a problem updating!');
				//echo "There was a problem updating!";
				//file_put_contents('debug_user_update', $date->format('Y-m-d H:i:s'). print_r("There was a problem updating") );
			}
		}
		catch (Exception $e) {
			file_put_contents('debug_user_update', $date->format('Y-m-d H:i:s'). print_r($e->getMessage()) );
			//echo 'Message: ' .$e->getMessage();
		}
	}

This post has been edited by pfar54: 26 September 2019 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2399
  • View blog
  • Posts: 7,316
  • Joined: 15-January 14

Re: How to debug using an exception and put into file_put_contents

Posted 26 September 2019 - 12:44 PM

Quote

Instead I tried sending the $login variable to a file.

OK, I don't see a login variable. You're sending $id and $fields to update, what's $login? Why aren't you printing the values of $id and $fields? You can use print_r to print an array if that's an array, which I assume it is. You can pass true as the second value to print_r to return the output instead of sending it to the browser, but it doesn't matter if the redirect works. If updating the last login is the problem, then figure out exactly what you are telling it to update in the database. Print the $id, and print $fields. If you want to go into the update method and print the actual SQL query, do that. You need to figure out what you are telling PHP and MySQL to do.

Quote

So based on the error's location and what is says, can I assume that the $loginTime variable is not being produced?

No you cannot, since writing just a variable name on a line doesn't do anything at all, since that line is not executable, and since the following line uses a call to a method called format at $date->format('Y-m-d H:i:s'), and since the error message says that you're trying to call a method on null, then it's probably fair to assume that the error is happening on the line after the one you think it is, and that $date is null, no? You can test that if you like, use is_null to return true if a variable is null and test that if you want to test it. Since you have a closing bracket on line 8 in the code you posted, then it sounds to me like you defined $date inside an if statement which did not run so that when you tried to use $date->format later on, $date was null.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2