HTML PHP 登录成功后显示用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20654848/
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
HTML PHP Display username after success login
提问by JK9
I have a <div>inside my onlinestore.html file which is my menu that contain of Login/Register. What I want is after the success login, the <div>for login/register change to the username. What i'hv done wont display the expected output that i want.So is there something wrong about my code?
<div>我的 onlinestore.html 文件中有一个包含登录/注册的菜单。我想要的是成功登录后,<div>登录/注册更改为用户名。我所做的不会显示我想要的预期输出。所以我的代码有问题吗?
Here is what I've done:
这是我所做的:
onlinestore.html
网上商店.html
<li class='active' style='float:right;'>
<?php
session_start();
if($_SESSION['logged']==true){
echo $_SESSION["username"];
echo '<a href="logout.php"><span>Logout</span></a></li>';
}
elseif($_SESSION['logged']==false)
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
?>
Here is another file checklogin.php:
这是另一个文件 checklogin.php:
if($count==1){
session_start();
$_SESSION['logged']=true;
$_SESSION ['username']=$myusername;
header("refresh:1;url=onlinestore.html");
}
else{
$_SESSION['logged']=false;
header("refresh:2;url=login.html");}
Here is the expected output:
这是预期的输出:
Before Login
After Login
Here is what i get with the code above:
登录前
登录后
这是我用上面的代码得到的:
采纳答案by James
Either you need to rename your onlinestore.htmland login.htmlto be .php files so the PHP will work in them, or use the addtype option in your htaccess file.
要么您需要将您的onlinestore.html和重命名login.html为 .php 文件,以便 PHP 在其中工作,要么在您的 htaccess 文件中使用 addtype 选项。
In your onlinestore.htmldo this:
在你onlinestore.html这样做:
<?php
session_start(); // Right at the top of your script
?>
<li class='active' style='float:right;'>
<?php
if($_SESSION['logged']==true)
{
echo $_SESSION["username"];
echo '<a href="logout.php"><span>Logout</span></a></li>';
}
elseif($_SESSION['logged']==false)
{
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>
In your checklogin.phpdo this:
在你checklogin.php这样做:
<?php
session_start(); // Right at the top of your script
?>
if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
header("Location: onlinestore.html");
exit();
}
else
{
$_SESSION['logged']=false;
header("Location: login.html");
exit();
}
If the above doesn't work then please tell me what happens.
Do you have html files set to parse PHP code?
Or a htaccess file with:AddType application/x-httpd-php .htm .html
?
如果上述方法不起作用,请告诉我会发生什么。
您是否设置了 html 文件来解析 PHP 代码?
或用htaccess文件:AddType application/x-httpd-php .htm .html
?
EDIT
编辑
Try this for debugging:
试试这个进行调试:
In your checklogin.phpdo this:
在你checklogin.php这样做:
<?php
session_start(); // Right at the top of your script
?>
if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
echo "Login worked";
exit();
}
else
{
$_SESSION['logged']=false;
echo "Login failed";
exit();
}
This will show you if login is working. If it's not and you get "login failed" then that is why you get the "Login/Register" link on your page.
这将显示登录是否有效。如果不是,并且您收到“登录失败”,那么这就是您在页面上看到“登录/注册”链接的原因。
If it shows "Login worked" then set the code back to how it was and then on your onlinestore.htmlpage, do this at the top of the file:
如果它显示“登录有效”,则将代码设置回原来的状态,然后在您的onlinestore.html页面上,在文件顶部执行此操作:
echo "This is working";
exit();
What happens? Do you get the message "This is working" on the page and nothing else?
发生什么了?您是否在页面上收到消息“这是有效的”,而没有其他消息?
回答by Orion
In your elsestatement you haven't defined a session_start()like you did in your ifstatement.
在您的else声明中,您没有session_start()像在声明中那样定义一个if。
And else, instead of checking the value of a $_SESSION and determining the value of it, you can use the following:
否则,您可以使用以下命令,而不是检查 $_SESSION 的值并确定它的值:
if (session_status() == PHP_SESSION_NONE) {
//Do something if the session is not existing
}
Other options are storing variables in a $_COOKIEand then check if it issetor not(if(isset($_COOKIE["username"]){})
其他选项是将变量存储在 a 中$_COOKIE,然后检查它是否isset( if(isset($_COOKIE["username"]){})
I hope this has helped you out
我希望这对你有帮助
回答by aary trivedi
<div class="msg"><?php echo $msg;?></div>
<form type="submit">
<input type="text" name="Email">
<input type="password" name="Password">
<button type="submit" name="Login">Login</button>
</form>
<?php
require 'connection.php';
if (isset($_POST['Login'])) {
$email = $_POST['Email'];
$password = $_POST['Password'];
$user = "SELECT username FROM table_name WHERE email = '".$email."' and password = '".$password."'";
$user = mysqli_query($con, $user);
if (mysqli_num_rows($user) > 0) {
while ($row = mysqli_fetch_array($user)) {
$username = $row['username'];
}
} else {
$msg = "Can't Log in";
}
}
echo $username;
?>
回答by mgunner
Try this. I don't how to explain but when the login is successful I created the session variable. If the session variable is not in used it will echo the Login that will link to login page otherwise if session variable is being used it will print the username
尝试这个。我不知道如何解释,但是当登录成功时,我创建了会话变量。如果未使用会话变量,它将回显将链接到登录页面的登录名,否则如果正在使用会话变量,它将打印用户名
code in process of logging in
登录过程中的代码
if($row['col_userName'] == $username AND $row['col_custPass'] = $password){
$_SESSION['session_var_user'] = $username; // I created the session variable also don't forget to put the session_start(); at the top of your code
echo "
<script>
window.location.href = 'index.php';
</script>
";
}else{echo "failed";}
code in navbar
导航栏中的代码
<li class="nav-item">
<a class = "justtextstyle" href="#">
<?php
if(!isset($_SESSION['session_variable']))
// if(!isset($_SESSION['session_variable'])) checking if $_SESSION['session_variable' is in use; sorry idk to explain, it's just like that lol
{
echo "<a class = 'justtextstyle' href='login.php'>Log In Here</a>";
}
else
{
echo "Hi," . $_SESSION['session_variable'];
}
?>
</a>
</li>
回答by Han Angeline
Maybe you want to try this instead, it worked for me.
也许你想试试这个,它对我有用。
<?php
if($_SESSION['username'] == true) {
echo '<a href="logout.php"><span>Logout</span></a>';
}
else {
echo '<a href="login.php"><span>Login</span></a>';
}
?>
回答by q0re
Well, there is a lot of code missing.
嗯,有很多代码缺失。
Where is your login script?
你的登录脚本在哪里?
First, you need a <form>with the input fields (login and password) and a submit button.
首先,您需要一个<form>带有输入字段(登录名和密码)和一个提交按钮的按钮。
For example:
例如:
<form action='login.php' method='post'>
<input type='text' name='loginname'></input>
<input type='password' name='loginpassword'></input>
<input type='submit'>Submit</input>
</form>
Your login.php
您的登录名.php
<?php
session_start();
// Dont forget to check your variables
$loginname = $_POST["loginname"];
$password = $_POST["loginpassword"];
// Check the correct login (for example with a database)
if ($login_correct) {
$_SESSION["username"] = $loginname;
$_SESSION["logged"] = true;
header("index.html");
exit;
else {
$_SESSION["logged"] = false;
header("registerform.html");
exit;
}
}
?>
In your index.html you can do:
在您的 index.html 中,您可以执行以下操作:
<?php
session_start();
if($_SESSION['logged'] == true){
echo $_SESSION["username"];
echo '<a href="logout.php"><span>Logout</span></a></li>';
}
else {
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>
回答by Johannes
If you want the username to be displayed instead like in the example you need to change your code for onlinestore.html to the following:
如果您希望像示例中那样显示用户名,您需要将 onlinestore.html 的代码更改为以下内容:
<li class='active' style='float:right;'>
<?php
session_start();
if($_SESSION['logged']==true){
echo '<a href="logout.php"><span>' + $_SESSION["username"] + ' (Logout)</span></a></li>';
}
elseif($_SESSION['logged']==false)
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
?>
回答by SAF
I think your PHP should look something like that:
我认为你的 PHP 应该是这样的:
<?php
session_start();
username = $_SESSION["username"]
if($_SESSION['logged'] == true) {
echo '<a href="logout.php"><span>' . username. 'Logout</span></a></li>';
}
else {
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>
Use either brackets in both if and else or use none. By the way you don't need an elseif if you're only checking a boolean. (there are only 2 possibilities)
在 if 和 else 中使用任一方括号或使用 none。顺便说一句,如果您只检查布尔值,则不需要 elseif。(只有2种可能)


