0

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

  • Does this answer your question? [How do I make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php) – Ron May 13 '20 at 04:38

1 Answers1

1

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.

Uncias
  • 82
  • 8
  • now i have new problem i have user and admin, the problem is if i login from admin/index.php and in new tab if i login as user from base directory index.php its logged in both separately, but if i logout admin then its logout the user alos – user3244304 May 13 '20 at 04:56
  • Is that your whole code. If no pleas provide all the code – Uncias May 13 '20 at 05:22
  • can you please give the code or the screenshot of the project – Uncias May 13 '20 at 05:41
  • try removing ` session_destroy();` – Uncias May 13 '20 at 05:47
  • already tried i am not a developer started learning admin and user db table are different one is admin table and one is user table – user3244304 May 13 '20 at 05:56
  • Sorry for a late reply. I actually got busy. You can use cookie it is same as session but better and easy to use – Uncias May 13 '20 at 06:21
  • But it might be better to understand the basic codes properly first. Because cookie and session are vast topics in PHP. – Uncias May 13 '20 at 06:29