简体   繁体   中英

session_start() error always redirect to login.php

Login (login.php) page works fine but it redirects to the index.php page

login.php

<?php 
 ob_start();
 session_start();  ?>   

<html>
<head>
<meta charset="utf-8">
    <form method="post" action="login.php" >
        <div class="form-group" >
            <input type="text" class="form-control" name="user_name" >
        </div>
        <div class="form-group">
            <input type="password" class="form-control" name="user_pass" >
        </div>
        <input type="submit" name="login" value="Login" > 
    </form>
</body>
</html>

<?php
    include '../includes/connection.php'; /* connection query */
    if (isset($_POST['login'])) {
        $username = $_POST['user_name'];
        $userpass = $_POST['user_pass'];

        $admin_query = "select * from admin_login where user_name =  '$username' AND user_pass = '$userpass'";

        $run = mysql_query($admin_query);     
        $rows = mysql_num_rows($run);

        if ($rows == 1) {
            $_SESSION['login_user']=$username;
            header("location: index.php");
        } else {
            echo "<script>alert('User name of password is incorrect')</script>";
        }
    }
?>

In index page (index.php), session has error and always redirects to login.php

index.php

<?php
    session_start();
    if(!isset($_SESSION['login_user'])){
    header('Location: login.php');
    }
    else
    {
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    <div class="row cms-admin-panel">
     <div class="col-md-12 "><h5> Welcome: <?php echo $_SESSION['login_user'] ?>  </h5> <a href="logout.php">logout</a>
    <h4 align="center">CMS Admin Panel</h4>
    </div>
    </div>
    </body>
    </html>
    <?php } ?>

Logout page working fine, it redirect to the login.php page.

logout.php

<?php  
    session_start();
    session_destroy();
    header("location: login.php");
?>

Move the session_start(); before the <form> .

The session_start() function must be the very first thing in your document. Before any HTML tags.

Where exactly do I put a SESSION_START?

Long answer:

Since sessions are handles by cookies by default,

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

http://php.net/manual/en/function.session-start.php

First of all, your session_start() should be at the very top of the page. Secondly, if you ever have some kind of situation where you have more than one session_start() in a page, always check if session is started already or not as,

if(session_status() == PHP_SESSION_NONE) {
   session_start();
}

Third reason, if header are already sent or some kind of session cache limiter error, you can fix that by output buffer started at the top of page as

ob_start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM