4

I would like to display a popup message when user is logged out, so I use

echo "<script>alert(\"You are logged out\");</script>";

But it doesn't work.

Below is my coding. Is there any logic problem in my coding?

<?php
session_start();
if(isset($_SESSION['Username']) == "admin")
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
@import "../CSS/Style.css";
@import "../CSS/Admin.css";
</style>
<title>Admin Home Page</title>
</head>

<body>
<div class="body"></div>
<?php
    if(isset($_GET['id']) == "logout")
    {
        session_destroy();
        echo "<script>alert(\"You are logged out\");</script>";
        header("Location: ..\Main.php");
    }
    else
    {
?>
<div class="menu">
    <a href="ManageStaff.php">Manage Staff</a>
</div>

<div class="menu2">
    <a href="ManageAccount.php">Manage Account</a>
</div>

<div class="logout">
    <a href="AdminHomePage.php?id=logout">Logout</a>
</div>
<?php
    }
}
else
{
?>
<center>
<p style="font-size:50px; font-weight:bold">Access Denied</p>
<p style="font-size:18px">Your request for this page has been denied because of access control</p>
</center>
<?php
}
?>
</body>
</html>

The session will be destroyed and will also redirect to Main.php, just the alert() will not come out.

2
  • Firstly, you're not using isset() correctly [docs]. Secondly, you can't use header() after output has been sent. Again, this is clearly stated in the php docs: php.net/manual/en/function.header.php. Finally, assuming you could, how do you expect your code to work? You can't mix PHP and Javascript; if you're using php to redirect the user, this would happen before anything is sent to the user, thus the javascript alert would never fire. You really need to think about your solution a bit more. Commented Apr 23, 2015 at 6:33
  • isset will only ever return true or false. Commented Apr 23, 2015 at 6:33

5 Answers 5

9

You're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.

echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";

Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.

So instead of

if(isset($_SESSION['Username']) == "admin")

You need to do:

if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin") 
Sign up to request clarification or add additional context in comments.

2 Comments

That's true; he does ask if there's 'any logic problem in my coding', and his misuse of isset will cause his logic to malfunction. I answered his actual question, which seemed to be about why his alert wasn't visible, the answer to which is that his redirect caused it never to be processed by the browser.
I think you'll find that the actual reason is misuse of isset causes the whole page to always inform you that there is an access violation. Also, the header call after output would also cause an error because headers have already been sent by that point.
2

header("Location: ..\Main.php"); tells the browser to go to another page before it even shows the page... if you want the user to see the alert, try this:

session_destroy();
echo "<script>";
echo "alert('You are logged out');";
echo "window.location = '../Main.php';"; // redirect with javascript, after page loads
echo "</script>";

Comments

2

use this it will solve your problem!! first change your code from

if(isset($_SESSION['Username']) == "admin")
{ 

to

if(!empty($_SESSION['Username']) && ($_SESSION['Username']=="admin")){

and than use following code

 if(!empty($_GET['id']) && ($_GET['id']=="logout"))
        {
          session_destroy();?>
             <script>
             alert("You are logged out");
             window.location.href='..\Main.php';
             </script>
           <?php }?>

1 Comment

@bob yeah.. i no just updating
0

the sleep function may to help you without to modify the header or include

Comments

-1

Try, This must work, And remove php header, replace with the following code.

 echo "<script>alert('You are logged out');
 location.href='..\Main.php';
 </script>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.