Jump to content

Conditional passing , but output is being skipped and returns a false result.


justin7410
Go to solution Solved by mac_gyver,

Recommended Posts

Hey there guys, 

I am trying to create a forgotten password / username file named recover.php

on this page a user will be able to validate his email to see if it exists and if so will send the information to that users registered email in the database. 

simple enough except i have a confusing issue with having multiple header redirects it seems.

MY CODE:

<?php
ob_start();
include('include/init.php');
logged_in_redirect();
include('include/header.php'); 
?>


<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 ! <span style="color:#FF0000">( * All Fields are required )</span></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) {

       echo 'ok'; // <-----  HERE I WANT TO REPLACE WITH A FUNCTION CALLED RECOVER(); && HEADER();

    /*  recover($_GET['mode'], $_POST['forgot_email']);
        header('Location: recover.php?success');          <---- REPLACE "ok" with THIS CODE
        exit();    */


 } else {

          echo '<p>We could not find this email in our database ! Are you sure you got the right email ?</p>';
     }
 } 
?>

        // FORM BEGINS FOR USER TO SUBMIT THEIR EMAIL FOR VALIDATION

<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>


<?  } else {
die('Utter confusion ensues'); //<--- MAIN REDIRECT FROM ISSET CONDITIONAL..

                              REPLACE die('utter confusion ensues'); WITH  header('Location: index.php');
                                                                           exit(); 
}

ISSUE:

 

when i click submit without any of the header()  in  place i get an output of "ok" , which is exactly what i want. 

 

now when i place the extra code as shown above in the // brackets , the conditional fails and i get the die('utter confusion')

 

when i add just the main header redirect to send a user back to index for not having a the correctly defined "$data_mode" ,  the code still works.

 

its when i add the second header(), for when the form is being submitted and redirecting to recover.php?success that it wont function correctly.

 

mind you, i have not even inserted the function of recover(); i was commenting it out, just to get the redirect to the success.

 

anytime i click submit with both header() functions declared i get sent back to the index.php or die('utter confusion ensues');

 

any suggestions as to why this is happening ? 

 

i really cant figure out why the conditional passes if no header is the output , yet when  the header is present to be outputted , the entire conditional fails.

Link to comment
Share on other sites

when recover.php redirects using - header('Location: recover.php?success'); it goes to that same page and the only get parameter that is set will be $_GET['success']. the first if() statement is false, it's testing $_GET['mode'], and the code goes to the else part. your code is doing exactly what it is written to do. what do you want to happen?

Link to comment
Share on other sites

hey mac_gyver,

 

thanks for the reply but,

 

i dont think you understood my problem exactly, or maybe im just not quite getting what your trying to say.

 

 

when recover.php redirects using - header('Location: recover.php?success'); it goes to that same page and the only get parameter that is set will be $_GET['success'].

 

 

 

Yes i am aware of this thats why i am checking to get to that landing page.

 

i was assuming once submitted instead of getting "ok" like i was originally getting , i would now be getting recover.php?success , instead i am being redirected to the homepage, which means my first if is failing.

 

the first if() statement is false, it's testing $_GET['mode'], and the code goes to the else part

 

 

if the first if() statement is false , then why i am returning "ok" when i submit a correct email ? your post did not seem to address this issue. if the conditional is passing with the correct email ( email is found in db ) , then why is it that as soon as i change the output to redirect using header() the entire conditional becomes false ? that doesnt make sense to me.

 

 your code is doing exactly what it is written to do. what do you want to happen?

 

 

 

my code is doing exactly what i want it do as long as there is not 2 header() functions declared , yes.

 

but, as i stated as soon as the second header comes into play to send the user to the recover.php?success  , no it does not do what i want.

 

i guess thats why i am here asking for suggestions . 

 

Link to comment
Share on other sites

  • Solution

your page is being requested two times. it's not going to index.php directly. the first time it is requested $_GET['mode'] is set and it runs the code you expect it to. the code then redirects to that same page with $_GET['success'] set. that causes it to skip the if() logic and goto the else part where you have a redirect to index.php.

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.