所以我有一个简单的数据库用于登录我的网站,但是我无法显示用户是否已登录。
session_start();
$username = $_POST['Username'];
$salt = substr($username, 0, 2);
$password = crypt($_POST['Password'], $salt);
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$query = $dbh->prepare("SELECT * FROM `7Ducklings` WHERE Username = ? AND Password = ?");
$array = array($username, $password);
$query->execute($array);
$numrows = $query->fetchColumn();
if($numrows == 1)
{
$_SESSION['Username'] = $username;
}else{
}
$dbh = null;
我希望如此,如果用户已登录以替换此div标签的内容:
<div id="duckdiv">
<form id="UserPass" method="POST" action="Check.php">
Username:<input type="text" placeholder="Username" name="Username">Password:<input type="password" placeholder="Password" name="Password">
<a href="#"><img src="ducklogin.png"></a>
</form>
</div>
有了这个:
<p>"Welcome back:" $_SESSION['Username']</p>
这怎么可能?
答案 0 :(得分:0)
在使用$ _SESSION超全局数组之前和渲染任何内容之前,您必须先启动会话。
session_start();
if($numrows == 1)
{
$_SESSION['Username'] = $username;
}
HTML视图:
<?php if (isset($_SESSION['Username'])) : ?>
// render the welcome message
<?php else : ?>
// render the form
<?php endif ?>