简体   繁体   中英

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

if $username variable having email id then Just select the username using your existing query. then save in session variable or cookie. then echo this session variable wherever you want.

or if $username variable having username then simply echo it. echo ($username = $_COOKIE['ID_my_site']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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