我用下面的代码创建了一个LoginPage.php
<?
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>它所做的是检查登录,当登录成功时,用户被引导到网站的index.php
但是,index.php页面没有检测到$_SESSION'‘变量,即在第14行的$_SESSION['uid']中显示:注意:未定义的索引: uid
但是我可以确认会话变量确实从函数selectSpecific()接收内容。为什么它不是由会话持有的?
下面是index.php的代码
<?php
session_start();
echo $_SESSION['uid'];
?>发布于 2015-04-30 19:35:55
我建议将其分解为最简单的迭代,并且只有一个带有会话初始化的页面
<?php
session_start();
$_SESSION['uid'] = 12345;还有一个有回声的。
<?php
session_start();
var_dump($_SESSION);如果结果是空的,那么PHP session handler肯定出了什么问题(这里有很多好信息)。
还要确保已建立会话。默认情况下,它应该使用cookie (php.ini中的session.use_cookies=1),因此您应该看到一个"PHPSESSID“cookie集。没有这一点,就没有办法真正地进行会话。
发布于 2015-04-30 19:42:43
首先检查您的会话值,然后更改您的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)."'");发布于 2015-05-05 10:42:56
我只是忘了把<?php放在我的LoginForm.php文件的最上面。
https://stackoverflow.com/questions/29965801
复制相似问题