会话未开始

时间:2016-02-23 03:56:03

标签: php

我刚刚完成登录表单,一切似乎都正常,但是当我尝试登录时,它会将我重定向回登录页面。我尝试了多种解决方案但没有工作。这是我的代码:

Mainpage.php

<?
include("index.php");
session_start();
if(!isset($_SESSION['id'])){
    header("Location:index.php");
}

?>



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Cable Tech Guide</title>

<!-- Bootstrap -->
<link href ="css/bootstrap.min.css" rel="stylesheet">


<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<style>

    .hitsCounter {

        position:bottom;
        float:right;

    }

</style>
</head>

<body>

    <div class="container">

        <div class="navbar navbar-inverse">

            <div class="navbar-header">

                <a href="#" class="navbar-brand">Twin Cities</a>

                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

                    <span class="sr-only">Toggle navigation</span>

                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>

                </button>

            </div>

            <div class="collapse navbar-collapse">

                <ul class="nav navbar-nav">

                    <li class="active"><a href="index.html">Home</a></li>
                    <li><a href="codes.html">Coding Guide</a></li>
                    <li><a href="cabletypes.html">Cable Types</a></li>
                    <li><a href="dropInt.html">Drop Integrity</a></li>

                </ul>

            </div>

        </div>

        <div class="jumbotron">

            <h2>Update!!  2/14</h2>
            <p>Unfortanely, the login system is taking longer than expected. I will post another update within the week, to keep you guys updated.</p>
            <p>I did add a drop integrity page for everybody to use, and I also updated the XH codes. Everybody enjoy!</p>

        </div>
        <div class="jumbotron">

            <p>After the login system is up and running, I should be able to add some contact numbers on here.</p>
            <p>Any other suggestions would be very helpful!</p>

        </div>

        <img class="hitsCounter" src="/Hits_Counter/gd-count.cgi?page=PageCounter&style=LED&x=16&y=24&nbdigits=1">

    </div>  


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>

的login.php

<?php


    session_start();

    if ($_GET["logout"]==1 AND $_SESSION['id']) { session_destroy();

        $message="You have been logged out. Have a nice day!";

    }

    include("connection.php");


    if ($_POST['submit']=="Sign Up") {

        if (!$_POST['email']) $error.="<br />Please enter your email";
            else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br />Please enter a valid email"; 

        $var1 = ($_POST['accessCode']);
        $var2 = "abC";

        if (strcmp($var1, $var2) !== 0) $error.="<br />That access code is incorrect!";


    if (!$_POST['password']) $error.="<br />Please enter your password";
    else { 


        if (strlen($_POST['password'])<8) $error.="<br />Please enter at least 8 characters";

        if(!preg_match('/[A-Z]/', $_POST['password'])) $error.= "<br />Please include at least one capital letter";
    }
        if ($error) $error = "There were error(s) in your sign up details:".$error;


        else {



        $query= "SELECT * FROM `users` WHERE techNumber ='".mysqli_real_escape_string($link, $_POST['techNumber'])."'";

        $result = mysqli_query($link, $query);  

        $results = mysqli_num_rows($result);

        if ($results) $error = "That tech number is already registered. Do you want to log in?";

        else {

        $query = "INSERT INTO `users` (`techNumber`, `email`, `password`, `accessCode`) VALUES ('".mysqli_real_escape_string($link, $_POST['techNumber'])."', '".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['techNumber']).$_POST['password'])."', '".mysqli_real_escape_string($link, $_POST['accessCode'])."')";

        mysqli_query($link, $query);


        $_SESSION['id']= mysqli_insert_id($link);

        header("Location:mainPage.php");

        }   

    }

}

if ($_POST['submit'] == "Log In") { 

    $query = "SELECT * FROM users WHERE techNumber='".mysqli_real_escape_string($link, $_POST['logintechNumber'])."'AND password='" .md5(md5($_POST['logintechNumber']) .$_POST['loginpassword']). "'LIMIT 1";

    $result = mysqli_query($link, $query);

    $row = mysqli_fetch_array($result);

    if($row){

        $_SESSION['id']=$row['id'];

        header("Location:mainPage.php");





    } else {

        $error = "We could not find a user with that tech number and password. Please try again.";



    }

}


?>

的index.php

<?php include("login.php"); ?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cable Tech Guide</title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<style>

</style>

</head>

<body data-spy="scroll" data-target=".navbar-collapse">

<div class="navbar navbar-default">

<div class="container">

    <div class="navbar-header">

        <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

            <span class="icon-bar"></span>

            <span class="icon-bar"></span>

            <span class="icon-bar"></span>

        </button>

        <a class="navbar-brand">Twin Cities</a>

    </div>

    <div class="collapse navbar-collapse">

        <form class="navbar-form navbar-right" method="post"> 

            <div class="form-group">

                <input type="techNumber" name="logintechNumber" placeholder="Tech Number" class="form-control" value="<?php echo addslashes($_POST['logintechNumber']); ?>" />

            </div>

            <div class="form-group">

                <input type="password" name="loginpassword" placeholder="Password" class="form-control" value="<?php echo addslashes($_POST['loginpassword']); ?>" />

            </div>

            <input type="submit" name= "submit" class="btn btn-success" value="Log In">

        </form>

    </div>

  </div>    

  </div>


  <div class="container contentContainer" id="topContainer">
    <div class="row">

        <div class="col-md-6 col-md-offset-3" id="topRow">

         <h1 class="marginTop">Cable Tech Guide</h1>


         <?php

            if ($error) {

                echo '<div class="alert alert-danger">'.addslashes($error).'</div>';

            }

            if ($message) {

                echo '<div class="alert alert-success">'.addslashes($message).'</div>';

            }

         ?>


         <form class="marginTop" method="post"> 

            <div class="form-group">

                <label for="techNumber">Tech Number</label>

            <input type="techNumber" name="techNumber" class="form-control" placeholder="Your Tech Number" value="<? echo addslashes($_POST['techNumber']); ?>" />

            </div>

            <div class="form-group">

                <label for="email">Email Address</label>

            <input type="email" name="email" class="form-control" placeholder="Your Email" value="<? echo addslashes($_POST['email']); ?>" />

            </div>

            <div class="form-group">

                <label for="password">Password</label>

            <input type="password" name="password" class="form-control" placeholder="Password" value="<? echo addslashes($_POST['password']); ?>" />

            </div>

            <div class="form-group">

                <label for="accessCode">Access Code</label>

            <input type="accessCode" name="accessCode" class="form-control" placeholder="Access Code" value="<? echo addslashes($_POST['accessCode']); ?>" />

            </div>

            <input type="submit" name="submit" value="Sign Up" class="btn btn-success btn-lg marginTop"/> 

         </form>

         </div>

        </div>

  </div>






 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

 <!-- Include all compiled plugins (below), or include individual files as needed -->

 <script src="js/bootstrap.min.js"></script>

 <script>


    $(".contentContainer").css("min-height",$(window).height());

 </script>

 </body>
 </html>

2 个答案:

答案 0 :(得分:1)

我观察您的代码..

我以为你忘了在mainpage.php

上正确打开php标签

你应该像这样使用正确的php开放标签

<?php
请等一下。这是你的问题。

并确保session_start();始终是php代码的第一行。所以更新你的代码如下所示。在mainpage.php

<?php    
session_start();
if(!isset($_SESSION['id'])){
    header("Location:index.php");
}

include("index.php");
?>

我无法理解你的逻辑 但我希望它能解决你的会话问题。

试一试。

答案 1 :(得分:0)

会话只是将数据保存到PHP的多个请求中,并且这是存储用户身份验证状态的理想位置并不意味着它只应在身份验证期间启动。

此外,名称session_start()有点误导,因为它不会start一个会话,如果它存在,或者开始一个新会话。第一次调用session_start()将在用户的浏览器中设置一个cookie,其中PHPSESSID变量会在后续请求中传回$_COOKIE superglobal数组。如果session_start()检测到此变量,则会将服务器会话缓存中的数据加载到$_SESSION,,但仅在调用session_start()时才会加载。

这就是为什么你应该为所有请求调用session_start(),以及为什么if应该是你脚本中的第一件事。就个人而言,我的所有项目在全局标题的开头都有相同的2行:

在每个页面上添加session_start()。

回显会话值,以便您可以了解会话是否已启动。

<?php
error_reporting(E_ALL);
session_start();

session_start();将设置cookie,因此应在任何输出之前调用。为了确保它会发生,它应该是在任何脚本中调用的第一个需要使用$_SESSION超全局的东西。

有关详情,请查看this answer

查看this

希望它有助于澄清您的疑虑。