简体   繁体   English

显示用户名PHP MySql

[英]Display User Name PHP MySql

I'm doing a project at the moment and I'm trying to get a user name to display on pages when they login. 目前,我正在做一个项目,我试图让用户名在用户登录时显示在页面上。 I've tried to echo the user name in various places but it won't work for me. 我试图在各个地方回显用户名,但对我来说不起作用。 Could anyone tell me what code to enter and where please? 谁能告诉我要输入什么代码,请在哪里输入?

<?php 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("project") or die(mysql_error()); 

if(isset($_COOKIE['ID_my_site'])) 

 { 

$username = $_COOKIE['ID_my_site']; 

$pass = $_COOKIE['Key_my_site']; 

$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or       die(mysql_error()); 

while($info = mysql_fetch_array( $check ))   

    { 

    if ($pass != $info['password']) 

        {           header("Location: login.php"); 

        } 

else 

        { 

         echo "Admin Area<p>"; 

echo "Your Content<p>"; 

echo "<a href=logout.php>Logout</a>"; 

        } 

    } 

    } 

else 

{            

header("Location: login.php"); 

} 

?> 

Here is a diagnostic idea, first see that you definately have a strig in the variable 这是一个诊断思路,首先请确保您在变量中肯定有条痕

echo ($username = $_COOKIE['ID_my_site']);

Once you definately have this you should be able to echo it further down in the html section 一旦确定了这一点,您应该可以在html部分中进一步回显它。

if $username variable having email id then Just select the username using your existing query. 如果$ username变量具有电子邮件ID,则只需使用现有查询选择用户名。 then save in session variable or cookie. 然后保存在会话变量或Cookie中。 then echo this session variable wherever you want. 然后在任何需要的地方回显此会话变量。

or if $username variable having username then simply echo it. 或者,如果$ username变量具有用户名,则只需回显它即可。 echo ($username = $_COOKIE['ID_my_site']); 回声($ username = $ _COOKIE ['ID_my_site']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM