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();
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.