且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTML PHP成功登录后显示用户名

更新时间:2023-12-03 19:47:28

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



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

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

< li class ='active'style ='float:right;'>
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; /跨度&GT;&LT; / A&GT;&LT; /立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?