Getting Alert to work After a redirect on a login page
up vote
0
down vote
favorite
I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php
map.php starts as follows:
<?php
session_start();
if ($_SESSION){
?>
then output a bunch of HTML and JAVASCRIPT then...
<?php } else {
header ('Location: ../login.php');
exit();
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');
</script>";
}
?>
If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php
php session redirect login alert
add a comment |
up vote
0
down vote
favorite
I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php
map.php starts as follows:
<?php
session_start();
if ($_SESSION){
?>
then output a bunch of HTML and JAVASCRIPT then...
<?php } else {
header ('Location: ../login.php');
exit();
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');
</script>";
}
?>
If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php
php session redirect login alert
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php
map.php starts as follows:
<?php
session_start();
if ($_SESSION){
?>
then output a bunch of HTML and JAVASCRIPT then...
<?php } else {
header ('Location: ../login.php');
exit();
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');
</script>";
}
?>
If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php
php session redirect login alert
I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php
map.php starts as follows:
<?php
session_start();
if ($_SESSION){
?>
then output a bunch of HTML and JAVASCRIPT then...
<?php } else {
header ('Location: ../login.php');
exit();
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');
</script>";
}
?>
If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php
php session redirect login alert
php session redirect login alert
edited Nov 18 at 9:32
asked Nov 18 at 2:06
grey krav
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
i wish to let the user know that he must be logged in in order to see the map.
You have mainly two different solutions to be able to handle the redirect.
Handle the redirect with PHP
Instead of showing an alert, you can output a normal text and use the refresh
header call in combination with the url
header call in order to send a header to the client browser that refresh the page after a certain number of seconds.
<?php
}
else
{
$message = "wrong answer";
header('Refresh: 5; url=login.php');
exit($message);
}
Handle the redirect with Javascript
Just output the alert script that you have already written and then handle the redirect through javascript instead of using header()
PHP function.
<?php
}
else
{
$message = "wrong answer";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your<a>
tag as<a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
i wish to let the user know that he must be logged in in order to see the map.
You have mainly two different solutions to be able to handle the redirect.
Handle the redirect with PHP
Instead of showing an alert, you can output a normal text and use the refresh
header call in combination with the url
header call in order to send a header to the client browser that refresh the page after a certain number of seconds.
<?php
}
else
{
$message = "wrong answer";
header('Refresh: 5; url=login.php');
exit($message);
}
Handle the redirect with Javascript
Just output the alert script that you have already written and then handle the redirect through javascript instead of using header()
PHP function.
<?php
}
else
{
$message = "wrong answer";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your<a>
tag as<a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
add a comment |
up vote
0
down vote
accepted
i wish to let the user know that he must be logged in in order to see the map.
You have mainly two different solutions to be able to handle the redirect.
Handle the redirect with PHP
Instead of showing an alert, you can output a normal text and use the refresh
header call in combination with the url
header call in order to send a header to the client browser that refresh the page after a certain number of seconds.
<?php
}
else
{
$message = "wrong answer";
header('Refresh: 5; url=login.php');
exit($message);
}
Handle the redirect with Javascript
Just output the alert script that you have already written and then handle the redirect through javascript instead of using header()
PHP function.
<?php
}
else
{
$message = "wrong answer";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your<a>
tag as<a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
i wish to let the user know that he must be logged in in order to see the map.
You have mainly two different solutions to be able to handle the redirect.
Handle the redirect with PHP
Instead of showing an alert, you can output a normal text and use the refresh
header call in combination with the url
header call in order to send a header to the client browser that refresh the page after a certain number of seconds.
<?php
}
else
{
$message = "wrong answer";
header('Refresh: 5; url=login.php');
exit($message);
}
Handle the redirect with Javascript
Just output the alert script that you have already written and then handle the redirect through javascript instead of using header()
PHP function.
<?php
}
else
{
$message = "wrong answer";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.
i wish to let the user know that he must be logged in in order to see the map.
You have mainly two different solutions to be able to handle the redirect.
Handle the redirect with PHP
Instead of showing an alert, you can output a normal text and use the refresh
header call in combination with the url
header call in order to send a header to the client browser that refresh the page after a certain number of seconds.
<?php
}
else
{
$message = "wrong answer";
header('Refresh: 5; url=login.php');
exit($message);
}
Handle the redirect with Javascript
Just output the alert script that you have already written and then handle the redirect through javascript instead of using header()
PHP function.
<?php
}
else
{
$message = "wrong answer";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.
edited Nov 18 at 2:52
answered Nov 18 at 2:39
gomd
1237
1237
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your<a>
tag as<a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
add a comment |
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your<a>
tag as<a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
– grey krav
Nov 18 at 9:49
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
– grey krav
Nov 18 at 20:17
You can set up your
<a>
tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.– gomd
Nov 19 at 1:37
You can set up your
<a>
tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>
. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.– gomd
Nov 19 at 1:37
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
– grey krav
Nov 20 at 2:16
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357288%2fgetting-alert-to-work-after-a-redirect-on-a-login-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown