我正在努力弄清楚如何在登录后用PHP回应当前用户。我知道这很简单,但我无法理解。当我<?php echo $_SESSION['usersignedin']; ?>
时,我会收到“用户名”。在此先感谢您的帮助。
这是我正在使用的。
index.php(登录页面)
<?php
require_once('include.php');
$error = '';
$form = (isset( $_POST['submit']) ? $_POST['submit'] : null);
$username = (isset( $_POST['username']) ? $_POST['username'] : null);
$password = (isset( $_POST['password']) ? $_POST['password'] : null);
if( isset($form) ) {
if( isset($username) && isset($password) && $username !== '' && $password !== '' ) {
$sql = mysql_query("SELECT * FROM `members` WHERE username='$username' and
password='$password';");
if( mysql_num_rows($sql) != 0 ) { //success
$_SESSION['logged-in'] = true;
header('Location: signedin.php');
exit;
} else { $error = "Invalid Username or Password"; }
} else { $error = 'Username and Password not filled out';}
}
?>
<html>
<head>
<title>Sign In</title>
</head>
<body>
<center><h1>Please sign in.</h1></center>
<div id="pagewrapper"> <!-- Start pagewrapper div -->
<div id="login_wrapper">
<div id="login">
<form action="<?php $PHP_SELF; ?>" method="post" >
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="<?php echo "$username";?>" /><br /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /><br /></td>
</tr>
</table>
<br>
<td>
<input name="submit" type="submit" value="Login" />
</td>
<?php
echo "<br /><span style=\"color:red\">$error</span>";
?>
</form>
</div>
</div> <!-- End login wrapper -->
</div> <!-- End pagewrapper div -->
</body>
</html>
signedin.php(登录后的页面)
<?php
require_once('include.php');
if ( !isset($_SESSION['logged-in']) || $_SESSION['logged-in'] !== true) {
header('Location: index.php');
exit;
}
$sql = mysql_query("SELECT * FROM `members` WHERE username='$username' and
password='$password';");
?>
<html>
<head>
<title>Sign in</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<body class="home">
<div id="header">
<div id="welcome">
<p>Hi, <?php echo $_SESSION['usersignedin']; ?> ! <a href="logout.php">logout</a></p>
</div>
<div id="links">
<ul>
</ul>
</div>
</div>
<div id="content">
<div id= "stuff">
<h3 class="stuffheader">Header</h3>
<p> Text </p>
</div>
<div id= "stuff">
<h3 class="stuffheader">Header</h3>
</div>
</div>
</body>
</html>
include.php(DB Stuff)
<?php
session_start();
$host = "localhost";
$username = "root";
$password = "root";
$db = "intranet";
@mysql_connect($host,$username,$password) or die ("error");
@mysql_select_db($db) or die("error");
?>
答案 0 :(得分:1)
查看代码,似乎您实际上并未设置$_SESSION['usersignedin'];
(我不知道它是如何获得值'用户名')。要进行设置,您可以修改以下内容:
if( mysql_num_rows($sql) != 0 ) { //success
$_SESSION['logged-in'] = true;
header('Location: signedin.php');
exit;
} else {
$error = "Invalid Username or Password";
}
对此:
if( mysql_num_rows($sql) != 0 ) { //success
$ui = mysql_fetch_object($sql);
$_SESSION['logged-in'] = true;
$_SESSION['usersignedin'] = $ui->username;
header('Location: signedin.php');
exit;
} else {
$error = "Invalid Username or Password";
}