0

Under my login script after checking the DB pages gets redirected but page messed up. My login page and dashboard page opens at login page rather redirect to dashboard.

my login check script is below

<?php
include_once'\..\database\connection.php';
class loginDetails extends connection{
    private $result,$query,$count;
    public function checkLogin($adusername,$adpassword){
        $adusername=mysqli_real_escape_string($this->con,$adusername);
        $adpassword=mysqli_real_escape_string($this->con,$adpassword);
        $this->query= "select * from quoteslogin where Username='$adusername' AND Password='$adpassword'";
        $this->result= mysqli_query($this->con,$this->query);
        $this->count=  mysqli_num_rows($this->result);
        if($this->count < 1)
        {
            echo "Access Denied";
        }
        else
        {
            $row=  mysqli_fetch_array($this->result);
            $_SESSION["quotes_username"]=$row["Username"];
            $_SESSION["quotes_password"]=$row["Password"];
            header("Location: http://www.google.com/");
//            header("Location:admin/dashboard.php");
            exit();
        }
    }

I put google.com in the location but my login pages shows only loading image and dosnt redirect on google no any error. My script is working as i have checked it and it echo correctly. enter image description here My connection string

class connection {
    private $host="localhost",$dbusername="root",$dbpassword="",$dbname="quotation";
    public $con;

    public function __construct() {
        $this->con=  mysqli_connect($this->host, $this->dbusername, $this->dbpassword, $this->dbname);
    }
}

If i use flush() before the header than script shows "headers already sent...." if i remove it works just like image

Vivek
  • 1,446
  • 18
  • 27

2 Answers2

1

I think it gives a warning header already sent because You can't add any more header lines using the header() function once the header block has already been sent. so try using

echo '<script language="javascript"> window.location="admin/dashboard.php";</script>'
Akhil Sidharth
  • 746
  • 1
  • 6
  • 16
  • I know we can use it but i dont want to put this Javascript in between my php. this is the last option only. I want it to be done with php only – Vivek Feb 03 '14 at 17:55
  • other option will be output buffering. try reading this http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Akhil Sidharth Feb 03 '14 at 17:58
0

I use this for my redirects, it uses fallbacks redirect::avert($url);

class Redirect 
{

    public static function avert($url) 
    {

        if (!headers_sent()) 
        {

            //header has not been sent so redirect
            header($url, true, 302);
            exit;

        } 
        else 
        {

            //redirect using javascript
            echo '<script type="text/javascript">';

            echo 'window.location.href="'.$url.'";';

            echo '</script>';

            //javascript turned off so redirect with meta-refresh
            echo '<noscript>';

            echo '<META HTTP-EQUIV="Refresh" Content="0; URL="' . $url . '">';

            echo '</noscript>';

            exit;

        }
    }
}
Ant
  • 118
  • 3