How to display popup message when logged out user try to vote ?
Last Updated :
29 Jun, 2020
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:
<!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 >
|
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:
<!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 >
|
Consider the following code inside the check.php file:
<?php
if (!isset( $_SESSION [ '' ])) {
echo "<script type='text/javascript'>" ;
echo "alert('User not logged in!')" ;
echo "</script>" ;
}
?>
|
Output:
- Before Clicking on Vote button:
- After clicking on Vote button:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...