php 登录后回显您好用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15736916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:51:44  来源:igfitidea点击:

echo Hello username after login

phphtmllogin

提问by S H

The first thing the user is asked is to login; if the login is successful the user is sent to the index.php if not; they're asked to re-enter their details.

要求用户的第一件事是登录;如果登录成功,则将用户发送到 index.php,否则;他们被要求重新输入他们的详细信息。

I want the username to display on the index.php AFTER the user logs in;

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

so using an echo function in the index.php file to GETthe username from the login

所以在 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");

?>

回答by Class

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.

should tell them Welcome 'username'then redirect to your page after 5 seconds, you can change 5 to any number you think is long enough. It might be advisable to use the meta since the header might give you an error/warning.

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

EDIT:

编辑:

It looks like you want to print Welcome 'username'on the index.php page you can do and and sure you have session_start()at the top of each page.

看起来你想Welcome 'username'在 index.php 页面上打印你可以做的,并且确保你session_start()在每个页面的顶部都有。

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

and remove echo "Welcome '.$username.'";from your login page. If you want it display once I can modify my answer if you want.

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

回答by acutesoftware

You cant echo any output before calling 'header', otherwise you will get that error.

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

The echo command needs to be done in the called page nindex.php

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

回答by samayo

<?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! ';  
  }

In index.phpput

index.php 中放入

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

}