登录后回显Hello用户名

时间:2013-04-01 02:32:45

标签: php html login

要求用户首先登录;如果登录成功,则用户被发送到index.php,如果没有;他们被要求重新输入他们的详细信息。

我想在用户登录后在index.php上显示用户名;

所以在index.php文件中使用echo函数将GET登录的用户名

//check to see if they match
if($username==$dbusername&&$password==$password)
{
$_SESSION['username']=$username;
    echo "Welcome '.$username.'";
    header('Location: nindex.php');
    die(); 
}
else
echo "incorrect password";

}
else
    die("That user does not exist");
}
else
    die("please provide a  username and password");

?>

3 个答案:

答案 0 :(得分:4)

header("Refresh: 5; url=index.php");
echo "Welcome '" . $username . "'";
exit;#should be added so rest of page doesn't load.

OR

echo "Welcome '" . $username . "'";?>
<meta http-equiv="refresh" content="5; url=index.php" />
<?php
exit;#should be added so rest of page doesn't load.

应告诉他们Welcome 'username'然后在5秒后重定向到您的网页,您可以将5更改为您认为足够长的任何数字。建议使用元数据,因为标题可能会给您一个错误/警告。

修改

您希望在index.php页面上打印Welcome 'username',并且确保每页顶部都有session_start()

if(isset($_SESSION['username'])){
    echo "Welcome '{$_SESSION['username']}'";
}

并从您的登录页面中删除echo "Welcome '.$username.'";。如果你想要它显示一次,我可以修改我的答案。

答案 1 :(得分:3)

在调用'header'之前你不能回显任何输出,否则你会得到那个错误。

echo命令需要在被调用的页面nindex.php

中完成

答案 2 :(得分:1)

<?php


  if(!empty($username) && !empty($password)) {

      if($username == $dbusername && $password == $dbpassword) {

            $_SESSION['username'] = $username;
            header("Location: index.php?id=$username");
            die();
      }else {
        echo 'incorrect username/password combination';
      }
  }else {
  echo 'Username and Password NOT FOUND! ';  
  }

index.php 中添加

if(isset($_GET['id'])){
echo 'Welcome '.$_GET['id'];
}else {
echo '(write) a 404 page';

}