简体   繁体   English

session_start()错误始终重定向到login.php

[英]session_start() error always redirect to login.php

Login (login.php) page works fine but it redirects to the index.php page 登录(login.php)页面工作正常,但它重定向到index.php页面

login.php 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)中,会话有错误,并且始终重定向到login.php

index.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. 注销页面工作正常,它重定向到login.php页面。

logout.php logout.php

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

Move the session_start(); 移动session_start(); before the <form> . <form>之前。

The session_start() function must be the very first thing in your document. session_start()函数必须是文档中的第一件事。 Before any HTML tags. 在任何HTML标记之前。

Where exactly do I put a SESSION_START? 我到底在哪里放置SESSION_START?

Long answer: 长答案:

Since sessions are handles by cookies by default, 由于默认情况下会话是cookie的句柄,

To use cookie-based sessions, session_start() must be called before outputing anything to the browser. 要使用基于cookie的会话,必须在将任何内容输出到浏览器之前调用session_start()。

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

First of all, your session_start() should be at the very top of the page. 首先,您的session_start()应该位于页面的顶部。 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, 其次,如果您遇到某种情况,即页面中有多个session_start(),请始终检查会话是否已经启动,

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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