-1

I have tried all methods but still am not seeing a redirect to index.php as expected. I also added ob_start() but it doesn't work. Then I tried header('location:...'). It also didn't work. Then I tried to redirect using JavaScript but that also didn't work. Any suggestions ??

<html>
<head>    

<link rel ="stylesheet" href=css/style.css>
</head>
<body>
<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start();
//if my form is submitted
if(isset($_POST['submit']))
 {

 $username=$_POST['username'];
 $password=$_POST['password'];

$query1= userIdentity($username,$password);
echo 'returning value deisplay';
  echo '\n' .$query1 . '\n'; 
  echo 'i am here';
  if($query1==1){
  //echo 'welcome dude';
  $_SESSION['username'] = $username;
   //    Redirect user to index.php
   //echo "<script type='text/javascript'>  window.location='index.php';      </script>";
 // header("Location: index.php");
   //e//cho 'heeeelo';
   echo "<script type='text/javascript'>window.location.href = 'index.php';  </script>";
        ob_flush();
        exit(0);       
      }

   // header("Location: index.php");
   else 
   {
   echo  " <div class='form'>
   <h3>Username/password is incorrent.</h3>
   <br/>click here to <a href='login.php'>Login</a></div>";
   } 
   //header("Location: index.php");

  }
  else {

 ?>


 <div class="form">
 <h1>Log In</h1>
 <form action="" method='post' name="login">
 <input type ="text" name="username" placeholder="username" required />
 <input type="password" name="password" placeholder="password" required />
 <input name="submit" type="submit" value="login" />
 </form>
 <p> Not registered yet ? <a href='register.php'>Register Here </a> </p>
 </div>

  <?php
  }
 ?>
 </body>
</html>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
fahad pirzada
  • 103
  • 10

2 Answers2

1

Instead of having the html <html><head><body> tags before your php code, have the php code before these and use the header function. Here is the working new version:

<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start();
//if my form is submitted
if(isset($_POST['submit']))
 {

 $username=$_POST['username'];
 $password=$_POST['password'];

$query1= userIdentity($username,$password);
echo 'returning value deisplay';
  echo '\n' .$query1 . '\n'; 
  echo 'i am here';
  if($query1==1){
  //echo 'welcome dude';
  $_SESSION['username'] = $username;
   //    Redirect user to index.php
   //echo "<script type='text/javascript'>  window.location='index.php';      </script>";
 // header("Location: index.php");
   //e//cho 'heeeelo';
   header("Location: index.php);
        ob_flush();
        exit(0);       
      }

   // header("Location: index.php");
   else 
   {
   echo  " <div class='form'>
   <h3>Username/password is incorrent.</h3>
   <br/>click here to <a href='login.php'>Login</a></div>";
   } 
   //header("Location: index.php");

  }
  else {

 ?>
<html>
<head>    

<link rel ="stylesheet" href=css/style.css>
</head>
<body>



 <div class="form">
 <h1>Log In</h1>
 <form action="" method='post' name="login">
 <input type ="text" name="username" placeholder="username" required />
 <input type="password" name="password" placeholder="password" required />
 <input name="submit" type="submit" value="login" />
 </form>
 <p> Not registered yet ? <a href='register.php'>Register Here </a> </p>
 </div>

  <?php
  }
 ?>
 </body>
</html>
Matthew Bergwall
  • 340
  • 1
  • 12
1

Per the PHP manual page for header():

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

So move the PHP code above the starting HTML tags, like the example below. I created a variable to hold the message in case the login attempt fails (i.e. $message). Notice how it is set it to a blank message initially (for good scope) and then set when appropriate. Then it is added to the form down below.

Edit:

You can see this demonstrated in this phpFiddle. Try logging in with any username. If you enter the password pw, then you should be taken to index.php, which on that page display an image with the text phpfiddle.org. Also, I removed the HTML from the else block, so it will always show the form (and insert the login failure message if it is set), though you might want to have it not show the form again if the login fails...

<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start(); 
$message = ''; //initialize to empty, for good scope
//if my form is submitted
if(isset($_POST['submit']))
{

    $username=$_POST['username'];
    $password=$_POST['password'];

    $query1= userIdentity($username,$password);
    if($query1==1){
        $_SESSION['username'] = $username;
        //    Redirect user to index.php</script>";
        header("Location: index.php");
        //...
     }//else:
     else {
          $message = "<div class='form'><h3>Username/password is incorrect.</h3>
          <br />click here to <a href='login.php'>Login</a></div>";
     }
?>
<html>
<head>    
<!-- the rest of the HTML content -->

and in the body, append that incorrect message if it is defined, e.g.:

<div class="form"><?php echo $message; ?> 
    <h1>Log In</h1><!-- rest of the HTML for the form-->
Community
  • 1
  • 1
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Still not working . i am not redirected to index.php when i enter correct username and password . If i enter correct password then again login page is displayed instead of index.php :( – fahad pirzada Dec 16 '16 at 13:22
  • See my updated answer, and [this phpFiddle](http://phpfiddle.org/main/code/n04g-v1dw). – Sᴀᴍ Onᴇᴌᴀ Dec 16 '16 at 15:09