简体   繁体   中英

how to redirect user to profile page if already user is logedin in php mysql

i have home page with login fields id and password if user login with id and password its redirect to profile.php its working fine, but when i open new tab its again showing login page not the profile page. here is my code bellow

<?php
session_start();

include('includes/config.php');
if(isset($_POST['login']))
{
$status='1';
$email=$_POST['username'];
$password=md5($_POST['password']);
$sql ="SELECT email,password FROM users WHERE email=:email and password=:password and status=(:status)";
$query= $dbh -> prepare($sql);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':password', $password, PDO::PARAM_STR);
$query-> bindParam(':status', $status, PDO::PARAM_STR);
$query-> execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 0)
{
$_SESSION['alogin']=$_POST['username'];

echo "<script type='text/javascript'> document.location = 'profile.php'; </script>";
} else{

echo "<script>alert('Invalid Details Or Account Not Confirmed');</script>";

}

}

?>

how to redirect user to profile.php if user is already logged in, if open in new tab or new window

this is the same login page in "admin" Directory

<?php
session_start();
if(isset($_SESSION['alogin'])){
 header('Location: dashboard.php');
}
include('includes/config.php');
if(isset($_POST['login']))
{
$email=$_POST['username'];
$password=md5($_POST['password']);
$sql ="SELECT UserName,Password FROM admin WHERE UserName=:email and Password=:password";
$query= $dbh -> prepare($sql);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':password', $password, PDO::PARAM_STR);
$query-> execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 0)
{
$_SESSION['alogin']=$_POST['username'];
echo "<script type='text/javascript'> document.location = 'dashboard.php'; </script>";
} else{

  echo "<script>alert('Invalid Details');</script>";

}

}

?>

this is the logout.php both are same only directory are different

<?php
session_start(); 
$_SESSION = array();
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 60*60,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}
unset($_SESSION['login']);
session_destroy(); // destroy session
header("location:index.php"); 
?>

problem is if i login both in same window in different tab if i login user then admin also logged in and if i logout any of them user or admin both are logout

Well you might add this code to your php code. Here's what you should do

<?php
session_start();
if(isset($_SESSION['alogin'])){
 header('Location: profile_page.php');
}

Hope so it helps you, well there are a lot of questions like this.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM