How to get logged person's user_id from the database in php [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to get logged person's user_id from the database in php [duplicate]



This question already has an answer here:



I'm new to the Sessions topic so, I've been trying to get the logged person's id(column) from the userdetails table using sessions and insert it into the another variabl but couldn't achieve.I would appreciate if anyone could help me out on how to achieve.



PHP Code here.....



login.php


<?php

include("login_connect.php");
session_start();

if($_SERVER["REQUEST_METHOD"]=="POST")

// sent user name and password
$loginusername = mysqli_real_escape_string($conn,$_POST['userName']);
$loginuserpassword = mysqli_real_escape_string($conn,$_POST['userPassword']);

$sql = "SELECT id FROM userdetails WHERE UserName = '$loginusername' and UserPassword = '$loginuserpassword'";
$result = mysqli_query($conn, $sql);

// show result error_get_last
if (!$result)
printf("Error: %sn", mysqli_error($conn));
exit();


$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $loginusername and $loginuserpassword, table row must be 1 row
if ($count == 1)
$_SESSION['name'] = $loginusername;
header("location: userwelcome.php");
else
echo "Your Login Name or Password is invalid";




?>



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



This question came from our site for computer enthusiasts and power users.





It's not clear what your actual issue is. The code you shown actually uses the session to store the username. So what's unclear about storing the user ID? As a friendly hint try to be consistent with your formatting choices. Sometime you had spaces around the = for assignments sometimes you had not. Also consider having a look at prepared statements at some point.
– Seth
Aug 10 at 10:33


=





There's only the one piece of code for one file. If sessions isn't working, it's not in what you posted, but possibly in the other file(s). Use php's error reporting for this and var_dump() the session array.
– Funk Forty Niner
Aug 10 at 21:09


var_dump()





You're not salt and hashing your passwords. Please fix that ASAP. You should consider making a User class and then saving that class in the session
– Chad K
Aug 10 at 21:10




Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard