duanmi3476 2013-04-10 00:50
浏览 15
已采纳

获取当前用户PHP签名[关闭]

I am having the hardest time trying to figure out how to echo the current user in PHP after logging in. I know it's something simple but I can't figure it out. When I do <?php echo $_SESSION['usersignedin']; ?> I get "username". Thanks in advance for your help.

Here is what I am using.

index.php (sign in page)

<?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 (page after signing in)

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


?>
  • 写回答

1条回答 默认 最新

  • dongwen3410 2013-04-10 01:28
    关注

    Looking through the code, it doesn't seem you've actually set $_SESSION['usersignedin']; (I don't know how it got the value 'username'). To set it you could modify the following:

    if( mysql_num_rows($sql) != 0 ) { //success
        $_SESSION['logged-in'] = true;
        header('Location: signedin.php');
        exit;
    } else { 
        $error = "Invalid Username or Password"; 
    }
    

    To this:

    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"; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测