在索引页面显示用户名 [英] Display Username in Index page

查看:57
本文介绍了在索引页面显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 SO 中有很多关于此的问题.但是,我拥有的代码与那里略有不同.

我有的是登录页面,我必须先登录才能进入索引页面.

首先,我想说我从某人那里获得的这段代码几天前不起作用.但是当我今天再次检查它时,令人难以置信的是它现在正在工作.

那么,今天我在这里有两个问题,如下:

<小时>

  1. 我想在索引页中显示用户信息.其实我只想显示用户名.但是,由于我在数据库表中没有用户名行,如果它只能显示用户电子邮件,我会暂时接受它.

<小时>

  1. 我希望这里的一些专家审查我拥有的代码.正如我上面提到的,几天前的代码不起作用.今天它可以工作,而我没有改变任何一件事.

<小时>

这里的重要问题是第一个问题,即显示用户信息.

这是我的代码:

1.索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>仪表板</title><link rel="stylesheet" type="text/css" href="css.css"/><身体><?php echo "$myemail";?>

<小时>

2.UserSessionAdmin.php

<小时>

3.登录.html

 

<form method="post" action="CheckLogin.php"><span class="identity">电子邮件</span><input type="text" id="myemail" name="myemail" class="text"/><span class="identity">密码</span><input type="text" id="mypassword" name="mypassword" class="text"/><p id="按钮"><input type="submit"/></p></表单>

4.CheckLogin.php

prepare("SELECT * FROM user WHERE myemail = :myemail AND mypassword = :mypassword");$STM->bindParam(':myemail', $myemail);$STM->bindParam(':mypassword', $mypassword);$STM->execute();$count = $STM->rowCount();$row = $STM ->拿来();如果($count==1){$_SESSION['login_id']=$row['id'];if($_SESSION['login_id'] == 1) {标头(位置:index.php");}别的 {标头(位置:login3.html");}}别的{header("位置:login4.html");}$dbh = 空;?>

<小时><小时>

谢谢大家的帮助.干杯和问候,

解决方案

CheckLogin.php

if ( $count == 1 ) {$_SESSION['login_id'] = $row['id'];$_SESSION['username'] = $row['username'];//添加if ( $_SESSION['login_id'] != '' || $_SESSION['login_id'] > 0 ) {//编辑header("位置:index.php");} 别的 {header("位置:login3.html");}}

Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>仪表板</title><link rel="stylesheet" type="text/css" href="css.css"/><身体><?php echo $username;?>//已编辑

I know there are so many questions about this in SO. However, the code I have is a little different from there.

What I have is the login page, in which I have to login first to enter the index page.

First, I want to say that this code I have from someone was not working a few days ago. But when I check it again today, unbelievable it is working now.

So, today I have two questions here, as follows:


  1. I want to show the user information in the index page. Actually I want to show only the username. However, as I don't have username row in the table of the database, I will accept it for a moment if it will only be able to show user email.


  1. I want some experts here to review the code I have. As what I have mentioned above, the code a few days ago is not working. And today it works without there is single thing I have changed.


The important question here is the question number one, which is to show the user information.

Here is the code I have:

1. Index.php

<?php
include('UserSessionAdmin.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DASHBOARD</title>
<link rel="stylesheet" type="text/css" href="css.css"/>


</head>

<body>
<?php echo "$myemail";?>


2. UserSessionAdmin.php

<?php
session_start();

if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
    header('location: login.html');
    exit();
}

require_once('configPDO.php');
?>


3. login.html

                <div id="login-form">
                <form method="post" action="CheckLogin.php">
                    <span class="identity">E-mail</span>
                        <input type="text" id="myemail" name="myemail" class="text" />
                    <span class="identity">Password</span>
                        <input type="text" id="mypassword" name="mypassword" class="text" />
                    <p id="button">
                        <input type="submit"/></p> 
                </form>
            </div>

4. CheckLogin.php

<?php
// Start Session because we will save some values to session varaible.
session_start();

   include("configPDO.php");

   $myemail=$_POST['myemail']; 
   $mypassword=$_POST['mypassword']; 

   $STM = $dbh->prepare("SELECT * FROM user WHERE myemail = :myemail AND mypassword = :mypassword");

   $STM->bindParam(':myemail', $myemail);
   $STM->bindParam(':mypassword', $mypassword);

   $STM->execute();

   $count = $STM->rowCount();

   $row  = $STM -> fetch();

   if($count==1)

   {
     $_SESSION['login_id']=$row['id'];

    if($_SESSION['login_id'] == 1) { 
        header( "location:index.php");  
    }
    else { 
        header( "location:login3.html");  
    }

}

else 
{
header("location:login4.html");
}

$dbh = null;
?>



Thank you for all your help. Cheers and regards,

解决方案

CheckLogin.php

if ( $count == 1 )  {
    $_SESSION['login_id'] = $row['id'];
    $_SESSION['username'] = $row['username']; // added

    if ( $_SESSION['login_id'] != '' || $_SESSION['login_id'] > 0 ) { // edited
        header("location: index.php");  
    } else { 
        header("location: login3.html");  
    }
}

Index.php

<?php
    require_once 'UserSessionAdmin.php'; // edited
    $username = $_SESSION['username']; // added
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>DASHBOARD</title>
    <link rel="stylesheet" type="text/css" href="css.css"/>
</head>

<body>
<?php echo $username; ?> // edited

这篇关于在索引页面显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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