How to display popup message when logged out user try to vote ?

How users logged in or logged out is detected?
When a user satisfies the conditions for login his status changes to logged in. For a website or system to understand that a user is logged in usually a $_SESSION[‘ ‘] variable is set as soon as the user logs in. This variable when set helps the website to recognize that a user is present and provides access to all the available functionalities.

For example, when we log into a website it usually displays messages like:

WELCOME Name_Of_The_user

So how does this happen?
When users login to a website, the website recognizes the user and extracts the necessary information about the user from the database for proper functioning. At the same time $_SESSION[‘ ‘] variable is set to help every page on the website be aware that a user is in session or logged in.

So, if a user is not logged in, the $_SESSION[‘ ‘] variable is not set. To display a popup message when a user is logged out we simply need to run an if loop. When a user clicks on a button direct them to a PHP file or a function within the same file that checks whether the user is logged in or not.

Example: When PHP function exists inside the same file.



Consider the following code inside HTML file:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to display popup message 
        when logged out user try to 
        vote?
    </title>
</head>
  
<body>
    <form method="POST" action="">
        <p> VOTE HERE! </p>
        <button type="submit">Vote </button>
    </form>
  
    <?php
        if(!isset($_SESSION[''])) {
            echo " <script type='text/javascript'>";
            echo "alert('User not logged in!')";
            echo " </script>"; 
        }
    ?>
</body>
  
</html>

chevron_right


Output:

  • Before Clicking on Vote button:
  • After clicking on Vote button:

Example: When PHP function exists in another file.

Consider the following code inside HTML file:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to display popup message 
        when logged out user try to 
        vote?
    </title>
</head>
  
<body>
    <form method="POST" action="check.php">
        <p> VOTE HERE! </p>
        <button type="submit">Vote </button>
    </form>
</body>
  
</html>

chevron_right


Consider the following code inside the check.php file:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    if(!isset($_SESSION[''])) {
      echo "<script type='text/javascript'>";
      echo "alert('User not logged in!')";
      echo "</script>"
    }
?>

chevron_right


Output:

  • Before Clicking on Vote button:
  • After clicking on Vote button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.