会话变量在页面之间仍然未声明

时间:2015-04-30 10:52:52

标签: php session session-variables

我使用以下代码

创建了一个LoginPage.php
(number_of_items, ..., Nmax)

它的作用是检查登录,当登录成功时,用户将通往网站的index.php

但是,index.php页面没有检测到$ _SESSION [&#39;&#39;]变量,即它说<? session_start(); if (isset($_SESSION['uid'])) { header('Location: index.php'); } ?> <!DOCTYPE html> <html> <head> <?php include("indexhead.php"); ?> </head> <body> <?php include("myCarousel.php"); echo " <div class='container'> <div class='row'> <div class='col-sm-6 col-md-4 col-md-offset-4'> <h1 class='text-center login-title'>Sign in to continue:</h1> <div class='account-wall'> <form class='form-signin' method='POST'> <input name='uid' type='text' class='form-control' placeholder='Username' required autofocus > <input name='pass' type='password' class='form-control' placeholder='Password' required > <button class='btn btn-lg btn-primary btn-block' type='submit'> Sign in</button> </form> </div> </div> </div> </div> "; if (isset($_POST['uid']) and isset($_POST['pass'])) { if (empty($_POST['uid']) or empty($_POST['pass'])) { echo "Please type data into the login"; } else { $uname = stripslashes($_POST['uid']); $pass = stripslashes($_POST['pass']); $nick = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); if ($nick != '') { $_SESSION['Nick1'] = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); $_SESSION['FullName'] = selectSpecific("select FullName from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); $_SESSION['dept'] = selectSpecific("select dept from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); $_SESSION['accesslevel'] = selectSpecific("select accesslevel from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); $_SESSION['uid'] = selectSpecific("select uid from hera.LoginTable where uid ='".$uname. "' and pass = md5('".$pass."');"); header("Location: index.php"); //echo $_SESSION['Nick1']; } else { echo "Unknown Username/Password"; } } } ?> </body> </html> :注意:未定义的索引:C:\ xampp \ htdocs中的uid第14行的Hera \ index.php

但我可以确认会话变量确实从函数selectSpecific()接收内容。为什么会议没有举行?

这是index.php的代码

$_SESSION['uid']

3 个答案:

答案 0 :(得分:1)

我建议将其分解为最简单的迭代,只需要1页进行会话初始化

<?php
    session_start();

    $_SESSION['uid'] = 12345;

和另一个回声。

<?php
session_start();

var_dump($_SESSION);

如果它出现空洞,那么PHP session handler会发生一些事情(这里有很多好消息)。

还要确保建立会话。默认情况下它应该使用cookie(php.ini中的session.use_cookies = 1),所以你应该看到&#34; PHPSESSID&#34;饼干套。没有它,就没有办法实际开会。

答案 1 :(得分:0)

首先检查您的会话值,然后更改您的html,如下所示,您还没有提供名称提交按钮

 <form class='form-signin' method='POST'>

                            <input name='uid' type='text' class='form-control' 
                            placeholder='Username' required autofocus >
                            <input name='pass' type='password' class='form-control' 
                            placeholder='Password' required >

                            <input class='btn btn-lg btn-primary btn-block' type='submit' name='submit'/>

而不是像下面那样更改你的php if condition

     if(isset($_POST['submit']))
         {
       if(isset($_POST['uid']) && isset($_POST['pass']))
        {
            if (empty($_POST['uid']) || empty($_POST['pass']))
            { 
                echo "Please type data into the login";                 
            } else
                 {       
                $uname = stripslashes($_POST['uid']);
                $pass = stripslashes($_POST['pass']);

         $nick =mysqli_query("select * from `hera.LoginTable` where `uid` ='".$uname."' and `pass` = '".md5($pass)."'","$dbcon");
  $rowcount=mysqli_num_rows($nick);
                if ($rowcount==1)
                {
                $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

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

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

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

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

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

                    header("Location: index.php");

                }
                else
                {
                    echo "Unknown Username/Password";
                }
        }   
    }   }
    ?> 

并使用多个查询为什么不使用下面的单个查询

"select * from `hera.LoginTable` where `uid` ='".$uname.
                    "' and `pass` = '".md5($pass)."'");

答案 2 :(得分:0)

我忘了把<?php放在我的LoginForm.php文件的最顶层。