0

我很难弄清楚登录后如何在 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">
&nbsp;


<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(数据库资料)

<?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");


?>
4

1 回答 1

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"; 
}
于 2013-04-10T01:28:55.563 回答