所以我在会话变量中存储了多个字段。当我在其他页面中使用它们时,它将无法工作。错误说不明的索引。流程是这样的。 index.php这是我的登录页面将值传递给getlogin.php然后它将其重定向到index_s.php,我需要显示登录的用户。这里是代码
的index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signin</title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" href="css/local.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div id="wrapper">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">V-cart</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active"><a href="index.php"><i class="fa fa-list-ol"></i> Sign in</a></li>
<li><a href="register.php"><i class="fa fa-font"></i> Register</a></li>
</ul>
</div>
</nav>
<div class="container">
<div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="panel panel-info" >
<div class="panel-heading">
<div class="panel-title">Sign In</div>
<div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#"></a></div>
</div>
<div style="padding-top:30px" class="panel-body" >
<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
<form action="getlogin.php" method="post" enctype="multipart/form-data" id="getlogin" class="form-horizontal" role="form">
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" type="text" class="form-control" name="username" placeholder="username or email">
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="password" type="password" class="form-control" name="password" placeholder="password">
</div>
<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
</label>
</div>
</div>
<div style="margin-top:10px" class="form-group">
<!-- Button -->
<div class="col-sm-12 controls">
<button input type="submit" name="submit" value="Upload" class="btn btn-info"/>Login </a>
</div>
</div>
<div class="form-group">
<div class="col-md-12 control">
<div style="border-top: 1px solid#888; padding-top:15px; font-size:85%" >
Don't have an account!
<a href="register.php">
Register
</a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
getlogin.php
<?php
session_start();
define('DB_NAME', 'userdb');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '127.0.0.1');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("userdb");
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$user = $_POST['username'];
$pass = $_POST['password'];
$sql = "SELECT count(*) FROM users WHERE(
username='".$user."' or emailaddress='".$user."' and password='".$pass."')";
$result = mysql_fetch_array(mysql_query($sql));
if($result[0]>0)
{
$_SESSION['isvalid'] = true ;
$_SESSION['susername']=$user;
$_SESSION['suid'] = $result['uid'];
$_SESSION['semail'] = $reult['emailaddress'];
$_SESSION['sfirstname'] = $reult['firstname'];
$_SESSION['slastname'] = $reult['lastname'];
header('Location: index_s.php');
}
else
{
header('Location: index2.php');
}
mysql_close();
?>
index_s.php
<?php session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timeline </title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" href="css/local.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div id="wrapper">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="timeline.php">V-cart</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li><a href="timeline.php"><i class="fa fa-font"></i> Timeline</a></li>
<li><a href="request.php"><i class="fa fa-font"></i> Request Ad</a></li>
<li><a href="post.php"><i class="fa fa-font"></i> Post Ad</a></li>
</ul>
<ul class="nav navbar-nav navbar-right navbar-user">
<li class="dropdown messages-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-envelope"></i> Messages <span class="badge"></span> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">No Messages</li>
<li class="message-preview">
<a href="#">
<span class="avatar"><i class="fa fa-bell"></i></span>
<span class="message">Security alert</span>
</a>
</li>
<li class="divider"></li>
<li class="message-preview">
<a href="#">
<span class="avatar"><i class="fa fa-bell"></i></span>
<span class="message">Security alert</span>
</a>
</li>
<li class="divider"></li>
<li><a href="#">Go to Inbox <span class="badge"></span></a></li>
</ul>
</li>
<li class="dropdown user-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> <?php echo $_SESSION['susername'] ; ?><b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="profileme.php"><i class="fa fa-user"></i> Profile</a></li>
<li><a href="#"><i class="fa fa-gear"></i> Settings</a></li>
<li class="divider"></li>
<li><a href="index.php"><i class="fa fa-power-off"></i> Log Out</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<hr />
<div class="container">
<div class="page-header">
<h1 id="timeline">User Events</h1>
</div>
<?php
define('DB_NAME', 'userdb');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '127.0.0.1');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("userdb");
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$sql = "SELECT * FROM postad";
$myData = mysql_query($sql,$link);
$count = "SELECT count(bid) FROM postad";
$record= mysql_fetch_array($myData);
mysql_close();
?>
<ul class="timeline">
<li>
<div class="timeline-badge info"><i class="fa fa-unlock"></i></div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title"><?php echo $record['title']; ?></h4>
<span><i class="fa fa-clock-o fa-2x"></i></span>
<hr />
</div>
<div class="timeline-body">
Author : <?php echo $record['author']; ?> <br>
Semester : <?php echo $record['semester']; ?> <br>
Details: <?php echo $record['description']; ?> <br>
</p>
</div>
</div>
</li>
<li class="timeline-inverted">
<div class="timeline-badge success"><i class="fa fa-unlock"></i></div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title">Book 2</h4>
<span><i class="fa fa-clock-o fa-2x"></i> 57 minutes ago</span>
<hr />
</div>
<div class="timeline-body">
<p>Details</p>
</div>
</div>
</li>
<li>
</div>
</div>
</li>
</ul>
</div>
</div>
</body>
</html>
我只是在线显示登录index_s.php的用户名 echo $ _SESSION ['susername']; 在所有三个中,我从未使用过破坏性但仍然表示不明的指数。这是我使用会话的正确方法吗?
答案 0 :(得分:0)
你的会话看不出有什么问题,但你确定它首先设置它们吗?
尝试在设置后立即回复它们。
同时尝试使用if ($result->num_rows > 0) {
代替if($result[0]>0) {
获取结果集时也会出现拼写错误
$ _ SESSION [&#39; semail&#39;] = $reult
[&#39; emailaddress&#39;];应该是$ result,这同样适用于其他一些行:)