How to publish full name from MySQL using only username as login in PHP?

SOLVED: after rewriting mysql code and membership, the last one worked several times when mysqlind (native driver) was enabled.


This script login works fine for me, but I want it to publish on my pages the full name of the user who is logged in.

I want it to retrieve and publish the first and last name in a div on any given page, using the username given to the session at login.

Script input:

<?php session_start(); require_once 'classes/membership.php'; $membership = new Membership(); // If the user clicks "Log Out". if(isset($_GET['status']) && $_GET['status'] == 'loggedout') { $membership->log_User_Out(); } // Did the user enter a password/username and click submit? if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) { $response = $membership->validate_User($_POST['username'], $_POST['pwd']); } ?> 

Verifying credentials in member.php:

 <?php require 'mysql.php'; class Membership { function validate_user($un, $pwd) { $mysql = New Mysql(); $ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd)); list($ensureCredentials, $data) = $mysql->verify_Username_and_Pass($un, md5($pwd)); if($ensure_credentials) { $_SESSION['status'] = 'authorized'; $_SESSION['fname'] = $data['fname']; $_SESSION['lname'] = $data['lname']; header("location: medlem.php"); } else return "Please enter correct username and password"; print_r($row); exit; } function log_User_Out() { if(isset($_SESSION['status'])) { unset($_SESSION['status']); if(isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 1000); session_destroy(); } } function confirm_Member() { session_start(); if($_SESSION['status'] !='authorized') header("location: login.php"); } } 

Establishing a connection to mysql in mysql.php:

 <?php require_once 'includes/constants.php'; class Mysql { private $conn; function __construct() { $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.'); } function verify_Username_and_Pass($un, $pwd) { $query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1"; if($stmt = $this->conn->prepare($query)) { $stmt->bind_param('ss', $un, $pwd); $stmt->execute(); // UPDATE : I added correct usage of the stmt here. $result = $stmt->get_result(); if($row = $result->fetch_array()) { $stmt->free_result(); $stmt->close(); // returning an array the first item is the validation the second is the data. return array(true, $row); } } // if there is no just return empty data, and false for validation. return array(false, array()); } 

And for the sake of reuse, I define constants in .php constants:

 <?php // Define constants here define('DB_SERVER', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_NAME', 'membership'); 

How can I easily get it to extract firstname (fname) and lastname (lname) from the MySQL database when the session knows only the username of the registered user.

As I said above, the string names for the first and last name are fname and lname.

I want to show it as simple as possible with a floating CSS-formatted id'ed topr div, for example:

 <?php session_start(); if(isset($_SESSION['status'])){ echo "<div id='topr'>Welcome, " . $_SESSION['fname'] . "!</div>"; } ?> 

I mainly focus on displaying the first name. By the time I get this right, I will most likely have the last name displayed.

Just want to know more about this. Since my Internet searches did not come up with anything that I could implement in my code without delving into something.

+5
source share
2 answers

Change the check function as follows. I have added relevant comments.

 function verify_Username_and_Pass($un, $pwd) { $query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1"; if($stmt = $this->conn->prepare($query)) { $stmt->bind_param('ss', $un, $pwd); $stmt->execute(); // UPDATE : I added correct usage of the stmt here. $result = $stmt->get_result(); if($row = $result->fetch_array()) { $stmt->free_result(); $stmt->close(); // returning an array the first item is the validation the second is the data. return array(true, $row); } } // if there is no just return empty data, and false for validation. return array(false, array()); } 

So, in your MemberShip class, where you call verify_Username_and_Pass, just enter the array data into the variables. and use it to send to session variables. This way you can use the session wherever you want.

  list($ensureCredentials, $data) = $mysql->verify_Username_and_Pass($un, md5($pwd)); if($ensure_credentials) { $_SESSION['status'] = 'authorized'; $_SESSION['first_name'] = $data['first_name']; $_SESSION['last_name'] = $data['last_name']; header("location: medlem.php"); } else return "Please enter correct username and password"; 
+4
source

One way to do this:

In verify_Username_and_Pass() instead of returning only the boolean value true, return the first and last name; on error / failure, return false.

Thus, the if statement in validate_user() in the Membership class can still work as it is. Then all you have to do is save this information in your session through $_SESSION['full_name'] = $ensure_credentials;

0
source

Source: https://habr.com/ru/post/1244802/


All Articles