3

PHP login Script Generate Session But Not Header() Move to Home page.

<?php
session_start();
$message="";
if(isset($_REQUEST['submit'])) {
    if(count($_POST)>0) {
        $username=$_POST["user_name"];
        $password=$_POST["password"];
        if(!empty($username) && !empty($password)){
            //include  connection file
            require_once "connection.php";
            $sql="SELECT * FROM `admin_users` WHERE `admin_user` LIKE  'username' AND `admin_pass`LIKE'$password'";
            $result = mysqli_query($con,$sql);
            $row=mysqli_fetch_row($result);
            if(is_array($row)) {
                $_SESSION["user_id"] = $row[0];
                $_SESSION["admin_name"] = $row[1];
                $_SESSION["type"]="admin_map";

                mysql_close($dbhandle);
            } else {
                $message = "Invalid Username or Password!";
            }
        }
        if(isset($_SESSION["user_id"]) &&  isset($_SESSION["admin_name"]) &&  isset($_SESSION["admin_name"])=="admin@map") {
            header("Location:user_dashboard.php");
        }
    }
    else{
        $message = "Fill All Fields!";
    }
}
?>
StepUp
  • 36,391
  • 15
  • 88
  • 148
Code Break
  • 33
  • 5
  • 2
    Have you tried `header("Location: /user_dashboard.php");` See http://stackoverflow.com/a/25241503/1604068 – Sevvlor Feb 14 '16 at 10:17

2 Answers2

1

First of all add spaces in your query. And you are missing $ sign before username $username.

$sql="SELECT * FROM `admin_users` WHERE `admin_user` LIKE '$username' AND `admin_pass` LIKE '$password'";

I don't know why are you using LIKE in Login form. This must be check with = operator. (Just Suggestion)

Main issue in your code is that you are using mysqli_* extension and using mysql_close() for closing database connection.

mysql_close($dbhandle);

This should be

mysqli_close($dbhandle);

but use connection close at the end of your PHP script not between.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • I would also **strongly** recommend using `mysqli_real_escape_string()` because you're leaving this query wide open to SQL-injection attacks. http://php.net/manual/en/mysqli.real-escape-string.php#example-1905 – Sevvlor Feb 14 '16 at 16:56
  • @sevvlor yes u r right. Code open for SQL injection – devpro Feb 14 '16 at 17:02
0

change

header("Location:user_dashboard.php");

to

echo "<script>window.locaion='user_dashboard.php'</script>"

or add ob_start()in first line of code

paranoid
  • 6,799
  • 19
  • 49
  • 86