gpt4 book ai didi

php - 我们如何在php中获取当前登录用户的用户名?

转载 作者:行者123 更新时间:2023-11-30 22:52:55 25 4
gpt4 key购买 nike

我想在包含设置、配置文件和注销选项的下拉列表中显示登录用户的用户名。这是 login.php

<?php
session_start(); // Starting Session
$error[] = ''; // Variable To Store Error Message
require 'function/general.php';
require 'function/users.php';
include 'includes/db_connect.php';


if(empty($_POST)=== false){
$username = $_POST['username'];

$password = $_POST['password'];

if(empty($username) === true || empty($password) === true){
$error[] = "you need to enter username and password";
}
else if(user_exists($username)=== false){
$error[] = "cant find username . have you registerd";
}
else if (user_active($username)=== false) {
$error[] = "havent activated account";

}else{
$login = login($username,$password);
if($login === false){
$error[] = "invalide login credentials";
}
else{
$_SESSION['id'] = $login;

header("Location: index.php");
exit();
}


}


}

?>

这里是包含所有函数的users.php

<?php

function logged_in(){
return(isset($_SESSION['id'])) ? true : false;

}

function user_exists($username) {
$username = sanitize($username);


return (mysql_result( mysql_query("SELECT COUNT(`id`) FROM `login` WHERE `username` ='$username' "), 0) == 1) ? true : false;

}

function user_active($username) {
$username = sanitize($username);


return (mysql_result( mysql_query("SELECT COUNT(`id`) FROM `login` WHERE `username` ='$username' AND `active` = 1 "), 0) == 1) ? true : false;

}

function user_id_from_username($username){
$username = sanitize ($username);
return mysql_result(mysql_query("SELECT `id` FROM `login` WHERE `username` = '$username'"),0, 'id');
}

function login($username , $password){

$id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `login` WHERE `username` ='$username' AND `password` = '$password'") , 0) == 1) ? $id : false;

}

这里是我想显示登录用户名的地方

<?php
session_start();

include 'includes/bootstrap.html';


require 'function/general.php';
require 'function/users.php';
require 'includes/bootstrap.html';
?>

<script>
$('.dropdown-toggle').dropdown();

</script>
<!---------navbar --->

<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php"><h3>Utile</h3></a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="index.php">Home <span class="sr-only">(current)</span></a></li>
<li><a href="#">Browse Photos</a></li>

</ul>

<?php if (logged_in() === true) {
?>
<!-- <ul class="nav navbar-nav navbar-right">
<li><a href="logout.php">Logout</a><a href="profile.php">Profile</a><a href="settings.php">Settings</a></li>

-->
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> **here i want username** <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="logout.php">Logout</a></li>
<li><a href="profile.php">Profile</a></li>
<li><a href="#">Setting</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>

<?php
} else {
?>

<ul class="nav navbar-nav navbar-right">
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Sign Up</a></li>
<?php } ?>

<li><a href="registerpro.php"><button type="button" class="btn btn-primary">Become Professional</button></a></li>

</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

最佳答案

如果你关注过phpacademy的登录系统那么你一定有以下功能

function user_data($user_id) {
$data = array();
$user_id = (int)$user_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if ($func_num_args > 1) {
unset($func_get_args[0]);

$fields = '`' .implode('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));

return $data;
}
}

function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

然后你可以从中获取用户名

    $username= $user_data['username']; 

关于php - 我们如何在php中获取当前登录用户的用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com