doudou3935
doudou3935
2019-04-21 19:40

如果登录成功,我如何使用header()函数重定向到索引?

已采纳

I have created a website and am implementing login functionality. I have created separate php file for main menu and am including it as required. When I try to login it says header has already been sent. I am new to php and wanted to implement master page and child pages but session seeems to create a problem.

Tried storing Request_URI in session and redirect using header for index to login if not logged in and from login to index on successfull login.

index page

Login page

<?php include('includes/session/mysession.php') ?>
<?php include('includes/navigation/navigation.php') ?>
<?php include('DbConnections/config.php') ?>

<section id="login-page-wrap" class="section-padding">
<div class="container">
    <div class="row">
        <div class="col-lg-4 col-md-8 m-auto">
            <div class="login-page-content">


                <?php

                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    $uname = $_POST['username'];
                    $pwd = $_POST['password'];
                    if (empty($uname) || empty($pwd)) {
                        $output = "Please enter all fields.";
                        echo 'output : ' . $output;
                    } else {

                        $uname = $mysqli->real_escape_string($uname);
                        $pwd = $mysqli->real_escape_string($pwd);

                        echo $uname;
                        echo $pwd;

                        $query = $mysqli->query("SELECT id FROM users 
                                                WHERE username = '$uname' 
                                                AND password = md5('$pwd')");

                        echo 'query done';
                        if ($query->num_rows == 0) {
                            $output = "Invalid username/password";
                            // echo $output;
                        } else {
                            echo 'Success';
                            $_SESSION['loggedin'] = TRUE;
                            $_SESSION['user'] = $uname;

                            $output = "WELCOME " . $_SESSION['user'];
                            // On successful login
                            $redirect = $_SESSION['redirect_to'];
                            // unset the session var
                            unset($_SESSION['redirect_to']);
                            header("Location: $redirect");
                            exit();
                            // header('location: index.php');
                            // echo $output;
                        }
                    }
                }
                //echo 'output : ' . $output;
                ?>
                <?php
                if (!isset($_SESSION['loggedin'])) {  ?>
                    <div class="login-form">
                        <h3>Welcome !</h3>
                        <form method="POST">
                            <div class="username">
                                <input name="username" type="text" placeholder="Email or Username">
                            </div>
                            <div class="password">
                                <input name="password" type="password" placeholder="Password">
                            </div>
                            <div class="log-btn">
                                <button type="submit"><i class="fa fa-sign-in"></i> Log In</button>
                            </div>
                        </form>
                    </div>
                <?php
                } else {
                    echo 'Done.';
                }
                ?>
            </div>
        </div>
    </div>
</div>
</section>

config.php

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
//pass variables for values
$dbserver = 'localhost';
$dbusername = 'root';
$dbpassword = '';
$dbname = 'cs';
define('DB_SERVER', $dbserver);
define('DB_USERNAME', $dbusername);
define('DB_PASSWORD', $dbpassword);
define('DB_NAME', $dbname);

/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if ($mysqli === false) {
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
?>

mysession.php

<?php session_start();?>

mainmenu.php

<?php
$HomeActive = '';
$ServiceActive = '';
$CarsActive = '';
$AboutActive = '';
$BlogActive = '';
$PagesActive = '';
$ContactsActive = '';
?>
<div class="col-lg-8 d-none d-xl-block">
    <nav class="mainmenu alignright">
        <ul>
            <li class="<?php echo $HomeActive; ?>"><a href="#">Home</a>
                <ul>
                    <li><a href="index.php">Home 1</a></li>
                    <li><a href="index2.php">Home 2</a></li>
                    <li><a href="index3.php">Home 3</a></li>
                </ul>
            </li>
            <li class="<?php echo $AboutActive; ?>"><a href="about.php">About</a></li>
            <li class="<?php echo $ServiceActive; ?>"><a href="services.php">services</a></li>
            <li class="<?php echo $CarsActive; ?>"><a href="#">Cars</a>
                <ul>
                    <li><a href="car-left-sidebar.php">Car Left Sidebar</a></li>
                    <li><a href="car-right-sidebar.php">Car Right Sidebar</a></li>
                    <li><a href="car-without-sidebar.php">Car Without Sidebar</a></li>
                    <li><a href="car-details.php">Car Details</a></li>
                </ul>
            </li>
            <li class="<?php echo $PagesActive; ?>"><a href="index.php">Pages</a>
                <ul>
                    <li><a href="package.php">Pricing</a></li>
                    <li><a href="driver.php">Driver</a></li>
                    <li><a href="faq.php">FAQ</a></li>
                    <li><a href="gallery.php">Gallery</a></li>
                    <li><a href="help-desk.php">Help Desk</a></li>
                    <li><a href="login.php">Log In</a></li>
                    <li><a href="register.php">Register</a></li>
                    <li><a href="404.php">404</a></li>
                </ul>
            </li>
            <li class="<?php echo $BlogActive; ?>"><a href="#">Blog</a>
                <ul>
                    <li><a href="article.php">Blog Page</a></li>
                    <li><a href="article-details.php">Blog Details</a></li>
                </ul>
            </li>
            <li class="<?php echo $ContactsActive; ?>"><a href="contact.php">Contact</a></li>
        </ul>
    </nav>
</div>```

navigation.php

<!DOCTYPE html>
<html cla s s="no-js" la n g="zx x">

    <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--=== Favicon ===-->
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />

    <title>Cardoor - Car Rental HTML Template</title>

    <!--=== Bootstrap CSS ===-->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <!--=== Slicknav CSS ===-->
    <link href="assets/css/plugins/slicknav.min.css" rel="stylesheet">
    <!--=== Magnific Popup CSS ===-->
    <link href="assets/css/plugins/magnific-popup.css" rel="stylesheet">
    <!--=== Owl Carousel CSS ===-->
    <link href="assets/css/plugins/owl.carousel.min.css" rel="stylesheet">
    <!--=== Gijgo CSS ===-->
    <link href="assets/css/plugins/gijgo.css" rel="stylesheet">
    <!--=== FontAwesome CSS ===-->
    <link href="assets/css/font-awesome.css" rel="stylesheet">
    <!--=== Theme Reset CSS ===-->
    <link href="assets/css/reset.css" rel="stylesheet">
    <!--=== Main Style CSS ===-->
    <link href="style.css" rel="stylesheet">
    <!--=== Responsive CSS ===-->
    <link href="assets/css/responsive.css" rel="stylesheet">


    <!--[if lt IE 9]>
        <script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body class="loader-active">

    <!--== Preloader Area Start ==-->
    <!-- <div class="preloader">
        <div class="preloader-spinner">
            <div class="loader-content">
                <img src="assets/img/preloader.gif" alt="JSOFT">
            </div>
        </div>
    </div> -->
    <!--== Preloader Area End ==-->

    <!--== Header Area Start ==-->
    <header id="header-area" class="fixed-top">
        <!--== Header Top Start ==-->
        <div id="header-top" class="d-none d-xl-block">
            <div class="container">
                <div class="row">
                    <!--== Single HeaderTop Start ==-->
                    <div class="col-lg-3 text-left">
                        <i class="fa fa-map-marker"></i> 802/2, Mirpur, Dhaka
                    </div>
                    <!--== Single HeaderTop End ==-->

                    <!--== Single HeaderTop Start ==-->
                    <div class="col-lg-3 text-center">
                        <i class="fa fa-mobile"></i> +1 800 345 678
                    </div>
                    <!--== Single HeaderTop End ==-->

                    <!--== Single HeaderTop Start ==-->
                    <div class="col-lg-3 text-center">
                        <i class="fa fa-clock-o"></i> Mon-Fri 09.00 - 17.00
                    </div>
                    <!--== Single HeaderTop End ==-->

                    <!--== Social Icons Start ==-->
                    <div class="col-lg-3 text-right">
                        <div class="header-social-icons">
                            <a href="#"><i class="fa fa-behance"></i></a>
                            <a href="#"><i class="fa fa-pinterest"></i></a>
                            <a href="#"><i class="fa fa-facebook"></i></a>
                            <a href="#"><i class="fa fa-linkedin"></i></a>
                        </div>
                    </div>
                    <!--== Social Icons End ==-->
                </div>
            </div>
        </div>
        <!--== Header Top End ==-->

        <!--== Header Bottom Start ==-->
        <div id="header-bottom">
            <div class="container">
                <div class="row">
                    <!--== Logo Start ==-->
                    <div class="col-lg-4">
                        <a href="index.php" class="logo">
                            <img src="assets/img/logo.png" alt="JSOFT">
                        </a>
                    </div>
                    <!--== Logo End ==-->

                    <!--== Main Menu Start ==-->
                    <?php include('includes/navigation/mainmenu.php') ?>

                    <!--== Main Menu End ==-->
                </div>
            </div>
        </div>
        <!--== Header Bottom End ==-->
    </header>
    <!--== Header Area End ==-->```
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douping3427 douping3427 2年前

    You echoed, before the redirect header, that's why you're getting that message. What you want to achieve looks like a client side job. Try replacing the php redirect with js.

    Replace this:

    header("Location: $redirect");
    

    with this

    echo "<script>window.location = '$redirect';</script>";
    
    点赞 评论 复制链接分享