duanfei9278 2016-02-23 03:56
浏览 34

会话未开始

I just finished my login form, and everything seems to be working properly, except that when I try logging in, it redirects me back to the login page. I've tried multiple solutions but none work. Here is my code:

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条回答 默认 最新

  • douyingmou1389 2016-02-23 04:01
    关注

    Sessions merely persist data across multiple requests to PHP, and the fact that this is an ideal place to store the user's authentication state does not mean that it should only be started during authentication.

    Also, the name session_start() is a bit misleading because it does not start a session so much as open one if it exists, or start a new one. The first invocation to session_start() will set a cookie in the user's browser with the PHPSESSID variable that is passed back in the $_COOKIE superglobal array on subsequent requests. If session_start() detects this variable it loads the data from the server's session cache into $_SESSION, but only when session_start() is called.

    This is why you should be calling session_start() for all requests, and why if should be one of the very first things in your script. Personally, all of my projects have the same 2 lines at the beginning of the global header:

    Add session_start() on each page.

    Echo session value so you can get idea that session started or not.

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

    session_start(); will set cookies therefore should be called before any output. To make sure it will happen it should be the first thing called in anyone script that require use of the $_SESSION superglobal.

    For more info check this answer

    Have a look at this

    Hope it will help and clear your doubts.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line