我已经构建了登录系统,我希望在用户登录时显示用户名和名字 [英] I already build login system and I want to display username and first name when user logged in
问题描述
这些代码没有显示任何错误,但我没有收到用户名
function display_username(){
// 启动会话
// 3。如果表单已提交。
// 3.1如果提交表单
if (isset($ _ POST [' username'])和 isset($ _ POST [' 密码'])){
// 3.1.1分配已发布值变量。
// 3.1.2检查值是否存在是否在数据库中
$ query = SELECT * FROM`users` WHERE username ='$ username'和password ='$ password';
$ result = mysqli_query($ connection,$ query)或 die( mysqli_error($连接));
$ count = mysqli_num_rows($ result);
// 3.1.2如果发布的值等于数据库值,则会创建会话用户。
// if($ count == 1){
$ _SESSION [' username'] = $用户名跨度>;
// } else {
// 3.1.3如果登录凭据不匹配,将显示错误消息。
// $ fmsg =无效的登录凭证。;
// }
// }
// 3.1.4如果用户已登录用消息向用户致意
if (isset($ _ SESSION [' username'])){
$ username = $ _SESSION [' 用户名'];
echo Hayyyyyy 。 $ username 。
;}
}
}
/ ****** **********注册用户功能******************** /
function register_user($ first_name,$ last_name,$ username,$ email,$ password){
$ first_name = escape($ first_name);
$ last_name = escape($ last_name);
$ username = escape($ username);
$ email = escape($ email);
$ password = escape($ password);
if(email_exists($ email)){
return false;
} else if (username_exists($ username)) {
return false;
} else {
$ password = MD5($密码);
$ validation_code = md5($ username。microtime());
$ sql = INSERT INTO用户(first_name,last_name,username,email,password,validation_code,active);
$ sql。= VALUES('$ first_name','$ last_name','$ username', '$ email','$ password','$ validation_code',0);
$ result = query($ sql);
确认($ result);
$ subject = 激活帐户;
$ msg = 请点击链接以下激活您的账户
http://omilearn.com/activate.php?email=$email&code=$validation_code
;
$ headers = 来自:noreply@omilearn.com;
send_email($ email,$ subject,$ msg,$ headers);
return true;
}
}
< pre lang =PHP> < pre lang = PHP < span class =code-keyword>>
/ ****************验证函数********* *********** /
function validate_user_registration(){< br $>
$ errors = [];
$ min = 3;
$ max = 20;
// if($ _ SERVER ['REQUEST_METHOD'] == POST){
if(isset($ _ POST ['register-submit'])){
$ first_name = clean( $ _POST ['first_name']);
$ last_name = clean($ _ POST ['last_name']);
$ username = clean($ _ POST ['username' ]);
$ email = clean($ _ POST ['e mail']);
$ password = clean($ _ POST ['password']);
$ confirm_password = clean($ _ POST ['confirm_password']); < br $> b $ b
if(strlen($ first_name)< $ min){
$ errors [] =你的名字不能少于{$ min}个字符;
}
if(strlen($ first_name)> $ max){
$ errors [] =你的名字不能超过{$ max}个字符;
}
if(strlen($ last_name)< $ min){
$ errors [] =你的姓氏不能少于{$ min}字符;
}
if(strlen($ last_name)> $ max){
$错误[] =你的姓氏不能超过{$ max}个字符;
}
if(strlen($ username)< $ min){
$ errors [] =你的用户名不能少于{$ min}个字符; < br $> b $ b
}
if(strlen($ username)> $ max){
$ errors [] =您的用户名不能超过{$ max}个字符;
}
if(username_exists($ username)){
$ errors [] =抱歉,用户名已被占用;
$ / b $ b $
if(email_exists($ email) ){
$ errors [] =很抱歉,电子邮件已经注册;
} < br $>
if(strlen($ email)< $ min){
$ errors [] =您的电子邮件不能超过{$ max}个字符;
}
if($ password!== $ confirm_password){
$ errors [] =您的密码字段不匹配;
}
if(!empty($ errors)){
foreach($ errors as $ error){
echo validation_errors($ error);
}
}否则{
if(register_user( $ first_name,$ last_name,$ username,$ email,$ password)){
set_message (
请检查您的电子邮件或垃圾邮件文件夹以获取激活链接
);redirect( index.php);
}否则{
set_message(
抱歉,我们无法注册用户
);redirect(index.php);
}
}
} //发布请求
} //功能
我的尝试:
i尝试了很多代码但没有工作
<?php display_username('$ username'); ?>
<?php $ _SESSION ['用户名']?>
<?php
session_start(); < br $>
echo $ _SESSION ['username']; ?> ;;
函数display_username(){
$ query = mysqli_query($ con,' SELECT username FROM users WHERE users =$ _ SESSION ['username']');
$ fetch = mysqli_fetch_assoc($ con,$ query);
$ userNameToShow = $ fetch ['username'];
}
<?php display_username()?>
if (isset($ _ SESSION ['email'])
_POST [' 用户名'])和 isset(
_POST [' password'])){
// 3.1.1将过帐的值分配给变量。
// 3.1.2检查值是e数据库中的xisting是否
query = SELECT * FROM`users` WHERE username ='
these code doesnt show any error but i am not getting username
function display_username() {
//Start the Session
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
//if ($count == 1){
$_SESSION['username'] = $username;
//}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
//$fmsg = "Invalid Login Credentials.";
//}
//}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hayyyyyy" . $username . "
";}
}
}
/****************Register user functions ********************/
function register_user($first_name, $last_name, $username, $email, $password) {
$first_name = escape($first_name);
$last_name = escape($last_name);
$username = escape($username);
$email = escape($email);
$password = escape($password);
if(email_exists($email)) {
return false;
} else if (username_exists($username)) {
return false;
} else {
$password = md5($password);
$validation_code = md5($username . microtime());
$sql = "INSERT INTO users(first_name, last_name, username, email, password, validation_code, active)";
$sql.= " VALUES('$first_name','$last_name','$username','$email','$password','$validation_code', 0)";
$result = query($sql);
confirm($result);
$subject = "Activate Account";
$msg = " Please click the link below to activate your Account
http://omilearn.com/activate.php?email=$email&code=$validation_code
";
$headers = "From: noreply@omilearn.com";
send_email($email, $subject, $msg, $headers);
return true;
}
}
<pre lang="PHP">
/****************Validation functions ********************/
function validate_user_registration(){
$errors = [];
$min = 3;
$max = 20;
// if($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['register-submit'])) {
$first_name = clean($_POST['first_name']);
$last_name = clean($_POST['last_name']);
$username = clean($_POST['username']);
$email = clean($_POST['email']);
$password = clean($_POST['password']);
$confirm_password = clean($_POST['confirm_password']);
if(strlen($first_name) < $min) {
$errors[] = "Your first name cannot be less than {$min} characters";
}
if(strlen($first_name) > $max) {
$errors[] = "Your first name cannot be more than {$max} characters";
}
if(strlen($last_name) < $min) {
$errors[] = "Your Last name cannot be less than {$min} characters";
}
if(strlen($last_name) > $max) {
$errors[] = "Your Last name cannot be more than {$max} characters";
}
if(strlen($username) < $min) {
$errors[] = "Your Username cannot be less than {$min} characters";
}
if(strlen($username) > $max) {
$errors[] = "Your Username cannot be more than {$max} characters";
}
if(username_exists($username)){
$errors[] = "Sorry that username is already is taken";
}
if(email_exists($email)){
$errors[] = "Sorry that email already is registered";
}
if(strlen($email) < $min) {
$errors[] = "Your email cannot be more than {$max} characters";
}
if($password !== $confirm_password) {
$errors[] = "Your password fields do not match";
}
if(!empty($errors)) {
foreach ($errors as $error) {
echo validation_errors($error);
}
} else {
if(register_user($first_name, $last_name, $username, $email, $password)) {
set_message("
Please check your email or spam folder for activation link
");redirect("index.php");
} else {
set_message("
Sorry we could not register the user
");redirect("index.php");
}
}
} // post request
} // function
What I have tried:
i tried many codes but didnt work
<?php display_username('$username'); ?>
<?php $_SESSION['username'] ?>
<?php
session_start();
echo $_SESSION['username']; ?>;
function display_username() {
$query=mysqli_query($con, ‘SELECT username FROM users WHERE users="$_SESSION[‘username’]"’);
$fetch=mysqli_fetch_assoc($con, $query);
$userNameToShow = $fetch[‘username’];
}
<?php display_username() ?>
if(isset($_SESSION['email'])
_POST['username']) and isset(
_POST['password'])){ //3.1.1 Assigning posted values to variables. //3.1.2 Checking the values are existing in the database or not
query = "SELECT * FROM `users` WHERE username='
这篇关于我已经构建了登录系统,我希望在用户登录时显示用户名和名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!