Jump to content


Photo

Is there a way to "step" through code for debugging *solved*


  • Please log in to reply
8 replies to this topic

#1 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 18 October 2006 - 11:01 PM

I am working on revising some code from a book I am studying on PHP5 and MySQL  - one piece isn't working - the part that lets a user reset their password.

There are about four or five function calls involved so I have no idea at the moment where it is breaking down.  Is there a way to put it in a debug mode so I can step through line by line to see where it is messed up?
???

#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 18 October 2006 - 11:18 PM

It really depends on the layout of your code.  Try posting the code and can try and figure out what the best approach is
If you're not part of the solution, you're part of the precipitate

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 October 2006 - 11:24 PM

You best bet is to post the code but in saying that here a thort to consider.

If a password is md5 encripted the the password can never be unencripted.

the solution to the problam when a user lose there password is to get the user to enater there email address in a box then cheek the email addres aginst the database then update the users password with a random number then post that information to the user to there email address.

when it comes to debuging if you got error all set in the php.ini then all bad bugs should show on the line that is wrong.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 18 October 2006 - 11:30 PM

I think your jumping the gun a little here Redarrow.  Conjurer has not mentioned anything about decrypting the password, and there are a number of places where the problem could lie, from generation of a new random password, or encrypting and entering a user defined password in the database, or any number of other things.

I think the best thing to do here is to post the code and take a look
If you're not part of the solution, you're part of the precipitate

#5 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 18 October 2006 - 11:35 PM

Well it is supposed to work like Redarrow said, they enter the user name, it creates a new password in SHA1 format and mails to their email. But it isn't creating the password.

I will try to piece together the functions and then post it here. Will take a bit.

Thanks!


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 October 2006 - 11:46 PM

Is there a way to put it in a debug mode so I can step through line by line to see where it is messed up?


If you get an IDE like Nusphere PHPEd with an integrated debugger, yes
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 19 October 2006 - 05:54 PM

I pulled together the pieces that are trying to run but not working.  See Code below. 

Problem is I have no way to tell where it is bombing.  When I click the link for forgot password I get the forgot password form, fill in the username for the test user and then I get two lines back:

This is the username you sent: Houdini
Your password could not be reset - please try again later.

And then a  link back to the login form.

It seems to be jumping to the following piece of code:

  catch (Exception $e)
  {
    echo 'Your password could not be reset - please try again later.';
  }



How can I add some error checking things in the reset_password() function to help me make sure this part is working and see what is happening?

Thanks!


<?php
  require_once("bookmark_fns.php5");

  do_html_header("Resetting password");		//called from output_fns

  // creating short variable name
  $username = $_POST['username'];
echo 'This is the username you sent: ' .$username .'<br>';		// added by me for debugging

  try
  {
    $password = reset_password($username);	//called from user_auth_fns.php5 -see below
    notify_password($username, $password);	//called from user_auth_fns.php5 -see below

    echo 'Your new password has been emailed to you.<br />';
  }
  catch (Exception $e)
  {
    echo 'Your password could not be reset - please try again later.';
  }
  do_html_url('login.php5', 'Login');	//called from output_fns - jsut wraps Url in in href tag
  
  do_html_footer();			//called from output_fns - just closes body and html tag.


?>


Here are the relevent called functions:

// bookmark_fns.php5
  require_once('data_valid_fns.php5'); 
  require_once('db_fns.php5');
  require_once('user_auth_fns.php5');
  require_once('output_fns.php5');
  require_once('url_fns.php5');

// functions from user_auth_fns.php5

function reset_password($username)
// set password for username to a random value
// return the new password or false on failure
{ 
  // get a random dictionary word b/w 6 and 13 chars in length
  $new_password = get_random_word(6, 13);
  
  if($new_password==false)
    throw new Exception('Could not generate new password.');
  // add a number  between 0 and 999 to it
  // to make it a slightly better password
  srand ((double) microtime() * 1000000);
  $rand_number = rand(0, 999); 
  $new_password .= $rand_number;
 
  // set user's password to this in database or return false
  $conn = db_connect();
  $result = $conn->query( "update user
                          set passwd = sha1('$new_password')
                          where username = '$username'");
  if (!$result)
    throw new Exception('Could not change password.');  // not changed
  else
    return $new_password;  // changed successfully  
}

function notify_password($username, $password)
// notify the user that their password has been changed
{
    $conn = db_connect();
    $result = $conn->query("select email from user
                            where username='$username'");
    if (!$result)
    {
      throw new Exception('Could not find email address.');  
    }
    else if ($result->num_rows==0)
    {
      throw new Exception('Could not find email address.');   // username not in db
    }
    else
    {
      $row = $result->fetch_object();
      $email = $row->email;
      $from = "From: support@phpbookmark \r\n";
      $mesg = "Your PHPBookmark password has been changed to $password \r\n"
              ."Please change it next time you log in. \r\n";
      
      
      if (mail($email, 'PHPBookmark login information', $mesg, $from))
        return true;      
      else
        throw new Exception('Could not send email.');
    }
} 



#8 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 19 October 2006 - 06:17 PM

I echoed out the $e error variable and get the following:

'Exception' with message 'Could not generate new password.' in /home/consult/public_html/development/Testing/26/user_auth_fns.php5:124 Stack trace: #0 /home/consult/public_html/development/Testing/26/forgot_passwd.php5(10): reset_password('Houdini') #1 {main}


Does that mean line 124 of user_auth_fns is where the error happened?  If so that would be where it tried to get a random dictionary word b/w 6 and 13 chars in length for the new password.

Is this dictionary function maybe not loaded? If so, how would I find out, how would I load, or what is another approach here to generate a random password?

So much to learn, so little time!  ::)


function reset_password($username)
116	function reset_password($username)
117	// set password for username to a random value
118	// return the new password or false on failure
119	{ 
120	  // get a random dictionary word b/w 6 and 13 chars in length
121	  $new_password = get_random_word(6, 13);
122	  
123	  if($new_password==false)
124	    throw new Exception('Could not generate new password.');  // <<<<<<<<<<<<Error Line!??
125	  // add a number  between 0 and 999 to it
126	  // to make it a slightly better password
127	  srand ((double) microtime() * 1000000);
128	  $rand_number = rand(0, 999); 
129	  $new_password .= $rand_number;
 


#9 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 19 October 2006 - 08:25 PM

Bingo!

Turns out get_random_word is a defined function in another part of the includes and it is looking for a dictionary to pull the word from.

Now I just need to find the dictionary.  But I would mark this resolved, if I knew how.  ::)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users