HTML PHP成功登录后显示用户名 [英] HTML PHP Display username after success login

查看:446
本文介绍了HTML PHP成功登录后显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的onlinestore.html文件中有一个< div> ,这是我的菜单,包含登录/注册。我想要的是成功登录后,登录/注册的< div> 更改为用户名。我做了什么不会显示我想要的预期输出。那么我的代码有什么问题吗?



以下是我所做的:



onlinestore.html

 < li class ='active'style ='float :右;'> 
<?php
session_start();
if($ _ SESSION ['logged'] == true){
echo $ _SESSION [username];
echo'< a href =logout.php>< span>注销< / span>< / a>< / li>';
}
elseif($ _ SESSION ['logged'] == false)
echo'< a href =registerform.html>< span>登录/注册< / span> ;< / A>< /立GT;';
?>

以下是另一个文件
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);}

是预期的输出:


在登录



/ 7fR0s.pngalt =

这里是我用上面的代码得到的:



解决方案

要么您需要将 onlinestore.html login.html 重命名为.php文件所以PHP将在其中工作,或者在你的htaccess文件中使用addtype选项。



在您的 onlinestore.html 中执行以下操作:

 <?php 
session_start(); //在脚本的顶部
?>

< li class ='active'style ='float:right;'>
<?php
if($ _ SESSION ['logged'] == true)
{
echo $ _SESSION [username];
echo'< a href =logout.php>< span>注销< / span>< / a>< / li>';
}
elseif($ _ SESSION ['logged'] == false)
{
echo'< a href =registerform.html>< span>登录/注册及LT; /跨度>< / A>< /立GT;';
}
?>

在您的 checklogin.php 中执行以下操作:

 <?php 
session_start(); //在脚本的顶部
?>

if($ count == 1)
{
$ _SESSION ['logged'] = true;
$ _SESSION ['username'] = $ myusername;
header(Location:onlinestore.html);
exit();
}
else
{
$ _SESSION ['logged'] = false;
header(Location:login.html);
exit();
}

如果上述不起作用,请告诉我发生了什么。 >
您是否设置了用于解析PHP代码的html文件?

或者htaccess文件包含:

AddType application / x-httpd-php .htm .html



编辑



尝试以下方式进行调试:

在您的 checklogin.php 中执行以下操作:

 <?php 
session_start(); //在脚本的顶部
?>

if($ count == 1)
{
$ _SESSION ['logged'] = true;
$ _SESSION ['username'] = $ myusername;
回声登录工作;
exit();
}
else
{
$ _SESSION ['logged'] = false;
回显登录失败;
exit();
}

这会显示登录是否正常。如果不是,并且您登录失败,那么这就是为什么您在页面上获得登录/注册链接的原因。



如果显示登录成功,则将代码重新设置为原来的样子,然后在 onlinestore.html 页面,在文件顶部执行此操作:

  echoThis is working; 
exit();

会发生什么?您是否在页面上看到This is working的信息,而没有其他信息?


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?

Here is what I've done:

onlinestore.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:

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:

解决方案

Either you need to rename your onlinestore.html and login.html to be .php files so the PHP will work in them, or use the addtype option in your htaccess file.

In your onlinestore.html do this:

<?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.php do this:

<?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
?

EDIT

Try this for debugging:

In your checklogin.php do this:

<?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.html page, do this at the top of the file:

echo "This is working";
exit();

What happens? Do you get the message "This is working" on the page and nothing else?

这篇关于HTML PHP成功登录后显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆