justin7410 Posted April 15, 2013 Share Posted April 15, 2013 (edited) Hello my beautiful freaks, i have an issue trying to create my forgot username code. i am trying to send my end user an email with their loggin info ( more specifically the username ) <? function recover($mode, $email) { $mode = sanitize($mode); $email = sanitize($email); $user_data = user_data(user_id_from_email($email),'username'); if ( $mode == 'username') { email_user_for_recovery($email, 'Your username info', " Hello, This email has been sent from http://j.justin7410.com/recover.php You have received this email because this email address" .$user_data['email'] . ", was used during the registration process of our website. There has been a request for your forgotten username, If you did not request this information plese disregard this email. You do not need to unsubscribe or take any further action. Your sign-in username is: " .$user_data['username'] . // <--- THIS VARIABLE IS NOT BEING INDEXED CORRECTLY "Thank you for using j.justin7410.com, we hope to make your experience as easy and seamless as can be. Cheers, - team j.motionempire.com "); in general the $user_data variable is not returning the values i need. i was wondering if you guys had any good way of debugging this issue ? the variables are passing fine in other instances, but when passed through it wont echo the things i need the email received is just sending everything but the data needed/ thanks guys Edited April 15, 2013 by justin7410 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2013 Share Posted April 15, 2013 Hello my beautiful freaks, Have you seen .josh? The username would appear to depend on these function calls user_data(user_id_from_email($email),'username') Quote Link to comment Share on other sites More sharing options...
justin7410 Posted April 15, 2013 Author Share Posted April 15, 2013 (edited) sorry my OP was not specific enough in the issue, i have 4 functions here function user_data($user_id){ $data = array(); $user_id = (int)$user_id; $func_num_args = func_num_args(); $func_get_args = func_get_args(); if ($func_num_args > 1) { unset($func_get_args[0]); $fields = '`' . implode('`, `', $func_get_args) . '`' ; $query = ("SELECT $fields FROM `users` WHERE `user_id` = $user_id"); $data = mysql_fetch_assoc(mysql_query($query)); return $data; } } function recover($mode, $email) { $mode = sanitize($mode); $email = sanitize($email); $user_data = user_data(user_id_from_email($email),'username'); if ( $mode == 'username') { email_user_for_recovery($email, 'Your username info', " Hello, This email has been sent from http://j.me.com/recover.php You have received this email because this email address" .$user_data["email"] . ", was used during the registration process of our website. There has been a request for your forgotten username, If you did not request this information plese disregard this email. You do not need to unsubscribe or take any further action. Your sign-in username is: " .$user_data["username"] . "Thank you for using j.m.com, we hope to make your experience as easy and seamless as can be. Cheers, - team j.m.com "); } else if ( $mode == 'password' ) { // recover password } } function user_id_from_email($email){ $email = sanitize($email); $query = (" SELECT `user_id` FROM `users` WHERE `email`= '$email' "); $results = mysql_query($query); return (mysql_result($results, 0 , 'user_id')); } function email_user_for_recovery($to, $subject, $body){mail($to, $subject, $body, 'From: recovery@j.m.com');} and within the function of recover() when sending the email back to the user (which works fine and i receive) the most important aspect of that email is missing. the $user_data variable is not being stored properly since when i input it in the email to be sent, the outputted email does not have it and instead the end user gets. Hello,This email has been sent from http://j.m.com/recover.php You have received this email because this email address, was used during the registration process of our website. There has been a request for your forgotten username, If you did not request this information plese disregard this email. You do not need to unsubscribe or take any further action. Your sign-in username is: Thank you for using j.m.com, we hope to make your experience as easy and seamless as can be.Cheers, - team j.m.com so as you can see .. my variable of $user_data is not outputting correctly .. thanks guys, all help is appreciated Edited April 15, 2013 by justin7410 Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 15, 2013 Share Posted April 15, 2013 It's difficult to see why. But I can see that you are not checking your database interaction. Statements like $data = mysql_fetch_assoc(mysql_query($query)); are not considered best practices. $res = mysql_query($query); if ($res === false) { // The query failed. In production do something intelligent // in development: die(sprintf('%s: %s<BR>%s<BR>', __FUNCTION__, $query, mysql_error())); } $data = mysql_fetch_assoc($res); if ($data === false) { die(sprintf('%s: mysql_fetch_assoc() failed', __FUNCTION__)); } return $data; Yes, it is more code. But it is much easier to read, (especially when you come back to change it later) and now the code will tell you what problem it might be having. Also, I don't know what your "sanitze()" function does, but you are calling it multiple times on the same data. If that function includes mysql_real_escape_string(), then this could cause problems. You should only escape the data JUST BEFORE putting it in the query -- and you "escape" it differently JUST BEFORE putting on a web page. Quote Link to comment Share on other sites More sharing options...
justin7410 Posted April 15, 2013 Author Share Posted April 15, 2013 Hey David , sadly i had my sql error conditionals set for most of my functions , i felt that the code was looking so sloppy that after i knew the codes were all firing and returning correct. i just deleted those conditionals just to make my functions page look a lot more compact. looking back this was probably a rookie mistake that might come back to haunt me, but we shall see. the query is returning properly that was the last thing i thought was gonna be the issue. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 15, 2013 Share Posted April 15, 2013 try using printf('<PRE>%s</PRE>',htmlspecialchars(print_r($user_data,true))); right after the call to user_data(). This should show you the content of that array, so you can see if the data is indeed there. I don't see the email address in that copy of the email you posted either (unless you removed it for the post). Of course, the email column name is not in the call to user_data(), so that column is not being SELECTed or returned. Turn on error reporting so you can see any messages: error_reporting(E_ALL); ini_set('display_errors', true); Quote Link to comment Share on other sites More sharing options...
justin7410 Posted April 15, 2013 Author Share Posted April 15, 2013 try using printf('<PRE>%s</PRE>',htmlspecialchars(print_r($user_data,true))); right after the call to user_data(). This should show you the content of that array, so you can see if the data is indeed there. I don't see the email address in that copy of the email you posted either (unless you removed it for the post). Of course, the email column name is not in the call to user_data(), so that column is not being SELECTed or returned. Turn on error reporting so you can see any messages: error_reporting(E_ALL); ini_set('display_errors', true); Hey David, thanks for the great feedback as always. i tried doing as you suggested : <? function recover($mode, $email) { $mode = sanitize($mode); $email = sanitize($email); $user_data = user_data(user_id_from_email($email),'username'); printf('<PRE>%s</PRE>',htmlspecialchars(print_r($user_data,true))); // <-- HERE I PLACED THE EXTRA CODE SNIPPET . if ( $mode == 'username') { email_user_for_recovery($email, 'Your username info', " Hello, This email has been sent from http://j.motionempire.com/recover.php You have received this email because this email address" .$user_data['email'] . ", was used during the registration process of our website. There has been a request for your forgotten username, If you did not request this information plese disregard this email. You do not need to unsubscribe or take any further action. Your sign-in username is: " .$user_data['username'] . "Thank you for using j.m.com, we hope to make your experience as easy and seamless as can be. Cheers, - team j.m.com "); } else if ( $mode == 'password' ) { // recover password } } Do i need to die then add the code snippet or an exit() ? i simply do not see the output, unless of course i am just not doing what you meant.. when i submit the code nothing seems to have changed , in terms of error output or message output . and about the failed index on 'email' .. yes i had tried to remove all other values, since i was following a tutorial. I figured maybe adding my own data or fields might of thrown a monkey wrench in the code, and simply was to lazy to edit that part out. but thanks for the heads up regardless any other suggestions or input would be much appreciated guys Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 15, 2013 Share Posted April 15, 2013 That statement should have output something like: Array ( [username] => DavidAM ) It should show up in the browser if this is a standard webpage. Try the "View Source" feature of your browser to see if it is there. If this is AJAX or a CRON job, then it may be elsewhere. At the very least, you should have gotten: Array ( ). If that did not happen, then you never got to that line of code. Quote Link to comment Share on other sites More sharing options...
justin7410 Posted April 15, 2013 Author Share Posted April 15, 2013 No sir, but maybe it is due to me failing to mention that my functions are all directed to a page called recover.php the function recover() is then used if the user has passed a bunch of conditionals : <?php ob_start(); include('include/init.php'); logged_in_redirect(); include('include/header.php'); if (isset($_GET['success']) === true && empty($_GET['success']) === true) { echo '<h1> RECOVERY <span style="color:green;">COMPLETE</span>, AND AN EMAIL HAS BEEN SENT WITH YOUR INFO</h1>'; } else { ?> <div style="height: 500px; width: 700px; padding: 50px 50px; margin: auto;"> <div class="back-btn"> <h3><a href= "take_me_back" onclick="history.go(-1); return false;">← Back</a></h3><br><br> </div> <h1><span>Forgot your username / password ? </span></h1> <p>In just a couple easy steps, we can recover your info and get you back on your way in no time !</p> <? $mode_allowed = array('username','password'); if(isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) === true) { if(isset($_POST['forgot_email']) === true && empty($_POST['forgot_email']) === false ){ if(email_exists($_POST['forgot_email']) === true) { recover($_GET['mode'], $_POST['forgot_email']); header('Location: recover.php?success'); exit(); } else { echo '<p><span style="color: red; font-weight: bolder; font-size: 12px;">We could not find this email in our database ! Are you sure you have the right email ?</span></p>'; } } ?> <div class="recover_user_info"> <div id="change_password" style="float:left;"> <table style="margin-left:10px; margin-top:10px; margin-bottom:10px;"> <form name="forgot_data" action="" method="post"> <tr> <td class="register_td_left"><span class="">Please enter your email here:</span></td> <td class="register_td_right" colspan="2"><input type="email" name="forgot_email" size="60" maxlength="60" value=""></td> </tr> <tr> <td class="register_td_left"></td> <td class="extra_data" colspan="2"></td> </tr> <tr><td class="register_td_left"></td><td class="extra_data2" colspan="2"><input class="register-button" type="submit" name="forgot_userdata_button" value="Recover my info"></td></tr> </tbody></table></form> </div> </div> </div> <? } else { header('Location: index.php'); exit(); } } im assuming this call for the output with printf is not outputing since a user is being redirected to recover.php?success with a success message as you see above. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 15, 2013 Share Posted April 15, 2013 Ahh, yeah that would do it. While you are debugging this problem, you might put a die() just before the header -- or comment out the header() since you have an exit() right after it. Then you can see any messages that are output during the POST. Quote Link to comment Share on other sites More sharing options...
justin7410 Posted April 15, 2013 Author Share Posted April 15, 2013 yea that makes alot more sense, no it gave me an error that my email function was not matching the row being indexed i realized what my problem was and continues to be with anything relating my $email variable i have a space in the database field, when data is being inputted, its not being trimmed and leaving a space. i had to just add a space to in front of the variable and the $user_data array is print_r properly . i tried to add the trim() function in front of the variable i thought was sending the data into the field, but the problem still persists anyway, thanks again man i really appreciate your helpful posts B) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 15, 2013 Share Posted April 15, 2013 (edited) the actual query statement that is inserting your email value probably has a space in it before the variable name. i was going to mention the output buffering in your previous thread as it cause nothing but problems. the only time you should use output buffering is if you want to buffer output. by using it and outputting php error messages or the messages your code produces, then unconditionally redirecting, you won't see any of that output and in the case of one of the messages your code produces, you should not be redirecting in those cases anyway. you should not use ob_start and you should organize the logic on your page so that all the php code that decides what to do on the page (the business logic) comes first before you try to output any of the html on the page. Edited April 15, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.