I'm newbie in php coding. Just start it a few weeks ago. I am making login, register and profile pages. I have a problem to display username that currently logged in. I tried all reference or tutorial and other forums. but still not working. Someone, please help me to solve.
Here is my auth.php
<?php
include ("config.php");
session_start();
if(!isset($_SESSION["user"])) header("Location: login.php");
?>
and the login.php
<?php
require_once("config.php");
if(isset($_POST['login'])){
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$sql = "SELECT * FROM users WHERE username=:username OR email=:email";
$stmt = $db->prepare($sql);
$params = array(
":username" => $username,
":email" => $username
);
$stmt->execute($params);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Registered
if($user){
// verifikasi password
if(password_verify($password, $user["password"])){
// Make Session
session_start();
$_SESSION["user"] = $user;
//success and redirect
header("Location: index.php");
}
}
}
?>
<!DoctypeHTML>
<html>
<head>
</head>
<body>
</body>
</html>
Thank you
[PS = Sorry for my english]
In index.php
, write these lines.
<?php
if(isset($_SESSION['user'])){
echo $_SESSION['user']->fieldName; // here you need to retrieve fields
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.