So i am new to php and basically what i am trying to do is to create a login page and then i am checking for the username and the password from my database by calling a function called and after it has been verified it returns the array which contains the user name to the calling function which is login.php . and then i redirect it to another page which shows that the user i s logged in and displays the username but the problem i face is that there is only the first chracter of the user name stored in it here is the login code
<?php session_start();?>
<?php require_once("../includes/functions.php");?>
<?php
$username="";
$message="";
if(isset($_POST['submit']))
{
$username=$_POST["username"];
$password=$_POST["password"];
$found_admin=attempt_login($username,$password,$connection);
if($found_admin)
{
$use=$found_admin["username"];
$_SESSION["username"]=$use;
header("Location:admin.php");
exit;
} else {
$message="Username/Password not found";
}
}
And here is the function which checks for the username and password
<?php session_start():>
function attempt_login($username,$entered_password,$connection)
//passing the $connection variable to establish the connection with the database
{
//checking for username and password match if found
$safe_username=mysqli_real_escape_string($connection,$username);//making the string harmless by using this function
$query=" select * from admin where username = '{$safe_username}' ";//query to check if the username value is matched an found
$result=mysqli_query($connection,$query);//fetch object
$obj=mysqli_fetch_assoc($result);//storing the object value in an array
confirm_query($result,$connection);//confirm if query has been passed
$hashed_password=$obj["password"];//storing the value of array password into a variable
$user=$obj["username"];
$pass=password_verify($entered_password,$hashed_password);//checking for the password match
if($pass)//if password match is found
{
return $user;
}else{
return null;
}
}
?>
So when i perform a var_dump on the variable $found_admin i get the full string .But when i perform a var_dump on $_SESSION["username"] i get the following warning 'Illegal string offset 'username'' and it only displays the first character of the string not the full string.
I am trying to solve this problem for past 5 days but couldn't find any solution.PLEASE HELP .