0

I am facing this php header redirection issue with bootstrap, Please find the code below,

Index file:

 <?php
    if(!isset($_SESSION)){
    session_start();
    }
    include("db.php");
    ?>
    <html>
    <head>
    <title>
    </title>
    <!-- jQuery Imports 
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>-->
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1     /css/bootstrap.min.css">

    <!-- Latest compiled and minified JavaScript 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js">    </script>-->
     <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>

    <!-- custom Js -->
    <script src="js/bookrate.js"></script>
    <style type="text/css">
    body{padding-top:20px;}

    </style>
    </head>
    <body>
    <div class="cycle">
    <h3 class="text-center">--</h3><br>
    <h2 class="text-center"><small>Admin Panel</small></h2><br>
    </div>
    <div class="container">
    <div class="row">

        <div class="col-md-4 col-md-offset-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Please sign in</h3>
                </div>
                <div class="panel-body">
                    <form id="adminLogin" method="POST">
                    <fieldset>
                        <div class="form-group">
                            <input class="form-control" placeholder="E-mail" name="email" type="text" id="email">
                        </div>
                        <div class="form-group">
                            <input class="form-control" placeholder="Password" name="password" type="password" value="" id="password">
                        </div>
                        <div class="checkbox">
                            <label>
                                <input name="remember" type="checkbox" value="Remember Me"> Remember Me
                            </label>
                        </div>
                        <input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
                    </fieldset>
                    </form>



                </div>
            </div>
        </div>
    </div>
</div>
</body>
    </html>

And My Db File is:

 <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('mydb');
    ?>

Ajax Admin Login Page:

 <?php
    OB_start();
    session_start();
    include("db.php");
    $adminEmail =$_POST['email'];
    $adminPassword = $_POST['password'];
    $adminLoginQry = "select email from `users` where (email='$adminEmail') and   password='$adminPassword'";
    $retval = mysql_query($adminLoginQry);
    if(mysql_num_rows($retval)==1)
    {
    header("Location: main.php");
    exit;
    }else{
    echo "<span style='color:red;'>Please enter valid Username and Password. </span>";
    } 
    ?>

Main.php

 <?php
     echo "My Menu Page";
    ?>

Once I enter the email id and password, on click of login button both GET and POST requests are triggered, how both the requests are getting triggere I am not able to understand ?. But still Everything working fine, which I am able to confirm with Mozilla firefox firebug (developer console).

Header Location redirection is not happening to the main.php page after the login success. Please tell me what is wrong here..

This question not having answer already, for me header location is working fine, but its not working only with bootstrap 3. Please do not mark this as duplicate.

Naveen
  • 117
  • 2
  • 11
  • please read the question fully then mark as duplicate. Whatever the link you suggested did not worked out for me..... So only I asked as new question... – Naveen Nov 27 '14 at 13:45

2 Answers2

0

Your login is not getting succeed because

mysql_select_db('mydb');

Should be

mysql_select_db('mydb', $conn);

and form should be

<form id="adminLogin" action="adminLogin.php" method="POST">
Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
0

The problem is Headers are already sent, or in simple language the output is already sent to browser when your header() function is invoked.

The best solution is not to pass headers (send html/output to browser) till the point header() function is fired.

Solution :

$url = "main.php";
if (!headers_sent()) {
    header('Location: '.$url);
    exit;
} else {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        exit;
}

This simple code will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.

  • Where something has been output before headers?? – Manish Jangir Nov 27 '14 at 11:38
  • It means somewhere in the code, something was printed to the browser before application had finished preparing the page. –  Nov 27 '14 at 11:41
  • `if(mysql_num_rows($retval)==1) { if (!headers_sent()) { header('Location: '.$url); exit; } else { echo ''; exit; } }else{ echo "Please enter valid Username and Password. "; }` But still no luck Weblineindia. – Naveen Nov 27 '14 at 11:41
  • @Naveen, Have you defined `$url = "main.php";` above this code ? –  Nov 27 '14 at 11:43
  • @Naveen, Then you need to debug your code & check which condition is being executed also print your query data so you can identify the issue. –  Nov 27 '14 at 11:45
  • @Weblineindia, I have added $url = "main.php"; and i checked the query, same query i took and ran in db also. Both giving same result(1 result only). Itseems like everything working, but only that redirection alone is not happening.... – Naveen Nov 27 '14 at 11:59
  • @Naveen, Have you debug your code & check which condition is being executed ? –  Nov 27 '14 at 12:04
  • @weblineindia, i debugged the code, its going inside this condition, if (!headers_sent()) {} – Naveen Nov 27 '14 at 12:12
  • @Ah that is ajax file ? Then you need to return response from ajax file & you use window.location in js file in which you did made your ajax request ! got my point ? –  Nov 27 '14 at 12:15
  • As you told I returned the file name from the php to javascript and there I put window.location.href. Then its worked fine. Really Thanks for your great help PHP Weblineindia. – Naveen Nov 27 '14 at 13:31
  • @Naveen,Glad i could help. can you please accept my answer as a solution ? –  Nov 27 '14 at 13:35