skip to Main Content

the redirect doesn’t work… and i am getting no errors when i change the header location to a .html page it.. redirects, but when i put .php page it doesn’t, the page just refreshes and clears the input fields

<?php
session_start();
include("connection.php"); //Establishing connection with our database

$error = ""; //Variable for storing our errors.
if(isset($_POST["submit"]))
    {
if(empty($_POST["username"]) || empty($_POST["password"]))
    {
$error = "Both fields are required.";
    }else{   
        // Define $username and $password
        $username=$_POST['username'];
        $password=$_POST['password'];

        // To protect from MySQL injection
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($db, $username);
        $password = mysqli_real_escape_string($db, $password);
        $password = md5($password);

        //Check username and password from database
        $sql="SELECT uid FROM users WHERE username='$username' and password='$password'";
        $result=mysqli_query($db,$sql);
        $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

        //If username and password exist in our database then create a session.
        //Otherwise echo error.

        if(mysqli_num_rows($result) == 1)   {
                                                $_SESSION['username'] = $login_user; // Initializing Session
                                                header("location: elements.php"); // Redirecting To Other Page
                                            }else   {
                                                    $error = "Incorrect username or password.";
                                                    }
        }   
    }
?>

AND HERE IS MY LOGIN FORM.

<?php
include('lgn.php'); // Include Login Script
if ((isset($_SESSION['username']) != '')) 
{
header('Location: elements.php');
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>mbbs in russia login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="smurtsmedia" />
    <meta name="keywords" content="smurts media, photogoraphy" />
    <meta name="author" content="smurtsmedia" />

  <!-- 
    //////////////////////////////////////////////////////

    //////////////////////////////////////////////////////
     -->

    <!-- Facebook and Twitter integration -->
    <meta property="og:title" content=""/>
    <meta property="og:image" content=""/>
    <meta property="og:url" content=""/>
    <meta property="og:site_name" content=""/>
    <meta property="og:description" content=""/>
    <meta name="twitter:title" content="" />
    <meta name="twitter:image" content="" />
    <meta name="twitter:url" content="" />
    <meta name="twitter:card" content="" />

    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <link rel="shortcut icon" href="favicon.ico">

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/animate.css">
    <link rel="stylesheet" href="css/form.css">


    <!-- Modernizr JS -->
    <script src="js/modernizr-2.6.2.min.js"></script>
    <!-- FOR IE9 below -->
    <!--[if lt IE 9]>
    <script src="js/respond.min.js"></script>
    <![endif]-->

    </head>
    <body class="style-2">

        <div class="container">
            <div class="row">
                <div class="col-md-12 text-center">
                    <ul class="menu">
                        <!--<li><a href="index.html">Style 1</a></li>
                        <li class="active"><a href="index2.html">Style 2</a></li>
                        <li><a href="index3.html">Style 3</a></li>-->
                    </ul>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">


                    <!-- Start Sign In Form -->
                    <form method="post" action="" class="fh5co-form animate-box" data-animate-effect="fadeInLeft">
                        <h2>Sign In</h2>
                        <div class="form-group">
                            <label for="username" class="sr-only">Username</label>
                            <input name="username" type="text" class="form-control" id="username" placeholder="Username" autocomplete="off">
                        </div>
                        <div class="form-group">
                            <label for="password" class="sr-only">Password</label>
                            <input name="password" type="password" class="form-control" id="password" placeholder="Password" autocomplete="off">
                        </div>
                        <div class="form-group">
                            <label for="remember"><input type="checkbox" id="remember"> Remember Me</label>
                        </div>
                        <div class="form-group">
                            <p>Not registered? <a href="register.php">Sign Up</a> | <a href="forgot2.html">Forgot Password?</a></p>
                        </div>
                        <div class="form-group">
                            <input type="submit"  name="submit" value="Login" class="btn btn-primary">

                        </div>
                        <div class="form-group"><?php echo $error;?></div>
                    </form>
                    <!-- END Sign In Form -->

                </div>
            </div>
            <div class="row" style="padding-top: 60px; clear: both;">
                <div class="col-md-12 text-center"><p><small>&copy; All Rights Reserved. Designed by <a href="#">SmurtsMedia</a></small></p></div>
            </div>
        </div>

    <!-- jQuery -->
    <script src="js/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="js/bootstrap.min.js"></script>
    <!-- Placeholder -->
    <script src="js/jquery.placeholder.min.js"></script>
    <!-- Waypoints -->
    <script src="js/jquery.waypoints.min.js"></script>
    <!-- Main JS -->
    <script src="js/main.js"></script>

    </body>
</html>

2

Answers


  1. Add a die() OR exit() after your header redirect change code as below,

        if(mysqli_num_rows($result) == 1)   {
    $_SESSION['username'] = $login_user; // Initializing Session
    header("location: elements.php"); // Redirecting To Other Page
    die(); // add die()
    }else   {
    $error = "Incorrect username or password.";
    }
    }
    
    Login or Signup to reply.
  2. use $username instead of $loginuser;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search