logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: If else not working properly in php
Author
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Wed 19 Aug '09 9:29    Post subject: If else not working properly in php Reply with quote

Hi all i am trying to workout in If-else statement. My problem is if the username and password is correct then the header is send properly and if anyone of these is not correct then the header is not send or even the comment is not echoed. It give just white blank page. Wat can be the problem i need urgent help.

i am giving my code
Code:
 

if ($username == $name and $password=$pass)
 {
     $_session['uname']=$username;
      header('Location:admin.php');
}
else
{

  echo "Either password or Username is not correct";
  header('Location:login.php');

}



The else part is not working at all. Even if i removed header the echo statement is not displayed

i dont know wat is the problem, i need this urgent help as i am working on a project rt now, if any one can reply me back instantly i will be really helpful. Thanks in advance.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Wed 19 Aug '09 9:33    Post subject: Reply with quote

You can't echo something and than making a redirect. Only one of it will work. Sending a header is only possible when nothing (included white space) has beens send.
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Wed 19 Aug '09 11:34    Post subject: If else not working Reply with quote

Thanks james for ur reply. I have tried that thing also. But still its not working properly. But when the usename and pasword are correct the header is send properly. Its causing problem only when any one of them is incorrect, why is that, as there is not chance of sending any white spaces , so i am confused where i wil get that white spaces.
Thanks in advance for ur reply.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Wed 19 Aug '09 18:27    Post subject: Reply with quote

if ($username == $name and $password=$pass)

Are you not comparing one value while assigning another a value instead of comparing which would mean it could never be true?

$password==$pass
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Wed 19 Aug '09 19:42    Post subject: Reply with quote

I've overseen that.

Code:

if ($username == $name and $password=$pass)

So if $username is $name the $password will be igonred cause it is always true.

true_test.php
Code:

<?php
$pass='test';
if($password=$pass){
   echo 'true';
}
else
{
   echo 'false';
}
?>


This echos always true.


white space means when you hit space on your keyboard.

Code:

<?php
header();
//this works


Code:

 <?php
header();
//this does not work because of the whitespace (space key)
// the white space is an output before the header


Code:

<?php
echo 'something';
header();
//this does not work there is an output before the header
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 24 Aug '09 9:28    Post subject: if else problem Reply with quote

Thanks for reply both james and glsmith, sorry glsmith its my mistake i am comparing both the values i made mistake while typing here. I am really sorry for that matter, actually its showing just blank white page is i put wrong password or user name. actually i m directing header to same index.php for relogin but its going the code page login.php. I have no idea whats going on with this code, i am giving the code below.
Code:


<?php
      
      session_start();
      
      include("connectdb.php");
      
      $username = strtoupper($_POST['username']);
      
      $password = md5($_POST['password']);
      
      $sql = "select * from login where loginname = '".$username."' and password = '".$password."'";
      
      $sqlres = mysqli_query($link,$sql);
      
      while($row = mysqli_fetch_array($sqlres))
        {      
         $emname = $row['empname'];   
         if ($username=='MW12' and $password == $password)
         {
            
            $_SESSION['uname'] = $username;
            $_SESSION['pass'] = $pass;
            $_SESSION['empid']=$eid;
            $_SESSION['ename']=$emname;
            header('Location:pmpage.php');
         
         }
         
         else
         
         {   
            if($username=='MW10' and $password == $password)
               {
                  
                  $_SESSION['ename']=$emname;
                  $_SESSION['uname'] = $username;
                  $_SESSION['pass'] = $pass;
                  $_SESSION['empid']=$eid;
                  header('Location:admin.php');
               
               }
               
               else
               
               {         
                     
                  $pass = md5($row['password']);
                  $lname = $row['loginname'];
                  $eid = $row['empid'];
                        
                  if(!$username == $lname  or !password == $pass)
                     {
                        header('Location:admin.php');
                     }   
                  else
                     {   
                        if ($username == $lname  and  $password == $pass and $loginstatus == '1')
                        
                           {   
                           
                              $_SESSION['ename'] = $emname;
                              $_SESSION['uname'] = $username;
                              $_SESSION['pass'] = $pass;
                              $_SESSION['empid']=$eid;
               
                              header('Location:emplogoutform.php');
                           
                           }
                        else
                           {
                                    
                              $_SESSION['ename'] = $emname;
                              $_SESSION['uname'] = $username;
                              $_SESSION['pass'] = $pass;
                              $_SESSION['empid']=$eid;
                              //$_SESSION['lid']=$row1['todaysdate'];
                              $_SESSION['lid']=$b;
                              
                              header('Location:emploginform.php');
                              
                           }   
                     }   
               }
         }
    }   

?>






Wat i want is there are 3 if statements, 1st and 2nd checks for entering into admin or pmpage. 3rd if statement checks whether the password and username and status is '1' then its should go to logout page. else it should go to login page so for entering todays date and time and other deails.
But when i enter wrong password it should go to same index.php page, but its going on login in page which page where the above code iswritten and shows blank white page as result. The above code is also not checking the status. Have i done any mistake checking condition for status Please let me know
Thanks in advance.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Mon 24 Aug '09 11:22    Post subject: Reply with quote

First of all, you don't need the while. With that SQL Statement there should be only one result or no result.
Next issue you nowhere defined $loginstatus. Maybe you should use $row['loginstatus'] ?
Quote:

if (!$username == $lname or !password == $pass) {

!password is not correct. You are missing the $.

$password == $password is crap! I think you want to use $password == $row['password']

$pass = md5($row['password']); I think this does not make sence since the password is already stored as md5 in the DB else you wouldn't transform that for your SQL statement ( $password = md5($_POST['password']); )
Back to top


Reply to topic   Topic: If else not working properly in php View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner